ipad UIImagePickerController problem? - iphone

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;
}

Related

Identify iphone's version

I'm developing an app which can only be installed in iPhone 4 and later versions.I also go through UIRequiredDeviceCapabilities and Device Compatibility Matrix.I need more solution.
Well, first of all you can receive the model as a string using the UIDevice class:
[[UIDevice currentDevice] model]
This will return something like "iPod touch" or "iPhone".
You can get the exact model platform using the following code:
(you have to #include <sys/types.h> and <sys/sysctl.h>)
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = #(machine); // Old syntax: [NSString stringWithCString:machine encoding:NSUTF8StringEncoding]
free(machine);
Now platform is a string containing the generation of the device, e.g.:
iPhone1,1 for the iPhone 2G (the first iPhone)
iPhone1,2 for the iPhone 3G
iPhone2,1 for the iPhone 3GS
iPhone3,1 or iPhone3,2 for the iPhone 4
iPhone4,1 for the iPhone 4S
iPhone5,1 or iPhone5,2 for the iPhone 5
Note that the number in front of the comma of the iPhone 4 platform is actually 3, not 4.
Using this string you can isolate this number and check if it is greater than or equal to 3:
if ([[[UIDevice currentDevice] model] isEqualToString:#"iPhone"]) {
if ([[[platform componentsSeparatedByString:#","][0] substringFromIndex:6] intValue] >= 3) {
// Device is iPhone 4 or newer
} else {
// Device is older than iPhone 4
}
}
However: You can actually check the screen's scale, since the iPhone 4 is the first iPhone with a retina display:
[[UIScreen mainScreen] scale] // Returns 2.0f on the iPhone 4 and newer and 1.0f on older devices

Detect iPhone 3G in iOS

Actually I can detect the iOS version but it is possible to detect a iPhone model 3G or 3GS inside iOS?
Thanks
You need to check for device capabilities. The 3g does not support video recorder and the 3gs does not support front facing camera.
Take a look to this question.
You can find the exact model string using this post (which I think is also a duplicate question):
#include <sys/types.h>
#include <sys/sysctl.h>
#implementation UIDevice (Hardware)
/*
Platforms
iPhone1,1 -> iPhone 1G
iPhone1,2 -> iPhone 3G
iPod1,1 -> iPod touch 1G
iPod2,1 -> iPod touch 2G
*/
-(NSString *) platform
{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);
return platform;
}

how to know is it iphone or ipad?

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.

iPhone 4 detection... on simulator

I need to detect if the user is using an iPhone 4, but I need this to work on the simulator (cause Apple forgot my country and there's no sign of iPhone 4 here soon).
I found this
http://www.clintharris.net/2009/iphone-model-via-sysctlbyname/
but running this on the simulator it does not detect the correct version. I mean, Xcode 3.2.3 has two simulators (3G/3GS and 4). I was expecting the detection method to tell me the correct version I am using... but instead it tells me "iphone simulator"...
Is there any way to do that?
thanks.
You don't need to detect the system version in your case.
Suppose an image is named foo.png, then you just need to add
foo~ipad.png for iPad
foo#2x~iphone.png for iPhone 4
and load the image with [UIImage imageNamed:#"foo.png"]. See the iPhone Application Programming Guide for detail.
I think it would be better to check for the feature you require, rather than a specific device. This is certainly what Apple recommends, as it gives you insurance when new devices roll around!
In this case, would it work to check the size of the screen, and use that to determine if you need to scale your image?
[[UIScreen mainScreen] bounds]
Hardware Availability and State
If a hardware feature (for example,
a gyroscope) is not available on a
device, calling a start method related
to that feature has no effect. You can
find out whether a hardware feature is
available or active by checking the
appropriate property; for example, for
gyroscope data, you can check the
value of the gyroAvailable or
gyroActive properties.
Use
#property(readonly, nonatomic, getter=isGyroAvailable) BOOL gyroAvailable
of class CMMotionManager.
See
#property (nonatomic, readonly, retain) NSString *systemVersion;
// It equal to #"4.0" on iOS 4.0
and
#property (nonatomic, readonly, retain) NSString *model;
// Possible examples of model strings are #”iPhone” and #”iPod touch”
of class UIDevice.
From Erica Sudan:
- (NSString *) platform
{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
/*
Possible values:
"iPhone1,1" = iPhone 1G
"iPhone1,2" = iPhone 3G
"iPhone2,1" = iPhone 3GS
"iPhone3,1" = iPhone 4
"iPod1,1" = iPod touch 1G
"iPod2,1" = iPod touch 2G
*/
NSString *platform = [NSString stringWithCString:machine];
free(machine);
return platform;
}
Or, if you just need to detect if it's a high res screen, you can use:
UIScreen *screen = [UIScreen mainScreen];
BOOL isHighRes;
if ([screen respondsToSelector:#selector(scale)]) {
isHighRes = ([screen scale] > 1);
} else {
isHighRes = NO;
}
Mike, you can know if the user is using an iPhone 4 by using preprocessor instructions. For example:
#ifdef __IPHONE_4_0
// Do some work for iPhone 4 device
#else
// Do some work for non iPhone 4 device
#endif
I hope it can help you.

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;