iOS 5应用程序中的iOS 6function的条件支持

如何在支持Minimal Deployment Target设置为iOS 5.0的应用程序中支持iOS6的function?

例如,如果用户拥有iOS 5,他将看到一个UIActionSheet ,如果用户拥有iOS 6,他将看到针对iOS 6的不同的UIActionSheet ? 你怎么做到这一点? 我有Xcode 4.5,并希望在iOS 5上运行一个应用程序。

你应该总是喜欢检测可用的方法/function,而不是iOS版本,然后假设一个方法是可用的。

请参阅Apple文档 。

例如,在iOS 5中显示一个模式视图控制器,我们会做这样的事情:

 [self presentModalViewController:viewController animated:YES]; 

在iOS 6中, UIViewControllerpresentModalViewController:animated:方法被弃用,你应该使用presentViewController:animated:completion:在iOS 6中,但是你怎么知道什么时候该使用什么?

你可以检测iOS版本,如果你使用前者或后者,就会有一个if语句,但是,这是脆弱的,你会犯一个错误,也许一个更新的操作系统在未来会有一个新的方法来做到这一点。

处理这个问题的正确方法是:

 if([self respondsToSelector:@selector(presentViewController:animated:completion:)]) [self presentViewController:viewController animated:YES completion:^{/* done */}]; else [self presentModalViewController:viewController animated:YES]; 

你甚至可以争辩说,你应该更严格,做一些事情:

 if([self respondsToSelector:@selector(presentViewController:animated:completion:)]) [self presentViewController:viewController animated:YES completion:^{/* done */}]; else if([self respondsToSelector:@selector(presentViewController:animated:)]) [self presentModalViewController:viewController animated:YES]; else NSLog(@"Oooops, what system is this !!! - should never see this !"); 

我不确定你的UIActionSheet例子,据我所知,这是相同的iOS 5和6.也许你正在考虑UIActivityViewController共享,如果你在iOS 5上,你可能想退回到UIActionSheet ,所以你可能要检查一个类是否可用,请看这里怎么做。