How to detect iPhone 5 (widescreen devices)?

前端 未结 24 1274
我寻月下人不归
我寻月下人不归 2020-11-22 00:50

I\'ve just upgraded to XCode 4.5 GM and found out that you can now apply the \'4\" Retina\' size to your view controller in the storyboard.

Now if I want to create a

24条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 01:08

    If the project is created using Xcode 6, then use the below mentioned code to detect the devices..

    printf("\nDetected Resolution : %d x %d\n\n",(int)[[UIScreen mainScreen] nativeBounds].size.width,(int)[[UIScreen mainScreen] nativeBounds].size.height);
    
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
        {
            if([[UIScreen mainScreen] nativeBounds].size.height == 960 || [[UIScreen mainScreen] nativeBounds].size.height == 480){
                printf("Device Type : iPhone 4,4s ");
    
            }else if([[UIScreen mainScreen] nativeBounds].size.height == 1136){
                printf("Device Type : iPhone 5,5S/iPod 5 ");
    
            }else if([[UIScreen mainScreen] nativeBounds].size.height == 1334){
                printf("Device Type : iPhone 6 ");
    
            }else if([[UIScreen mainScreen] nativeBounds].size.height == 2208){
                printf("Device Type : iPhone 6+ ");
    
            }
        }
    }else{
        printf("Device Type : iPad");
    }
    

    If the project was created in Xcode 5 and opened in Xcode 6, then use the below mentioned code to detect the devices.(This code works if no launching images for iPhone 6,6+ are assigned)

    printf("\nDetected Resolution : %d x %d\n\n",(int)[[UIScreen mainScreen] nativeBounds].size.width,(int)[[UIScreen mainScreen] nativeBounds].size.height);
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
        {
           if([[UIScreen mainScreen] nativeBounds].size.height == 960 || [[UIScreen mainScreen] nativeBounds].size.height == 480){
                printf("Device Type : iPhone 4,4s");
                appType=1;
            }else if([[UIScreen mainScreen] nativeBounds].size.height == 1136 || [[UIScreen mainScreen] nativeBounds].size.height == 1704){
                printf("Device Type : iPhone 5,5S,6,6S/iPod 5 ");
                appType=3;
            }
        }
    }else{
        printf("Device Type : iPad");
        appType=2;
    }
    

    If you are still using Xcode 5 all together then use the following code to detect the devices (iPhone 6 and 6+ will not be detected)

    printf("\nDetected Resolution : %d x %d\n\n",(int)[[UIScreen mainScreen] bounds].size.width,(int)[[UIScreen mainScreen] bounds].size.height);
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
        {
            CGSize result = [[UIScreen mainScreen] bounds].size;
            CGFloat scale = [UIScreen mainScreen].scale;
            result = CGSizeMake(result.width * scale, result.height * scale);
            if(result.height == 960 || result.height == 480){
                printf("Device Type : iPhone 4,4S ");
    
            }else if(result.height == 1136){
                printf("Device Type : iPhone 5s/iPod 5");
    
            }
        }
    }else{
        printf("Device Type : iPad");
    
    }
    

提交回复
热议问题