2013年7月18日 星期四

iOS 取得裝置平台類型或版本名稱的方法

step1.
有關的標頭檔

#import <sys/types.h>
#import <sys/sysctl.h>

step2.
取使用的機台副程式

- (NSString *) deviceModel {
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *model = malloc(size);
    sysctlbyname("hw.machine", model, &size, NULL, 0);
    NSString *deviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
    free(model);
    
    return deviceModel;
}

step3.
使用

NSString *platform = [self deviceModel];

NSString *iosVersion = [[UIDevice currentDevice] systemVersion];
NSString *appVersion = [[[NSBundle mainBundle] infoDictionary]objectForKey:@"CFBundleShortVersionString"];


NSString *content = [NSString stringWithFormat:@"\n\nApp版本: %@, iOS版本: %@, 硬體版本: %@",appVersion, iosVersion, platform];



app版本可以到這裡來修改 如下圖






機器版本      市售的商品名稱
 
i386         iOS 模擬器(i386)
x86_64       iOS 模擬器(x86_64)
 
iPhone1,1    iPhone 1G
iPhone1,2    iPhone 3G
iPhone2,1    iPhone 3Gs
iPhone3,1    iPhone 4
iPhone4,1    iPhone 4s
iPhone5,1    iPhone 5
 
iPod1,1      iPod touch 1
iPod2,1      iPod touch 2
iPod3,1      iPod touch 3
iPod4,1      iPod touch 4
 
iPad1,1      iPad
iPad2,1      iPad 2
=====================================================

辨別是否為iphne or ipad

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    //判斷為 iPhone 介面裝置
}
 
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    //判斷為 iPad 介面裝置
}

=====================================================

辨別是否為模擬器 或是實體機器

#if TARGET_IPHONE_SIMULATOR
 
    NSLog(@"使用為模擬器執行");
 
#endif


若要判斷實體裝置請使用「!TARGET_IPHONE_SIMULATOR」或是「#else」敘述,切勿使用 TARGET_OS_IPHONE(用來判斷是否是 iOS 應用程式)。
最後,在 UIDevice 方面,則可以透過 modle 所回傳的字串來判斷是實體裝置或是模擬器。

[[UIDevice currentDevice] model]



參考文獻:http://furnacedigital.blogspot.tw/2011/02/blog-post_14.html





0 意見:

張貼留言