2

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.
}
animuson
  • 53,861
  • 28
  • 137
  • 147
Mark
  • 16,906
  • 20
  • 84
  • 117
  • This appears to be a duplicate of this question: http://stackoverflow.com/questions/688001/how-to-tell-if-your-code-is-running-on-an-iphone-or-an-iphone3g – Brad Larson Mar 15 '10 at 18:06

2 Answers2

7

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.

Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • So how would that work for the Telephone? I use tel:// to dial a number together with an icon that shows that the number can be called. I don't want this icon to show if the user can not make a call directly. Which is the case on the iPad and iPod Touch. – Mark Mar 16 '10 at 14:01
  • Thank you very much Vladimir! I increased the answer number and marked it as answer. – Mark Mar 29 '10 at 08:54
1

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.

Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172