簡體   English   中英

如何確定iphone / ipad / ipod / appletv是否較舊,較新或等同於同一家族的其他設備?

[英]How to determine if iphone / ipad / ipod / appletv is older, newer or equal to other device of same family?

如何比較不同的平台?

示例:我想知道當前設備是否早於iPhone 4S。

將這些方法添加到某個類(例如,YourClass)中:

+(NSString*)deviceVersion{
    // get hardware device version string
    struct utsname systemInfo;
    uname(&systemInfo);
    return [NSString stringWithCString:systemInfo.machine
                              encoding:NSUTF8StringEncoding];
    /*      possible values:
     @"i386" (or similar)     on the simulator //
     @"iPod1,1"   on iPod Touch
     @"iPod2,1"   on iPod Touch Second Generation
     @"iPod3,1"   on iPod Touch Third Generation
     @"iPod4,1"   on iPod Touch Fourth Generation
     @"iPhone1,1" on iPhone
     @"iPhone1,2" on iPhone 3G
     @"iPhone2,1" on iPhone 3GS
     @"iPad1,1"   on iPad
     @"iPad2,1"   on iPad 2
     @"iPad3,1"   on iPad 3 (aka new iPad)
     @"iPhone3,1" on iPhone 4
     @"iPhone4,1" on iPhone 4S
     @"iPhone5,1" on iPhone 5
     @"iPhone5,2" on iPhone 5
     ...future version...?       */
}

+(NSNumber *)getVersionForDevice:(NSString*)deviceToCheck{
    NSNumber* versionNumber = nil;
    if (deviceToCheck && ![deviceToCheck isEqualToString:@""]) {
        NSString* deviceMachine = [Utils deviceVersion];
        NSString* deviceVersion = nil;
        if ([deviceMachine hasPrefix:deviceToCheck]) {
            deviceVersion = [deviceMachine stringByReplacingOccurrencesOfString:deviceToCheck withString:@""];
            deviceVersion = [deviceVersion stringByReplacingOccurrencesOfString:@"," withString:@"."];
            float floatVersion = [deviceVersion floatValue];
            versionNumber = [NSNumber numberWithFloat:floatVersion];
        }
    }
    return versionNumber;
}


+(BOOL)isSimulator{
    NSString* deviceMachine = [Utils deviceVersion];
    //    NSLog(@"Utils deviceMachine: %@", deviceMachine);
    if (![deviceMachine hasPrefix:@"iPhone"] && ![deviceMachine hasPrefix:@"iPod"] && ![deviceMachine hasPrefix:@"iPad"]) {
        return 1;
    }
    return 0;
}

然后,如果在iPhone上並且想要測試版本:

float iPhoneVersion = [[YourClass getVersionForDevice:@"iPhone"] floatValue];

並為iPad ... iPod ...做類似的通話

如果您需要進行比較,可以使用

然后,您可以輕松比較相同類型的平台

UIDeviceComparisonResult result = [[UIDevice currentDevice] compareCurrentPlatformWithOtherPlatform:@"iPhone4,1"];
BOOL isOlderThanIPhone4S = result == UIDeviceComparisonResultOtherNewer;
return isOlderThanIPhone4S;

平台的所有名稱都可以在UIDevice-Hardware.m中找到

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM