这是检测用户正在运行哪个设备的正确方法吗?

NSString *currentModel = [[UIDevice currentDevice] model];
if ([currentModel isEqualToString:@"iPhone"]) {
    // The user is running on iPhone so allow Call, Camera, etc.
} else {
    // The user is running on a different device (iPod / iPad / iPhone Simulator) disallow Call.
}
有帮助吗?

解决方案

这不是一个通用的解决方案,但 Apple 在许多情况下提供 API 调用来检查是否支持特定功能。例子可以是:

  • +isSourceTypeAvailable:+availableMediaTypesForSourceType:UIImagePickerController 允许您检查当前设备是否可以使用相机。

  • +canSendMailMFMailComposeViewController 检查设备是否配置为发送邮件。

  • -canOpenURLUIApplication 类来检查 URL 是否可以打开。例如,它可用于检查是否可以拨打电话:

    if (![[UIApplication sharedApplication] canOpenURL:
                                     [NSURL URLWithString:@"tel://"]])
        //We cannot make a call - hide call button here
    

如果此类 API 调用可用于您的目的,我会使用它们,而不是依赖于硬编码的字符串标识符。

其他提示

我不知道我要笼统地说很多(即有可能最终是一个iPod有摄像头,我不知道iPhone将永远被称为“iPhone”),惟命是从,这是被接受的方法。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top