繁体   English   中英

无法识别xcode 7 beta版中的iPhone和iOS模拟器之间的区别?

[英]Can't identify the difference between iPhone and iOS simulator in xcode 7 beta version?

我正在使用以下代码从应用程序拨打电话。在xcode 5中,它工作正常。 但是在xcode 7 beta版中,我在iOS Simulator上运行我的应用,并且if ([[device model] isEqualToString:@"iPhone"] )对于模拟器来说是正确的。 怎么样 ?

 if ([[device model] isEqualToString:@"iPhone"] )
 {  
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:130-032-2837"]]];
 }
 else
 {
      UIAlertView *notPermitted=[[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device doesn't support this feature." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
      [notPermitted show];
 }

它不能解决您无法检测到模拟器的事实,但是我非常确定,如果您询问它是否可以以前打开该URL,在模拟器上它将返回否。

- canOpenURL:

文档在这里

设备或模拟器定义为基于所选目标的构建时间。 这是您应该通过在UIDevice上添加类别来做到这一点的方法:

@implementation UIDevice (Addition)


- (BOOL)isSimulator {
#if TARGET_IPHONE_SIMULATOR
    return true;
#else
    return false;
#endif  
}


+ (BOOL)isSimulator {
    return [[self currentDevice] isSimulator];
}

然后只需调用:

[UIDevice isSimulator]

如果您只想检查您是否正在模拟器中或真实设备上运行,我强烈建议您使用TARGET_OS_SIMULATOR define(包含在SDK

像这样使用它:

#if !(TARGET_IPHONE_SIMULATOR)
//use case for iPhone
#else
//use case for simulator
#endif

我在Xcode 7iOS9 SDK当前项目中使用它

编辑:正如trojanfoe指出的,这是编译时检查,而不是运行时检查。

如果要对模拟器进行运行时特定检查,则可以使用以下命令:

    if ([[[UIDevice currentDevice].model lowercaseString] rangeOfString:@"simulator"].location != NSNotFound) {
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM