在iOS 5应用程序中有条件支持iOS 6功能

Ser*_*yov 7 iphone xcode objective-c ios iphone-5

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

例如,如果用户有iOS 5,他会看到一个UIActionSheet,如果用户有iOS 6,他会看到不同UIActionSheet的iOS 6?你怎么做到这一点?我有Xcode 4.5,想要在iOS 5上运行的应用程序.

Dan*_*iel 19

您应该总是更喜欢检测可用的方法/功能,而不是iOS版本,然后假设方法可用.

请参阅Apple文档.

例如,在iOS 5中显示模态视图控制器,我们会这样做:

[self presentModalViewController:viewController animated:YES];
Run Code Online (Sandbox Code Playgroud)

在iOS 6中,presentModalViewController:animated:方法UIViewController是Deprecated,你应该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];
Run Code Online (Sandbox Code Playgroud)

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

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 !");
Run Code Online (Sandbox Code Playgroud)

我不确定你的UIActionSheet例子,据我所知,这在iOS 5和6上都是一样的.也许你想要UIActivityViewController分享,UIActionSheet如果你在iOS 5上,你可能会想要回归,所以你可能会检查课程是否可用,请参阅此处如何操作.