xcode 有条件地支持 iOS 5 应用程序中的 iOS 6 功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12589866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:38:00  来源:igfitidea点击:

Conditional support of iOS 6 features in an iOS 5 app

iphoneobjective-ciosxcodeiphone-5

提问by Sergey Grischyov

How can you support features of iOS6 in an app with a Minimal Deployment Targetset to iOS 5.0?

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

For example, if a user has iOS 5 he will see one UIActionSheet, if the user has iOS 6 he will see a different UIActionSheetfor iOS 6? How do you do this? I have Xcode 4.5 and want an app running on iOS 5.

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

回答by Daniel

You should always prefer detecting available methods/feature rather then iOS versions and then assuming a method is available.

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

See Apple documentation.

请参阅Apple 文档

For example, in iOS 5 to display a modal view controller we would do something like this:

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

[self presentModalViewController:viewController animated:YES];

In iOS 6, the presentModalViewController:animated:method of UIViewControlleris Deprecated, you should use presentViewController:animated:completion:in iOS 6, but how do you know when to use what?

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

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.

您可以检测 iOS 版本并使用 if 语句来指示您是使用前者还是后者,但是,这很脆弱,您会犯错,也许将来更新的操作系统会有新的方法来做到这一点。

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 !");

I'm unsure about your UIActionSheetexample, as far as I'm aware this is the same on iOS 5 and 6. Maybe you are thinking of UIActivityViewControllerfor sharing and you might like to fallback to a UIActionSheetif you're on iOS 5, so you might to check a class is available, see herehow to do so.

我不确定你的UIActionSheet例子,据我所知,这在 iOS 5 和 6 上是一样的。也许你正在考虑UIActivityViewController分享,UIActionSheet如果你在 iOS 5 上,你可能想回退到一个,所以你可能会检查类是否可用,请参阅此处如何操作。