how to know is it iphone or ipad?

后端 未结 5 831
北荒
北荒 2021-01-06 07:35

i want to know the user uses the iphone or ipad,if the user uses the iphone i want to open the camera,if he uses the ipad or runs in simulator i want to open the library. ho

相关标签:
5条回答
  • 2021-01-06 07:36

    Make use of this to identify devices.

    // If iPhoneOS is 3.2 or greater then __IPHONE_3_2 will be defined
    #ifndef __IPHONE_3_2    
    
    typedef enum {
        UIUserInterfaceIdiomPhone,           // iPhone and iPod touch
        UIUserInterfaceIdiomPad,             // iPad
    } UIUserInterfaceIdiom;
    
    #define UI_USER_INTERFACE_IDIOM() UIUserInterfaceIdiomPhone
    
    #endif // ifndef __IPHONE_3_2
    

    but if you want to check if camera is available I think you can make use of UIImagePickerController's static method

    + (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType
    
    0 讨论(0)
  • 2021-01-06 07:45
    NSString *deviceType = [UIDevice currentDevice].model;
    
    if([deviceType isEqualToString:@"iPhone"])
    {
         //your code
    }
    .....
    

    Hope this helps.

    EDIT:

    See this thread -determine-device-iphone-ipod-touch-with-iphone-sdk .

    0 讨论(0)
  • 2021-01-06 07:47
    [[UIDevice currentDevice].model hasPrefix:@"iPhone"]
    

    Use the "hasPrefix" so that it works in simulator.

    0 讨论(0)
  • 2021-01-06 07:49

    You should not determine whether there is a camera by looking at the model. This is not future proof - for instance, you would not be supporting the iPad 2's camera.

    UIImagePickerController has a special method to determine whether a camera in available:

    + (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType
    

    With sourceType being one of

    UIImagePickerControllerSourceTypePhotoLibrary,
    UIImagePickerControllerSourceTypeCamera,
    UIImagePickerControllerSourceTypeSavedPhotosAlbum
    
    0 讨论(0)
  • 2021-01-06 07:51

    Working on Vaibhav Tekam's answer, I used this

    NSString *deviceType = [UIDevice currentDevice].model;
    
    
    if([deviceType hasPrefix:@"iPhone"])
    {
         //your code
    }
    

    or

     NSString *deviceType = [UIDevice currentDevice].model;
    
    if([deviceType hasPrefix:@"iPad"])
    {
         //your code
    }
    

    etc. It's much easier that way as it covers all models.

    0 讨论(0)
提交回复
热议问题