I'm a starter in obj-c programming and I need to know how to display device info ( name, device type, ios version )
I you know the answer, please tell me and keep in mind i'm a starter with xcode ;)
I've used these information in an App that I developed so I did the following code. I think this may help you. I just didn't understand what you mean with device type.
To get the device model:
// get model from UIDevice
NSString *modelDevice = [UIDevice currentDevice].model;
To get the iOS Version:
//get the iOS version
NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
To get the device name:
/** Method responsible to get the device name
*
* #return device Name
*/
+ (NSString *)deviceName
{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithUTF8String:machine];
free(machine);
return platform;
}
You may try something like this: I use this for in-app support emailing from users.
#import <sys/utsname.h>
- (void)yourMethod
{
struct utsname systemInfo;
uname(&systemInfo);
NSString *appVersion = [NSBundle mainBundle].infoDictionary[#"CFBundleVersion"];
NSString *osVersion = [[UIDevice currentDevice] systemVersion];
NSString *machine = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}
NSLog(#"uniqueIdentifier: %#", [[UIDevice currentDevice] uniqueIdentifier]);
NSLog(#"name: %#", [[UIDevice currentDevice] name]);
NSLog(#"systemName: %#", [[UIDevice currentDevice] systemName]);
NSLog(#"systemVersion: %#", [[UIDevice currentDevice] systemVersion]);
NSLog(#"model: %#", [[UIDevice currentDevice] model]);
NSLog(#"localizedModel: %#", [[UIDevice currentDevice] localizedModel])
;
Please refer to UIDevice Class. It has all accessible system information properties. This is a singleton class. You can access this class instance like this : [UIDevice currentDevice]
For example if you want to access device model, you can access like this :
[UIDevice currentDevice]. model
Please refer this link to get information about all properties : https://developer.apple.com/library/ios/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html
Related
Is there a way to programmatically detect iOS version's build number,
For example iOS version 9.0.2(13A452).
I know how to detect iOS version eg: 9.0.2.
Is there any API to detect 13A452?
Thanks in advance.
Figured I'd put the code up incase anyone needs it (works at run time), since it's slightly tricky to process!
MAKE SURE YOU #include <sys/sysctl.h>
//make sure you `#include <sys/sysctl.h>`
NSString *ctlKey = #"kern.osversion";
BOOL buildValueFound;
NSString *buildValue;
size_t size = 0;
if (sysctlbyname([ctlKey UTF8String], NULL, &size, NULL, 0) == -1) {
buildValueFound = NO;
} else {
char *machine = calloc( 1, size );
sysctlbyname([ctlKey UTF8String], machine, &size, NULL, 0);
NSString *ctlValue = [NSString stringWithCString:machine encoding:[NSString defaultCStringEncoding]];
free(machine);
buildValue = ctlValue;
buildValueFound = YES;
}
if (buildValueFound) {
NSLog(#"%#", buildValue);
} else {
NSLog(#"No build value found");
}
I have figured this out. Using KERN_OSVERSION on function sysctl returns iOS build number.
I have read the answer to this question, Identify new iPhone model on xcode (5, 5c, 5s) and would like to add this as an NSString to my MFMailCompose. I have tried using this method but am not having any luck. Someone please help me.
Sorry I am new to Xcode.
NSString *iOS = [[UIDevice currentDevice] systemVersion];
NSString *model = [[UIDevice currentDevice] model];
NSString *appVersionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleShortVersionString"];
NSString *name = [[UIDevice currentDevice] name];
NSString *appID=#" ";
NSString *appStoreURL =[NSString stringWithFormat:#"https://itunes.apple.com/us/app/scanmarks/id926114469?ls=1&mt=8", appID];
NSString *body = [NSString stringWithFormat:#"I need help with\n\n\n----------\n Name: %#\nDevice: %# (%#)\n Scanmarks Version: %#\n",name,model,iOS,appVersionString];
It currently shows:
I would like it to show the iPhone Device model like Tweetbot does
I have added the suggested and now have this:
Using the link you you put in your question, if you just want it to say the iPhone platform you would just use this portion of their answer :
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);
That will translate to iPhone7,2 etc. just pass the NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding]; string 'platform' into your message body
If you want to convert that to whatever string you want that's when the other portion comes into play. This is the only time you'll use the other part and all of the conversions in their answer:
NSString *convertPlatformToString = [self platformType:platform]);
Example for putting in mail message:
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);
NSString *body = [NSString stringWithFormat:#"Platform is %#", platform];
Harvey,
I built an open source tool that does exactly what you want. Feel free to use it or browse through the code to see how I accomplished what you're asking.
https://github.com/michaelpatzer/MPFeedbackMailComposeViewController
I am a new developer in iOS Developing. I faced a problem on current device. According to requirement i have to design app for all device. So anybody can help me how to get Current device & current platform of iOS?
For current device i am using this code:
NSString *deviceType = [[UIDevice currentDevice] model];
My requirement is i have to adjust UITableView row height according to device.
I think you got my question. If you need any information please tell me.
Thanks in advanced.
Ok try this one.
NSString *deviceType = [[UIDevice currentDevice] model]; // for current device
if ([deviceType isEqualToString:#"iPhone"])
{
NSString *platform = [self platformRawString];
NSLog(#"platform :%#",platform);
// To set the half cut in the last cell.
if ([platform isEqualToString:#"iPhone2,1"])
//cellHeight = 39.5;
else if ([platform isEqualToString:#"iPhone4,1"])
//cellHeight = 42;
else if ([platform isEqualToString:#"iPhone5,3"] || [platform isEqualToString:#"iPhone5,4"])
//cellHeight = 42.5;
else
//cellHeight = 40.0;
}
Method for getting platform
- (NSString *)platformRawString
{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithUTF8String:machine];
free(machine);
return platform;
}
I am trying to activate APNS. Because uniqueIdentifier is deprecated, I'm trying to use CFUUIDCreate with the following code:
UIDevice *dev = [UIDevice currentDevice];
NSString *deviceUuid;
if ([dev respondsToSelector:#selector(uniqueIdentifier)])
deviceUuid = dev.uniqueIdentifier;
else {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
id uuid = [defaults objectForKey:#"deviceUuid"];
if (uuid)
deviceUuid = (NSString *)uuid;
else {
CFStringRef cfUuid = CFUUIDCreateString(NULL, CFUUIDCreate(NULL));
deviceUuid = (__bridge NSString *)cfUuid;
CFRelease(cfUuid);
[defaults setObject:deviceUuid forKey:#"deviceUuid"];
}
}
How can I replace uniqueIdentifier with CFUUIDCreate?
iOS < 6.0
// Create universally unique identifier (object)
CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
// Get the string representation of CFUUID object.
NSString *uuidStr = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);
CFRelease(uuidObject);
iOS >= 6.0
NSString* UDID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
I use it to check iOS version, but it doesn't work:
#ifndef kCFCoreFoundationVersionNumber_iPhoneOS_5_0
#define kCFCoreFoundationVersionNumber_iPhoneOS_5_0 675.000000
#endif
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0
#define IF_IOS5_OR_GREATER(...) \
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_5_0) \
{ \
__VA_ARGS__ \
}
#else
#define IF_IOS5_OR_GREATER 0
#endif
when I make
#if IF_IOS5_OR_GREATER
NSLog(#"iOS5");
#endif
nothing happens. Is something wrong here?
Much simpler:
#define IS_IOS6_AND_UP ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
#ifdef __IPHONE_5_0
etc
Just look for that constant. All the objective c constants start with two underscores
You've defined a macro, but you're using it in the non-macro way. Try something like this, with your same macro definition.
IF_IOS5_OR_GREATER(NSLog(#"iOS5");)
(This is instead of your #if/#endif block.)
Define this method:
+(BOOL)iOS_5 {
NSString *osVersion = #"5.0";
NSString *currOsVersion = [[UIDevice currentDevice] systemVersion];
return [currOsVersion compare:osVersion options:NSNumericSearch] == NSOrderedAscending;
}
Then define the macro as that method.
For a runtime check use something like this:
- (BOOL)iOSVersionIsAtLeast:(NSString*)version {
NSComparisonResult result = [[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch];
return (result == NSOrderedDescending || result == NSOrderedSame);
}
If you create a category on UIDevice for it, you can use it as such:
#implementation UIDevice (OSVersion)
- (BOOL)iOSVersionIsAtLeast:(NSString*)version {
NSComparisonResult result = [[self systemVersion] compare:version options:NSNumericSearch];
return (result == NSOrderedDescending || result == NSOrderedSame);
}
#end
...
if([[UIDevice currentDevice] iOSVersionIsAtLeast:#"6.0"]) self.navigationBar.shadowImage = [UIImage new];
#define isIOS7 ([[[UIDevice currentDevice]systemVersion]floatValue] > 6.9) ?1 :0