当我运行iPhone应用程序时,我怎么知道正在使用什么IOS ?



自从IOS 5发布以来,我不得不对我的应用程序做一些只适用于IOS 5的更改。在启动应用程序时,我如何知道用户使用的是IOS 5还是IOS 4或更早版本?

你应该完全避免询问系统版本。

一个更好的设计应该询问一个特定的功能。

例如:if (NSClassFromString(@"UIPrintInfo"))将告诉您当前设备是否支持打印API, 4.2或更高版本可用。

这样你可以计划你的代码使用一个功能,如果它是可用的,而不是基于操作系统版本。更多关于这里-如何检查iOS版本?

但如果你必须,那么-

float version = [[[UIDevice currentDevice] systemVersion] floatValue]; 
if([[[UIDevice currentDevice] systemVersion] compare:@"5.0" options:NSNumericSearch] !=  NSOrderedDescending)
{
   // code here
}
else 
{
  // code here
}

有时候知道使用的是哪个版本的iOS是很好的。但是最好询问系统是否存在一个方法:

// iOS 5 UINavigationBar backgroundImage
if ([self.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
    UIImage *backgroundImage = [UIImage imageNamed:@"titlebar.png"];
    [self.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
} 

最新更新