mee*_*tpd 288 iphone cocoa-touch objective-c ios4 ios
我想检查用户是否在低于5.0的iOS上运行应用程序并在应用程序中显示标签.
如何以编程方式检测用户设备上运行的iOS?
谢谢!
Mar*_*era 675
最好的当前版本,无需在NSString中处理数字搜索就是定义macros(参见原始答案:查看iPhone iOS版本)
这些宏确实存在于github中,请参阅:https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h
像这样:
#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
Run Code Online (Sandbox Code Playgroud)
并像这样使用它们:
if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
    // code here
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
    // code here
}
Run Code Online (Sandbox Code Playgroud)
获取操作系统版本:
[[UIDevice currentDevice] systemVersion]
Run Code Online (Sandbox Code Playgroud)
返回字符串,可以转换为int/float via
-[NSString floatValue]
-[NSString intValue]
Run Code Online (Sandbox Code Playgroud)
像这样
两个值(floatValue,intValue)将因其类型而被剥离,5.0.1将变为5.0或5(float或int),为了准确比较,你必须将它分离到INT数组中检查接受的答案:检查iPhone iOS版本
NSString *ver = [[UIDevice currentDevice] systemVersion];
int ver_int = [ver intValue];
float ver_float = [ver floatValue];
Run Code Online (Sandbox Code Playgroud)
和这样比较
NSLog(@"System Version is %@",[[UIDevice currentDevice] systemVersion]);
NSString *ver = [[UIDevice currentDevice] systemVersion];
float ver_float = [ver floatValue];
if (ver_float < 5.0) return false;
Run Code Online (Sandbox Code Playgroud)
对于Swift 4.0语法
下面的例子只是检查设备是否是iOS11更高版本.
let systemVersion = UIDevice.current.systemVersion
if systemVersion.cgFloatValue >= 11.0 {
    //"for ios 11"
  }
else{
   //"ios below 11")
  }
Run Code Online (Sandbox Code Playgroud)
        Gab*_*lla 259
从iOS 8开始,我们可以使用新isOperatingSystemAtLeastVersion方法NSProcessInfo
   NSOperatingSystemVersion ios8_0_1 = (NSOperatingSystemVersion){8, 0, 1};
   if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:ios8_0_1]) {
      // iOS 8.0.1 and above logic
   } else {
      // iOS 8.0.0 and below logic
   }
Run Code Online (Sandbox Code Playgroud)
请注意,这将在iOS 7上崩溃,因为在iOS 8之前不存在API.如果您支持iOS 7及更低版本,则可以安全地执行检查
if ([NSProcessInfo instancesRespondToSelector:@selector(isOperatingSystemAtLeastVersion:)]) {
  // conditionally check for any version >= iOS 8 using 'isOperatingSystemAtLeastVersion'
} else {
  // we're on iOS 7 or below
}
Run Code Online (Sandbox Code Playgroud)
为了完整起见,这里是Apple自己在iOS 7 UI Transition Guide中提出的另一种方法,它涉及检查Foundation Framework版本.
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
   // Load resources for iOS 6.1 or earlier
} else {
   // Load resources for iOS 7 or later
}
Run Code Online (Sandbox Code Playgroud)
        Yi *_*ang 22
我知道我来不及回答这个问题.我不确定我的方法是否仍适用于低iOS版本(<5.0):
NSString *platform = [UIDevice currentDevice].model;
NSLog(@"[UIDevice currentDevice].model: %@",platform);
NSLog(@"[UIDevice currentDevice].description: %@",[UIDevice currentDevice].description);
NSLog(@"[UIDevice currentDevice].localizedModel: %@",[UIDevice currentDevice].localizedModel);
NSLog(@"[UIDevice currentDevice].name: %@",[UIDevice currentDevice].name);
NSLog(@"[UIDevice currentDevice].systemVersion: %@",[UIDevice currentDevice].systemVersion);
NSLog(@"[UIDevice currentDevice].systemName: %@",[UIDevice currentDevice].systemName);
Run Code Online (Sandbox Code Playgroud)
你可以得到这些结果:
[UIDevice currentDevice].model: iPhone
[UIDevice currentDevice].description: <UIDevice: 0x1cd75c70>
[UIDevice currentDevice].localizedModel: iPhone
[UIDevice currentDevice].name: Someones-iPhone002
[UIDevice currentDevice].systemVersion: 6.1.3
[UIDevice currentDevice].systemName: iPhone OS
Run Code Online (Sandbox Code Playgroud)
        小智 14
[[UIDevice currentDevice] systemVersion];
或检查版本
您可以从这里获得以下宏.
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(IOS_VERSION_3_2_0))      
{
        UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cs_lines_back.png"]] autorelease];
        theTableView.backgroundView = background;
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助
Marek Sebera的大部分时间都很棒,但是如果你像我一样并且发现你需要经常检查iOS版本,你不希望在内存中不断运行宏,因为你会经历一个非常轻微的减速,特别是在旧设备上.
相反,您希望将iOS版本计算为浮动一次并将其存储在某处.在我的情况下,我有一个GlobalVariables单例类,我用我的代码检查iOS版本使用这样的代码:
if ([GlobalVariables sharedVariables].iOSVersion >= 6.0f) {
    // do something if iOS is 6.0 or greater
}
Run Code Online (Sandbox Code Playgroud)
要在您的应用中启用此功能,请使用此代码(对于使用ARC的iOS 5+):
GlobalVariables.h:
@interface GlobalVariables : NSObject
@property (nonatomic) CGFloat iOSVersion;
    + (GlobalVariables *)sharedVariables;
@end
Run Code Online (Sandbox Code Playgroud)
GlobalVariables.m:
@implementation GlobalVariables
@synthesize iOSVersion;
+ (GlobalVariables *)sharedVariables {
    // set up the global variables as a static object
    static GlobalVariables *globalVariables = nil;
    // check if global variables exist
    if (globalVariables == nil) {
        // if no, create the global variables class
        globalVariables = [[GlobalVariables alloc] init];
        // get system version
        NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
        // separate system version by periods
        NSArray *systemVersionComponents = [systemVersion componentsSeparatedByString:@"."];
        // set ios version
        globalVariables.iOSVersion = [[NSString stringWithFormat:@"%01d.%02d%02d", \
                                       systemVersionComponents.count < 1 ? 0 : \
                                       [[systemVersionComponents objectAtIndex:0] integerValue], \
                                       systemVersionComponents.count < 2 ? 0 : \
                                       [[systemVersionComponents objectAtIndex:1] integerValue], \
                                       systemVersionComponents.count < 3 ? 0 : \
                                       [[systemVersionComponents objectAtIndex:2] integerValue] \
                                       ] floatValue];
    }
    // return singleton instance
    return globalVariables;
}
@end
Run Code Online (Sandbox Code Playgroud)
现在,您可以轻松检查iOS版本,而无需经常运行宏.特别注意我是如何将[[UIDevice currentDevice] systemVersion]NSString 转换为CGFloat的,这种CGFloat可以不使用许多已经在本页面指出的任何不正确的方法而不断访问.我的方法假设版本字符串的格式为n.nn.nn(允许以后的位丢失)并适用于iOS5 +.在测试中,这种方法比不断运行宏的速度快得多.
希望这有助于任何人遇到我遇到的问题!
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           211120 次  |  
        
|   最近记录:  |