How to detect iOS device programmatically - iphone

I need to know which iOS device is currently running app (saying more exactly I need to know is device armv6 or armv7). UIUserInterfaceIdiomPad() could not check is device an iPhone4S or iPhone3G. Is it possible?

Download https://github.com/erica/uidevice-extension (UIDevice-Hardware class) and you can use these:
[UIDevice currentDevice] platformType] // returns UIDevice4GiPhone
[[UIDevice currentDevice] platformString] // returns #"iPhone 4G"
Or check if its retina
+ (BOOL) isRetina
{
if([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
return [[UIScreen mainScreen] scale] == 2.0 ? YES : NO;
return NO;
}
Or check iOs version
+ (BOOL) isIOS5
{
NSString *os5 = #"5.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
// currSysVer = #"5.0.1";
if ([currSysVer compare:os5 options:NSNumericSearch] == NSOrderedAscending) //lower than 4
{
return NO;
}
else if ([currSysVer compare:os5 options:NSNumericSearch] == NSOrderedDescending) //5.0.1 and above
{
return YES;
}
else // IOS 5
{
return YES;
}
return NO;
}

If you really want to know (at run time) if you are running on arm6 or arm7, you can use "NXGetArchInfoFromCPUType" (much more detail is available in the accepted answer to this question).
Otherwise you can use platformType or platformString, as our incredibly quick answering friend Omar suggested (and +1 to him!).

Related

How to check if an iOS device is a retina display iPod touch?

Is there a safe way to determine that a device is of a particular model? For example I must know if the device the user uses is a retina display iPod touch.
NSRange r = [[[UIDevice currentDevice] model] rangeOfString:#"iPod"];
float s = [[UIScreen mainScreen] scale];
if (r.location != NSNotFound && s > 1.5f) {
// retina iTouch
}
I would probably try something like this:
+(BOOL) isRetinaiPod
{
return [[[UIDevice currentDevice] model] isEqualToString:#"iPod touch"] && [UIScreen mainScreen].scale >= 2.0f;
}
However you can return the device's name with this:
+ (NSString *) deviceName
{
struct utsname u;
uname(&u);
return [NSString stringWithUTF8String:u.sysname];
}

How can we programmatically detect which iOS version is device running on? [duplicate]

This question already has answers here:
How to check iOS version?
(36 answers)
Closed 9 years ago.
I want to check if the user is running the app on iOS less than 5.0 and display a label in the app.
How do I detect which iOS is running on user's device programmatically?
Thanks!
Best current version, without need to deal with numeric search within NSString is to define macros (See original answer: Check iPhone iOS Version)
Those macros do exist in github, see: https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h
Like this:
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
and use them like this:
if (SYSTEM_VERSION_LESS_THAN(#"5.0")) {
// code here
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"6.0")) {
// code here
}
Outdated version below
to get OS version:
[[UIDevice currentDevice] systemVersion]
returns string, which can be turned into int/float via
-[NSString floatValue]
-[NSString intValue]
like this
Both values (floatValue, intValue) will be stripped due to its type, 5.0.1 will become 5.0 or 5 (float or int), for comparing precisely, you will have to separate it to array of INTs
check accepted answer here: Check iPhone iOS Version
NSString *ver = [[UIDevice currentDevice] systemVersion];
int ver_int = [ver intValue];
float ver_float = [ver floatValue];
and compare like this
NSLog(#"System Version is %#",[[UIDevice currentDevice] systemVersion]);
NSString *ver = [[UIDevice currentDevice] systemVersion];
float ver_float = [ver floatValue];
if (ver_float < 5.0) return false;
For Swift 4.0 syntax
below example is just checking if the device is of iOS11 or greater version.
let systemVersion = UIDevice.current.systemVersion
if systemVersion.cgFloatValue >= 11.0 {
//"for ios 11"
}
else{
//"ios below 11")
}
Update
From iOS 8 we can use the new isOperatingSystemAtLeastVersion method on NSProcessInfo
NSOperatingSystemVersion ios8_0_1 = (NSOperatingSystemVersion){8, 0, 1};
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:ios8_0_1]) {
// iOS 8.0.1 and above logic
} else {
// iOS 8.0.0 and below logic
}
Beware that this will crash on iOS 7, as the API didn't exist prior to iOS 8. If you're supporting iOS 7 and below, you can safely perform the check with
if ([NSProcessInfo instancesRespondToSelector:#selector(isOperatingSystemAtLeastVersion:)]) {
// conditionally check for any version >= iOS 8 using 'isOperatingSystemAtLeastVersion'
} else {
// we're on iOS 7 or below
}
Original answer iOS < 8
For the sake of completeness, here's an alternative approach proposed by Apple itself in the iOS 7 UI Transition Guide, which involves checking the Foundation Framework version.
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// Load resources for iOS 6.1 or earlier
} else {
// Load resources for iOS 7 or later
}
I know I am too late to answer this question. I am not sure does my method still working on low iOS versions (< 5.0):
NSString *platform = [UIDevice currentDevice].model;
NSLog(#"[UIDevice currentDevice].model: %#",platform);
NSLog(#"[UIDevice currentDevice].description: %#",[UIDevice currentDevice].description);
NSLog(#"[UIDevice currentDevice].localizedModel: %#",[UIDevice currentDevice].localizedModel);
NSLog(#"[UIDevice currentDevice].name: %#",[UIDevice currentDevice].name);
NSLog(#"[UIDevice currentDevice].systemVersion: %#",[UIDevice currentDevice].systemVersion);
NSLog(#"[UIDevice currentDevice].systemName: %#",[UIDevice currentDevice].systemName);
You can get these results:
[UIDevice currentDevice].model: iPhone
[UIDevice currentDevice].description: <UIDevice: 0x1cd75c70>
[UIDevice currentDevice].localizedModel: iPhone
[UIDevice currentDevice].name: Someones-iPhone002
[UIDevice currentDevice].systemVersion: 6.1.3
[UIDevice currentDevice].systemName: iPhone OS
[[UIDevice currentDevice] systemVersion]
[[UIDevice currentDevice] systemVersion];
or check the version like
You can get the below Macros from here.
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(IOS_VERSION_3_2_0))
{
UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cs_lines_back.png"]] autorelease];
theTableView.backgroundView = background;
}
Hope this helps
[[[UIDevice currentDevice] systemVersion] floatValue]
Marek Sebera's is great most of the time, but if you're like me and find that you need to check the iOS version frequently, you don't want to constantly run a macro in memory because you'll experience a very slight slowdown, especially on older devices.
Instead, you want to compute the iOS version as a float once and store it somewhere. In my case, I have a GlobalVariables singleton class that I use to check the iOS version in my code using code like this:
if ([GlobalVariables sharedVariables].iOSVersion >= 6.0f) {
// do something if iOS is 6.0 or greater
}
To enable this functionality in your app, use this code (for iOS 5+ using ARC):
GlobalVariables.h:
#interface GlobalVariables : NSObject
#property (nonatomic) CGFloat iOSVersion;
+ (GlobalVariables *)sharedVariables;
#end
GlobalVariables.m:
#implementation GlobalVariables
#synthesize iOSVersion;
+ (GlobalVariables *)sharedVariables {
// set up the global variables as a static object
static GlobalVariables *globalVariables = nil;
// check if global variables exist
if (globalVariables == nil) {
// if no, create the global variables class
globalVariables = [[GlobalVariables alloc] init];
// get system version
NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
// separate system version by periods
NSArray *systemVersionComponents = [systemVersion componentsSeparatedByString:#"."];
// set ios version
globalVariables.iOSVersion = [[NSString stringWithFormat:#"%01d.%02d%02d", \
systemVersionComponents.count < 1 ? 0 : \
[[systemVersionComponents objectAtIndex:0] integerValue], \
systemVersionComponents.count < 2 ? 0 : \
[[systemVersionComponents objectAtIndex:1] integerValue], \
systemVersionComponents.count < 3 ? 0 : \
[[systemVersionComponents objectAtIndex:2] integerValue] \
] floatValue];
}
// return singleton instance
return globalVariables;
}
#end
Now you're able to easily check the iOS version without running macros constantly. Note in particular how I converted the [[UIDevice currentDevice] systemVersion] NSString to a CGFloat that is constantly accessible without using any of the improper methods many have already pointed out on this page. My approach assumes the version string is in the format n.nn.nn (allowing for later bits to be missing) and works for iOS5+. In testing, this approach runs much faster than constantly running the macro.
Hope this helps anyone experiencing the issue I had!
In MonoTouch:
To get the Major version use:
UIDevice.CurrentDevice.SystemVersion.Split('.')[0]
For minor version use:
UIDevice.CurrentDevice.SystemVersion.Split('.')[1]
To get more specific version number information with major and minor versions separated:
NSString* versionString = [UIDevice currentDevice].systemVersion;
NSArray* vN = [versionString componentsSeparatedByString:#"."];
The array vN will contain the major and minor versions as strings, but if you want to do comparisons, version numbers should be stored as numbers (ints). You can add this code to store them in the C-array* versionNumbers:
int versionNumbers[vN.count];
for (int i = 0; i < sizeof(versionNumbers)/sizeof(versionNumbers[0]); i++)
versionNumbers[i] = [[vN objectAtIndex:i] integerValue];
* C-arrays used here for more concise syntax.
A simple check for iOS version less than 5 (all versions):
if([[[UIDevice currentDevice] systemVersion] integerValue] < 5){
// do something
};

How can I get UI_USER_INTERFACE_IDIOM

How can I get UI_USER_INTERFACE_IDIOM, I am not clear that how does it work, can anyone please guide me?
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// iPad device
} else {
// iPhone / iPod touch device
}
From the Apple Header files:
/* 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. */
#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:#selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)

ipad 2 camera support detection

I have an app that uses the following Macro:
#define IS_IPAD ([[UIDevice currentDevice] respondsToSelector:#selector(userInterfaceIdiom)] && [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
which works very well for me.
However, I was using it to turn off the option of taking a photo in the app.
How can I detect if the camera option is available regardless of device?
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
That should work
See How to Detect Camera Existence with AVFoundation.
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = nil;
if ( [videoDevices count] > 0 ) // This device has one or more cameras
....

How do I determine whether the current device is an iPhone or iPad?

In my universal app I need to check if the current device is an iPad or iPhone. How can I do this programmatically? I plan to put the code in my viewDidLoad.
check if UISplitViewController class available on the platform, if so make sure it is iPad using Apple's macro (notice that UIUserInterfaceIdiomPad constant is available only on iOS 3.2 and up).
if (NSClassFromString(#"UISplitViewController") != nil && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//currentDeviceType = iPad;
}
else {
//currentDeviceType = iPhone;
}
Proper method to detect device model (iPhone/iPod Touch)?
I use this simple function in all my apps:
#import "isPad.h"
BOOL isPad () {
static BOOL isPad;
static BOOL used = NO;
if (used)
return isPad;
used = YES;
NSRange range = [[[UIDevice currentDevice] model] rangeOfString:#"iPad"];
isPad = range.location != NSNotFound;
return isPad;
}
try this.. this will help you.
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:#"iPod touch"]||[deviceType isEqualToString:#"iPhone"]||[deviceType isEqualToString:#"iPad"]){
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// Statements
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
// Statements
}