在iOS 5应用程序中有条件地支持iOS 6功能 [英] Conditional support of iOS 6 features in an iOS 5 app
问题描述
如何在最小部署目标
设置为iOS 5.0的应用中支持iOS6的功能?
How can you support features of iOS6 in an app with a Minimal Deployment Target
set to iOS 5.0?
例如,如果用户拥有iOS 5,他将看到一个 UIActionSheet
,如果用户拥有iOS 6,他将看到不同的 UIActionSheet
适用于iOS 6?你怎么做到这一点?
我有Xcode 4.5,想在iOS 5上运行一个应用程序。
For example, if a user has iOS 5 he will see one UIActionSheet
, if the user has iOS 6 he will see a different UIActionSheet
for iOS 6? How do you do this?
I have Xcode 4.5 and want an app running on iOS 5.
推荐答案
你应该总是喜欢检测可用的方法/功能而不是iOS版本,然后假设有一个方法。
You should always prefer detecting available methods/feature rather then iOS versions and then assuming a method is available.
参见 Apple文档。
例如,在iOS 5中显示一个模态视图控制器我们会这样做:
For example, in iOS 5 to display a modal view controller we would do something like this:
[self presentModalViewController:viewController animated:YES];
在iOS 6中, presentModalViewController:animated:
UIViewController
的方法已弃用,你应该在iOS 6中使用 presentViewController:animated:completion:
,但是你怎么做知道什么时候使用什么?
In iOS 6, the presentModalViewController:animated:
method of UIViewController
is Deprecated, you should use presentViewController:animated:completion:
in iOS 6, but how do you know when to use what?
你可以检测到iOS版本并且如果你使用前者或后者就有一个if语句,但这很脆弱,你会做一个错误,也许未来更新的操作系统会有新的方法来实现这一目标。
You could detect iOS version and have an if statement dictate if you use the former or latter but, this is fragile, you'll make a mistake, maybe a newer OS in the future will have a new way to do this.
处理这个问题的正确方法是:
The correct way to handle this is:
if([self respondsToSelector:@selector(presentViewController:animated:completion:)])
[self presentViewController:viewController animated:YES completion:^{/* done */}];
else
[self presentModalViewController:viewController animated:YES];
你甚至可以说你应该更加严格,做一些事情:
You could even argue that you should be more strict and do something like:
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
进行共享,你可能想要回退到 UIActionSheet
如果您使用的是iOS 5,那么您可以查看课程是否可用,请参阅这里怎么做。
I'm unsure about your UIActionSheet
example, as far as I'm aware this is the same on iOS 5 and 6. Maybe you are thinking of UIActivityViewController
for sharing and you might like to fallback to a UIActionSheet
if you're on iOS 5, so you might to check a class is available, see here how to do so.
这篇关于在iOS 5应用程序中有条件地支持iOS 6功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!