Proper method to detect device model (iPhone/iPod Touch)?

六眼飞鱼酱① 提交于 2019-12-14 03:42:37

问题


Is this the proper way to detect which device a user is running?

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.
}

回答1:


It is not a general solution but Apple in many cases provides API calls to check wether specific feature is supported or not. Examples could be:

  • +isSourceTypeAvailable: and +availableMediaTypesForSourceType: in UIImagePickerController allowing you to check if camera is available for the current device.

  • +canSendMail in MFMailComposeViewController to check if device is configured to send mail.

  • -canOpenURL in UIApplication class to check if URL can be opened. For example it can be used to check if it is possible to make a phone call:

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

If such API calls are available for your purpose I would use them rather then rely on hardcoded string identifiers.




回答2:


I'm not sure I'd want to generalize that much (ie, there may eventually be an iPod with a camera, and I don't know that the iPhone will ALWAYS be called "iPhone"), but yes, this is the accepted way.



来源:https://stackoverflow.com/questions/2447539/proper-method-to-detect-device-model-iphone-ipod-touch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!