在了解如何检测用户设备后:iOS - How to get device make and model?
我制作了一个快速测试应用程序以根据设备显示警报。我没有从这段代码中得到任何警告。我在这里错过了什么/做错了什么?
#import "ViewController.h"
#import <sys/utsname.h>
NSString* machineName()
{
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
}
@interface ViewController ()
@end
- (IBAction)btn:(id)sender {
if ([machineName() isEqualToString:@"iPhone5,1"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Checking device iphone5" delegate:nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];
[alert show];
} else if ([machineName() isEqualToString:@"iPhone4,1"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Checking device iphone4" delegate:nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];
[alert show];
}
}
最佳答案
struct utsname
的machine
字段为机器硬件平台,如果程序在iOS模拟器中运行,则为"x86_64"
.
只有在真实设备上,您才会得到像 "iPhone5,1"
这样的字符串。
关于ios - 获取设备类型/型号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14716754/