代码之家  ›  专栏  ›  技术社区  ›  Sergey Grischyov

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

  •  7
  • Sergey Grischyov  · 技术社区  · 12 年前

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

    例如,如果用户有iOS 5,他会看到一个 UIActionSheet ,如果用户拥有iOS 6,他将看到不同的 操作表 适用于iOS 6?你是怎么做到的? 我有Xcode 4.5,想要一个在iOS 5上运行的应用程序。

    1 回复  |  直到 12 年前
        1
  •  19
  •   Daniel    12 年前

    您应该始终倾向于检测可用的方法/功能,而不是iOS版本,然后假设有可用的方法。

    看见 Apple documentation

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

    [self presentModalViewController:viewController animated:YES];
    

    在iOS 6中 presentModalViewController:animated: 方法 UIViewController 已弃用,应使用 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,因此您可能需要检查是否有可用的类,请参阅 here 如何做到这一点。