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;
}
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'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
I am having a memory leak in the code below but don't really know how to fix it. I tried making machine = nil; but that didn't work. Anyone have any suggestions? Thanks in advance.
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];
if ([platform isEqualToString:#"iPhone3,1"] || [platform isEqualToString:#"iPhone3,2"] || [platform isEqualToString:#"iPhone3,3"] || [platform isEqualToString:#"iPhone4,1"])
[UINavigationBar appearance].barTintColor = [UIColor colorWithRed:255.0/255.0 green:220.0/255.0 blue:0.0/255.0 alpha:0.9];
machine = nil;
I get the leak in stringWithCSString:machine: potential leak of memory pointed to by 'machine'.
Calls to malloc need to be paired with calls to free.
Change machine = nil to free(machine).
BTW - your code be made a lot easier:
#include <sys/utsname.h>
struct utsname si;
uname(&si);
if (strcmp(si.machine, "iPhone3,1") == 0 || strcmp(si.machine, "iPhone3,2") == 0 || strcmp(si.machine, "iPhone3,3") == 0 || strcmp(si.machine, "iPhone4,1") == 0) {
}
No need for the memory management or creating NSString objects.
What are all the possible values returned by [[UIDevice currentDevice] model];? It isn't documented.
The possible vales are iPod touch, iPhone, iPhone Simulator, iPad, iPad Simulator
If you want to know which hardware iOS is ruining on like iPhone3, iPhone4, iPhone5 etc below is the code for that
NOTE: The below code may not contain all device's string, I'm with other guys are maintaining the same code on GitHub so please take the latest code from there
Objective-C : GitHub/DeviceUtil
Swift : GitHub/DeviceGuru
#include <sys/types.h>
#include <sys/sysctl.h>
- (NSString*)hardwareDescription {
NSString *hardware = [self hardwareString];
if ([hardware isEqualToString:#"iPhone1,1"]) return #"iPhone 2G";
if ([hardware isEqualToString:#"iPhone1,2"]) return #"iPhone 3G";
if ([hardware isEqualToString:#"iPhone3,1"]) return #"iPhone 4";
if ([hardware isEqualToString:#"iPhone4,1"]) return #"iPhone 4S";
if ([hardware isEqualToString:#"iPhone5,1"]) return #"iPhone 5";
if ([hardware isEqualToString:#"iPod1,1"]) return #"iPodTouch 1G";
if ([hardware isEqualToString:#"iPod2,1"]) return #"iPodTouch 2G";
if ([hardware isEqualToString:#"iPad1,1"]) return #"iPad";
if ([hardware isEqualToString:#"iPad2,6"]) return #"iPad Mini";
if ([hardware isEqualToString:#"iPad4,1"]) return #"iPad Air WIFI";
//there are lots of other strings too, checkout the github repo
//link is given at the top of this answer
if ([hardware isEqualToString:#"i386"]) return #"Simulator";
if ([hardware isEqualToString:#"x86_64"]) return #"Simulator";
return nil;
}
- (NSString*)hardwareString {
size_t size = 100;
char *hw_machine = malloc(size);
int name[] = {CTL_HW,HW_MACHINE};
sysctl(name, 2, hw_machine, &size, NULL, 0);
NSString *hardware = [NSString stringWithUTF8String:hw_machine];
free(hw_machine);
return hardware;
}
I just did a test on iPod Touch, iPhone, Phone Retina, iPhone 5, iPad, iPad Retina and iPad Mini. So this is my conclusion:
iPod touch
iPhone
iPad
On simulators - this could be useful if you're a developer working on features that sometimes do not work at all on simulators - you'll get these values:
iPhone Simulator
iPad Simulator
I believe the best answer to explain(something which wasn't written here)
Is to say that the value itself is a String value.
and the possible answers are string e.g: "iPhone","iPad" and etc..
None of these answers are extendable for new model numbers. Here is an enumeration:
public enum DeviceType {
case iPad(String?)
case iPhone(String?)
case simulator(String?)
case appleTV(String?)
case unknown
}
And Extension I wrote that I think is a little cleaner and a little more extendable for when new model number come out.
extension UIDevice {
public static func getDevice() -> DeviceType {
var info = utsname()
uname(&info)
let machineMirror = Mirror(reflecting: info.machine)
let code = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8, value != 0 else {
return identifier
}
return identifier + String(UnicodeScalar(UInt8(value)))
}
if code.lowercased().range(of: "ipad") != nil {
if let range = code.lowercased().range(of: "ipad") {
var mutate = code
mutate.removeSubrange(range)
return .iPad(mutate)
}else{
return .iPad(nil)
}
}else if code.lowercased().range(of: "iphone") != nil {
if let range = code.lowercased().range(of: "iphone") {
var mutate = code
mutate.removeSubrange(range)
return .iPhone(mutate)
}else{
return .iPhone(nil)
}
}else if code.lowercased().range(of: "i386") != nil || code.lowercased().range(of: "x86_64") != nil{
return .simulator(code)
}else if code.lowercased().range(of: "appletv") != nil {
if let range = code.lowercased().range(of: "appletv") {
var mutate = code
mutate.removeSubrange(range)
return .appleTV(mutate)
}else{
return .appleTV(nil)
}
}else{
return .unknown
}
}
}