how to know is it iphone or ipad? - iphone

i want to know the user uses the iphone or ipad,if the user uses the iphone i want to open the camera,if he uses the ipad or runs in simulator i want to open the library. how it is possible?
how to find the details of devices?
how to know current using device by user through xcode?

NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:#"iPhone"])
{
//your code
}
.....
Hope this helps.
EDIT:
See this thread -determine-device-iphone-ipod-touch-with-iphone-sdk .

[[UIDevice currentDevice].model hasPrefix:#"iPhone"]
Use the "hasPrefix" so that it works in simulator.

You should not determine whether there is a camera by looking at the model. This is not future proof - for instance, you would not be supporting the iPad 2's camera.
UIImagePickerController has a special method to determine whether a camera in available:
+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType
With sourceType being one of
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum

Make use of this to identify devices.
// If iPhoneOS is 3.2 or greater then __IPHONE_3_2 will be defined
#ifndef __IPHONE_3_2
typedef enum {
UIUserInterfaceIdiomPhone, // iPhone and iPod touch
UIUserInterfaceIdiomPad, // iPad
} UIUserInterfaceIdiom;
#define UI_USER_INTERFACE_IDIOM() UIUserInterfaceIdiomPhone
#endif // ifndef __IPHONE_3_2
but if you want to check if camera is available I think you can make use of UIImagePickerController's static method
+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType

Working on Vaibhav Tekam's answer, I used this
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType hasPrefix:#"iPhone"])
{
//your code
}
or
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType hasPrefix:#"iPad"])
{
//your code
}
etc.
It's much easier that way as it covers all models.

Related

iPhone : How to check device using MACRO?

I want to check whether the device is iPhone or iPad using the macro.
I have a file Constant.h where I have given values using #define.
Now, I want to check device using #ifdef #endif.
Follwing method can be possible only in the .m file.
But I have only one .h only.
- (BOOL) isPad{
#ifdef UI_USER_INTERFACE_IDIOM
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#else
return NO;
#endif
}
So above method is not useful for me ?
Is there any way to do this ? Or any other way?
I have simple answer to this question.
#define isiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? YES : NO)
This will returns 1 if device is iPad and 0 if device is iPod or iPhone.
You can't check it by macro, because macro is expanded during compilation. So you need to know device type at compile time.
If you want to support both devices at runtime, you need to check device type and use appropriate set of constants.
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
UIDevice* thisDevice = [UIDevice currentDevice];
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
// etc.
}
#endif

ipad UIImagePickerController problem?

My app is an iphone app not an universal app.I am implementing uiimagepickercontroller it's working fine in iphone .But our client test my app on ipad the app will crash.I find out the issue 'On iPad, UIImagePickerController must be presented via UIPopoverController'
I am checking device by
NSString *deviceType = [UIDevice currentDevice].model;
(This code work in universal app)
implementing UIPopoverController for iPad only. But deviceType always displays 'iPhone' even if i change to iPad simulator.Is it possible to solve this issue in iphone Build?
You should use UI_USER_INTERFACE_IDIOM macro to check for device.
if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM())
{
// your code
}
UIDevice is not the right choice here.
Refer to userInterfaceIdiom property of UIDevice
I did iPad for separately to solve this issue.because i can't get a device type iPad in iPhone app.
Like Erica Sadun said you can access device string with:
- (NSString *) getSysInfoByName:(char *)typeSpecifier
{
size_t size;
sysctlbyname(typeSpecifier, NULL, &size, NULL, 0);
char *answer = malloc(size);
sysctlbyname(typeSpecifier, answer, &size, NULL, 0);
NSString *results = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];
free(answer);
return results;
}

determine ipad device

I am writing a universal app for both iphone and ipad. How do I determine if the device is an iPad. I have used this link to determine the iPhone type (3G, 3GS).
Determine device (iPhone, iPod Touch) with iPhone SDK
It is highly recommended that you not do device type detection for determining if the application is running on an iPad, but that you examine either features or the user interface idiom. Many applications that test just for specific device types break when new hardware comes out (which tends to be pretty frequent).
Usually, if you need to determine if an application is running on an iPad, it is because you need to adjust the user interface to match the larger display area of the device. For that, Apple recommends that you check the user interface idiom using code like the following:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// iPad-specific interface here
}
else
{
// iPhone and iPod touch interface here
}
Brad's solution is absolutely right. If you're building a universal app designed to run on iPhones with older OS along with up-to-date iPads and iPhones, you might want to add this code to catch situations where the idiom is not defined.
// If iPhoneOS is 3.2 or greater then __IPHONE_3_2 will be defined
#ifndef __IPHONE_3_2
typedef enum {
UIUserInterfaceIdiomPhone, // iPhone and iPod touch
UIUserInterfaceIdiomPad, // iPad
} UIUserInterfaceIdiom;
#define UI_USER_INTERFACE_IDIOM() UIUserInterfaceIdiomPhone
#endif // ifndef __IPHONE_3_2
Here is the best answer I got:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
//iPhone methods
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//iPad methods
}
try this
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
// etc.
}
else
{
//iphone
}
if ( [(NSString*)[UIDevice currentDevice].model isEqualToString:#"iPad"] ) {
NSLog(#"...in iPad");
} else {
NSLog(#"...in iPhone/iPod");
}

How to detect an iPhone app is running on iPhone 4 device or not?

I try to update an iPhone application to support the larger screen resolution on iPhone 4. How to detect if my app is running on iPhone 4 or not?
Thanks in advance.
i can trace my iPhone 3GS through this code
struct utsname systemInfo;
uname(&systemInfo);
NSLog(#"%#", [NSString stringWithCString:systemInfo.version
encoding:NSUTF8StringEncoding]);
i hope it will work for iPhone 4
You could try using this method
-(NSString *) getDeviceType {
return [UIDevice currentDevice].model;
}
Or just get it like this:
NSLog(#"%#", [UIDevice currentDevice].model);
To check if a device is retina capable do the following:
BOOL result;
if ([[UIScreen mainScreen] scale] == 2.0) result = TRUE; else result = FALSE;

Best way to programmatically detect iPad/iPhone hardware

The reason I need to find out is that on an iPad, a UIPickerView has the same height in landscape orientation as it does in portrait. On an iPhone it is different. The iPad programming guide introduces an "idiom" value to UIDevice:
UIDevice* thisDevice = [UIDevice currentDevice];
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
// iPad
}
else
{
// iPhone
}
which works OK while you're in iPad (3.2) but not iPhone (3.1.3) - so it looks like there also needs to be an ifdef to conditionally compile that check, like:
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
UIDevice* thisDevice = [UIDevice currentDevice];
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
// etc.
}
#endif
To me that's starting to look very clumsy. What's a better way?
Checking at runtime (your first way) is completely different from #if at compile time. The preprocessor directives won't give you a universal app.
The preferred way is to use Apple's Macro:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// The device is an iPad running iPhone 3.2 or later.
}
else
{
// The device is an iPhone or iPod touch.
}
Use 3.2 as the base SDK (because the macro is not defined pre 3.2), you can target prior OS versions to get it running on the iPhone.
I'm answering this now (and at this late date) because many of the existing answers are quite old, and the most Up Voted actually appears to be wrong according to Apples current docs (iOS 8.1, 2015)!
To prove my point, this is the comment from Apples header file (always look at the Apple source and headers):
/*The UI_USER_INTERFACE_IDIOM() macro is provided for use when
deploying to a version of the iOS less than 3.2. If the earliest
version of iPhone/iOS that you will be deploying for is 3.2 or
greater, you may use -[UIDevice userInterfaceIdiom] directly.*/
Therefore, the currently APPLE recommended way to detect iPhone vs. iPad, is as follows:
1) (DEPRECATED as of iOS 13) On versions of iOS PRIOR to 3.2, use the Apple provided macro:
// for iPhone use UIUserInterfaceIdiomPhone
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
2) On versions of iOS 3.2 or later, use the property on [UIDevice currentDevice]:
// for iPhone use UIUserInterfaceIdiomPhone
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
I like my isPad() function. Same code but keep it out of sight and in only one place.
My solution (works on 3.2+):
#define IS_IPHONE (!IS_IPAD)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)
then,
if (IS_IPAD)
// do something
or
if (IS_IPHONE)
// do something else
In Swift use userInterfaceIdiom instance property as-
if UIDevice.current.userInterfaceIdiom == .phone {
print("iPhone")
}
& For other devices -
switch UIDevice.current.userInterfaceIdiom {
case .pad:
print("iPad")
case .phone:
print("iPhone")
case .tv:
print("TV")
case .carPlay:
print("carPlay")
default: break;
}
Put this method in your App Delegate so that you can call it anywhere using [[[UIApplication sharedApplication] delegate] isPad]
-(BOOL)isPad
{
BOOL isPad;
NSRange range = [[[UIDevice currentDevice] model] rangeOfString:#"iPad"];
if(range.location==NSNotFound)
{
isPad=NO;
}
else {
isPad=YES;
}
return isPad;
}
If you are using features that are not backwards compatible, I found the best way for me is to create a #define in the pre-compiled header. Example:
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_3_2
#define USING_4_X
#endif
Then in your code, you can do this:
BOOL exists = NO;
#ifdef USING_4_X
exists = [SomeObject someMethod:[url lastPathComponent]];
#else
exists = [SomeObject someMethod:[[url path] lastPathComponent]];
#endif
If
1- you already have the app installed into your device,
2- you change its build settings to be a 'Universal' app,
3- install the app to your device on top of the pre-existing app (without deleting the previous one)
You might find that the solutions provided here to detect iPhone/iPad do not work. First, delete the app that was 'only' for iPad/iPhone and install it fresh to your device.
BOOL isIpad()
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES;
}
return NO;
}
extension UIDevice {
var isIPad: Bool {
return UIDevice.current.userInterfaceIdiom == .pad
}
}