简体   繁体   English

为多个iOS版本开发应用程序

[英]Developing apps for multiple iOS version

I am testing an app on an iPhone 4 with iOS 5.1 and an iPad 4 with iOS 6.0. 我正在使用iOS 5.1和带有iOS 6.0的iPad 4测试iPhone 4上的应用程序。 I looked around and surprisingly did not find similar questions: 我环顾四周,出乎意料地没有找到类似的问题:

1- My app has some methods that have been deprecated in iOS 6.0 so I believe I have to build some if/then conditions to test for system version using: [[UIDevice currentDevice] systemVersion], and then use the version appropriate methods. 1-我的应用程序有一些在iOS 6.0中已被弃用的方法,因此我认为我必须构建一些if / then条件来测试系统版本,使用:[[UIDevice currentDevice] systemVersion],然后使用版本相应的方法。 Is that the most efficient way? 这是最有效的方式吗?

2- My understanding is, with only one target, the "project" Deployment Target and the "Targets" deployment target serve the same purpose. 2-我的理解是,只有一个目标,“项目”部署目标和“目标”部署目标服务于同一目的。 And I need to have one of them or both as iOS 5.1 since that is the minimum iOS supported. 我需要将其中一个或两个作为iOS 5.1,因为这是iOS支持的最低要求。 What is confusing is that if the target is built based on iOS 5.1, how would it run on the iPad4 with iOS 6.0: 令人困惑的是,如果目标是基于iOS 5.1构建的,它将如何在iOS4的iPad4上运行:

  • Does the iPad OS checks for target versions before running or just tries to run the code and it happens that the iOS 5.1 target does not have any code that the 6.0 is incompatible with? iPad OS是在运行之前检查目标版本还是只是尝试运行代码,而且iOS 5.1目标没有任何与6.0不兼容的代码?

  • Even if that is the case though, how could a 5.1 target support 6.0 methods that I built to conditionally replace deprecated methods? 即使是这种情况,5.1目标如何支持我为有条件地替换弃用方法而构建的6.0方法?

Many thanks! 非常感谢!

Deprecated methods 不推荐的方法

Deprecated methods can be used if you are targetting iOS versions that were released before those methods were deprecated. 如果要定位在不推荐使用这些方法之前发布的iOS版本,则可以使用不推荐使用的方法。 But assuming your deployment target is set correctly, you won't get any compiler errors unless those deprecated methods were always deprecated for the versions you are targetting. 但假设您的部署目标设置正确,您将不会收到任何编译器错误,除非这些已弃用的方法始终不适用于您要定位的版本。 In other words, if you are seeing deprecation warnings in your code you need to fix them or check that your deployment target setting is correct. 换句话说,如果您在代码中看到弃用警告,则需要修复它们或检查部署目标设置是否正确。 Do not ignore them! 不要忽视它们!

Xcode setting levels Xcode设置级别

You mention the fact that you can define the deployment target setting at both the target and project level. 您提到可以在目标和项目级别定义部署目标设置这一事实。 Xcode build settings at the target level will override project settings. 目标级别的Xcode构建设置将覆盖项目设置。 So define the deployment target at one of these levels only, then go to the other and hit delete so you don't have duplicate values. 因此,仅在其中一个级别定义部署目标,然后转到另一个并点击删除,这样您就没有重复的值。 If you only have one target then it doesn't really matter if you define it at the target or project level. 如果您只有一个目标,那么在目标或项目级别定义它并不重要。

Backwards and forwards compatibility 向后兼容兼容性

Finally, there are many factors that come into play for backwards and forwards compatibility. 最后,有许多因素可用于向后兼容。 Sometimes there will be new iOS 6 methods like supportedInterfaceOrientations which will simply be ignored on older iOS versions. 有时候会有新的iOS 6方法,比如supportedInterfaceOrientations ,在旧的iOS版本中会被忽略。 Other times you need to add explicit checks: 其他时候您需要添加显式检查:

  • If you are calling a method on an object and that method was only introducted with iOS 6, you will need to add a respondsToSelector: check like this: 如果要在对象上调用方法,并且该方法仅在iOS 6中引入,则需要添加respondsToSelector:检查如下:

     // only available on iOS 6 if ([locationManager respondsToSelector:@selector(pausesLocationUpdatesAutomatically)]) { locationManager.pausesLocationUpdatesAutomatically = YES; } 
  • If you want to check if a particular class exists on the current iOS version, you can check the return value of the +class method like this: 如果要检查当前iOS版本上是否存在特定类,可以检查+ class方法的返回值,如下所示:

     // Only available on iOS 6 if ([UICollectionView class]) { // ... } else { // class doesn't exist in this iOS version } 
  • If you want to check if a particular function is available, do a simple if statement on it: 如果要检查特定函数是否可用,请在其上执行简单的if语句:

     // Only available in iOS 6 if (ABAddressBookCreateWithOptions) { ABAddressBookCreateWithOptions(...); } else { ABAddressBookCreate(...); } 
  • Finally, if you want to check if a constant is available, check it's address: 最后,如果要检查常量是否可用,请检查它的地址:

     // Only available in iOS 4 if (&UIApplicationProtectedDataDidBecomeAvailable) { // subscribe to notification } 

Your Base SDK setting should always be set to "latest". 您的Base SDK设置应始终设置为“最新”。

If you follow all these guidelines you will be able to solve most of your problems without having to add explicit version checks. 如果您遵循所有这些准则,您将能够解决大多数问题,而无需添加显式版本检查。 Checking the iOS version or device ID is very brittle and is likely to cause your app to break in future versions. 检查iOS版本或设备ID非常脆弱,可能会导致您的应用在未来版本中中断。 You really want to avoid it. 你真的想避免它。

  1. You can use [[UIDevice currentDevice] systemVersion] to detect the OS version, that would work. 您可以使用[[UIDevice currentDevice] systemVersion]来检测[[UIDevice currentDevice] systemVersion]的操作系统版本。 Rather than detecting the OS version, you could use respondsToSelector: to see what methods are present, NSClassFromString() != nil to see if classes are present, and fallback if that method is not available. 您可以使用respondsToSelector:查看存在的方法, NSClassFromString() != nil以查看是否存在类,以及如果该方法不可用则使用回退,而不是检测操作系统版本。

  2. iOS is backward compatible, so iOS 6 will run iOS 5 apps just fine. iOS向后兼容,因此iOS 6可以正常运行iOS 5应用程序。 However, if you want to use iOS 6 specific feature but still support iOS 5.1, you have to : 但是,如果您想使用iOS 6特定功能但仍支持iOS 5.1,则必须:

    • Compile using the 6.0 SDK so that the compilers knows new APIs 使用6.0 SDK进行编译,以便编译器知道新的API
    • Set your target version to the lowest supported, aka. 将目标版本设置为支持的最低版本,也称为。 5.1. 5.1。 Any system higher than that number will run the code. 任何高于该数字的系统都将运行代码。 Any lower will not try. 任何低级都不会尝试。
    • Use one of the method described in 1. to make sure that each device execute codes the OS support. 使用1.中描述的方法之一,以确保每个设备执行代码OS支持。

I hope that's clear, don't hesitate if you have more questions 我希望这很清楚,如果你有更多问题,请不要犹豫

What you must do is detect functionality, not iOS version, please check out this thread: 您必须做的是检测功能,而不是iOS版本,请查看此主题:

Conditional support of iOS 6 features in an iOS 5 app 在iOS 5应用程序中有条件支持iOS 6功能

iOS is backwards compatible. iOS向后兼容。 If you compile your app for iOS 5.1 it will be perfectly run on iOS 6 as well. 如果您为iOS 5.1编译应用程序,它也将在iOS 6上完美运行。 No need to re-implement deprecated classes. 无需重新实现已弃用的类。

But if you change your deployment version from 5.0 to 6.0, Xcode will show you warnings if you use deprecated methods 但是,如果将部署版本从5.0更改为6.0,如果使用弃用方法,Xcode将向您显示警告

If you want to use methods, which are available only in iOS 6, you can use this check: 如果要使用仅在iOS 6中可用的方法,可以使用此检查:

if ([self respondsToSelector:@selector(doSomething:)]) {
        [self doSomething];
    }

You shouldn't check against the system version but rather check against the functionality you are trying to use. 您不应检查系统版本,而应检查您尝试使用的功能。 On that note remember that deprecated does not mean removed so whatever you are trying to do may very well be there for iOS6. 在那个注意事项中记住,弃用并不意味着删除所以无论你想要做什么,都可能适用于iOS6。 To check against a specific functionality you would do something like: 要检查特定功能,您可以执行以下操作:

if([TheClassInQuestion class] != nil)
{
    //use it
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM