Identify iphone's version - iphone

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

Related

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

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

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;

Detect the specific iPhone/iPod touch model [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Determine device (iPhone, iPod Touch) with iOS
I am making a game that utilizes the peer-to-peer bluetooth capabilities of the iPhone (and probably the iPod touch 2nd generation). However, to stop the users from trying to play a multiplayer on an iPod 1st gen and iPhone 2G I need to check for the specific device model.
[[UIDevice currentDevice] model] will only tell me if the device is an "iPhone" or an "iPod touch". Is there a way to check for the specific device model, like: "iPhone 3GS", "iPod touch 1st generation" or something.
EDIT:
There is a category to UIDevice (I think it's created by Erica Sadun, I don't take credit for it) that uses the following code to get the specific device model. You can find the whole category here along with other useful stuff: https://github.com/erica/uidevice-extension
#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;
}
This works and apps using this have been lately approved in the AppStore.
You can get the device model number using uname from sys/utsname.h. For example:
#import <sys/utsname.h>
NSString*
machineName()
{
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
}
The result should be:
#"i386" on the simulator
#"iPod1,1" on iPod Touch
#"iPod2,1" on iPod Touch Second Generation
#"iPod3,1" on iPod Touch Third Generation
#"iPod4,1" on iPod Touch Fourth Generation
#"iPhone1,1" on iPhone
#"iPhone1,2" on iPhone 3G
#"iPhone2,1" on iPhone 3GS
#"iPad1,1" on iPad
#"iPad2,1" on iPad 2
#"iPad3,1" on iPad 3 (aka new iPad)
#"iPhone3,1" on iPhone 4
#"iPhone4,1" on iPhone 4S
#"iPhone5,1" on iPhone 5
#"iPhone5,2" on iPhone 5
Most complete UIDevice (Hardware) category probably is http://github.com/erica/uidevice-extension/ (by Erica Sadun):
[[UIDevice currentDevice] platformType] // ex: UIDevice4GiPhone
[[UIDevice currentDevice] platformString] // ex: #"iPhone 4G"
How about this code, if new version was released, you will identifier with the last know device
#include <sys/types.h>
#include <sys/sysctl.h>
- (NSString *)getModel {
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *model = malloc(size);
sysctlbyname("hw.machine", model, &size, NULL, 0);
NSString *sDeviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
free(model);
if ([sDeviceModel isEqual:#"i386"]) return #"Simulator"; //iPhone Simulator
if ([sDeviceModel isEqual:#"iPhone1,1"]) return #"iPhone1G"; //iPhone 1G
if ([sDeviceModel isEqual:#"iPhone1,2"]) return #"iPhone3G"; //iPhone 3G
if ([sDeviceModel isEqual:#"iPhone2,1"]) return #"iPhone3GS"; //iPhone 3GS
if ([sDeviceModel isEqual:#"iPhone3,1"]) return #"iPhone4 AT&T"; //iPhone 4 - AT&T
if ([sDeviceModel isEqual:#"iPhone3,2"]) return #"iPhone4 Other"; //iPhone 4 - Other carrier
if ([sDeviceModel isEqual:#"iPhone3,3"]) return #"iPhone4"; //iPhone 4 - Other carrier
if ([sDeviceModel isEqual:#"iPhone4,1"]) return #"iPhone4S"; //iPhone 4S
if ([sDeviceModel isEqual:#"iPhone5,1"]) return #"iPhone5"; //iPhone 5 (GSM)
if ([sDeviceModel isEqual:#"iPod1,1"]) return #"iPod1stGen"; //iPod Touch 1G
if ([sDeviceModel isEqual:#"iPod2,1"]) return #"iPod2ndGen"; //iPod Touch 2G
if ([sDeviceModel isEqual:#"iPod3,1"]) return #"iPod3rdGen"; //iPod Touch 3G
if ([sDeviceModel isEqual:#"iPod4,1"]) return #"iPod4thGen"; //iPod Touch 4G
if ([sDeviceModel isEqual:#"iPad1,1"]) return #"iPadWiFi"; //iPad Wifi
if ([sDeviceModel isEqual:#"iPad1,2"]) return #"iPad3G"; //iPad 3G
if ([sDeviceModel isEqual:#"iPad2,1"]) return #"iPad2"; //iPad 2 (WiFi)
if ([sDeviceModel isEqual:#"iPad2,2"]) return #"iPad2"; //iPad 2 (GSM)
if ([sDeviceModel isEqual:#"iPad2,3"]) return #"iPad2"; //iPad 2 (CDMA)
NSString *aux = [[sDeviceModel componentsSeparatedByString:#","] objectAtIndex:0];
//If a newer version exist
if ([aux rangeOfString:#"iPhone"].location!=NSNotFound) {
int version = [[aux stringByReplacingOccurrencesOfString:#"iPhone" withString:#""] intValue];
if (version == 3) return #"iPhone4"
if (version >= 4) return #"iPhone4s";
}
if ([aux rangeOfString:#"iPod"].location!=NSNotFound) {
int version = [[aux stringByReplacingOccurrencesOfString:#"iPod" withString:#""] intValue];
if (version >=4) return #"iPod4thGen";
}
if ([aux rangeOfString:#"iPad"].location!=NSNotFound) {
int version = [[aux stringByReplacingOccurrencesOfString:#"iPad" withString:#""] intValue];
if (version ==1) return #"iPad3G";
if (version >=2) return #"iPad2";
}
//If none was found, send the original string
return sDeviceModel;
}
BOOL hasHighResScreen = NO;
if ([UIScreen instancesRespondToSelector:#selector(scale)]) {
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale > 1.0) {
hasHighResScreen = YES;
}
}
iPhone 4 is iPhone3,1 and iPhone3,2
iPhone 4S is iPhone4,1
iPad 2 is iPad2,1 iPad2,2 and iPad2,3 depending on version (GSM etc)
iPad 3 is iPad3,1 iPad3,2 and iPad3,3 depending on version (GSM etc)
See Iphone secrets (scroll down to "internal product codes")
Another good source is:
everyiphone.com
NSString* valueDevice = [[UIDevice currentDevice] model];
and then check if the string is equal to whatever device you are looking for like :
if(value==#"iPod1,1" )
{}
and you should be good to go