【问题标题】:Deployment target iOS8 but want to support iOS7 - Deprecated Warnings部署目标 iOS8 但希望支持 iOS7 - 已弃用警告
【发布时间】:2023-03-11 04:04:01
【问题描述】:

我有一个应用程序,我想以新的 iOS 8.1 为目标,但我也希望拥有 iOS 7 的用户能够使用我的应用程序。因此,例如对于我的推送通知,在我的应用程序委托中我有

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                    UIUserNotificationTypeBadge |
                                                    UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                             categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
} else {
    // Register for Push Notifications before iOS 8
    [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert |
        UIRemoteNotificationTypeSound)];
}

要为 iOS 8 和 iOS 7 打开推送通知,但是当我更改我的部署目标时,我收到警告说这已被弃用:

[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert |
            UIRemoteNotificationTypeSound)];

处理此问题的最佳方法是什么?对于提交到应用商店以及个人而言,我不希望在我的代码中看到警告。

提前感谢您的帮助。

编辑

这是一张警告图片,我看到我的部署目标是 iOS8.1,但不确定在哪里可以找到我的基础 SDK?这是图片:

【问题讨论】:

  • 有时你只需要接受在为旧版本的 iOS 构建时会有弃用警告。只要它们仅在生产构建期间发生,您就可以在每次构建时检查列表以确保没有出现任何新内容。
  • 请说明您的基础 SDK 和部署目标的设置。它应该分别是“最新(8.1)”和“iOS 7.0”。如果是这样,您只会收到自 iOS 7.0 起已弃用的方法的弃用警告。在这种情况下,只能使用 iOS 7.0 或更高版本的方法。
  • 换句话说,如果您收到关于 registerForRemoteNotificationTypes 的弃用警告,那么您的部署目标未设置为 iOS 7,而是设置为 iOS 8。
  • @rmaddy 我应该在“项目”下或“目标”下或两者下设置部署目标?
  • 目标设置优先于项目设置。

标签: ios ios7 ios8 deprecated


【解决方案1】:

按照orkenstein 所述更改您的代码:

if ([application respondsToSelector:@selector(registerForRemoteNotificationTypes:)])
{

    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                    UIUserNotificationTypeBadge |
                                                    UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                             categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
}
else
{
    [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert |
                                                     UIRemoteNotificationTypeSound)];
}

接下来调整您的构建设置
将基础 SDK 设置为最新:

...向下滚动到部署并将部署目标设置为 7.0:

请注意,通过更改此处的设置,您可以(几秒钟后)直接看到警告的开启和关闭

使用目标 = iOS 8.x

使用目标 = iOS 7.x

请忽略 SplashScreen 警告...

【讨论】:

  • 什么时候在应用商店的描述下会说这个应用是针对 iPhone 5,5s 和 6 的?
  • 这取决于您支持的架构。列表中有 armv7 吗?这应该会将 iPhone 4s 和 iPad 2 添加到列表中...
  • 项目设置 > 构建设置 > 目标架构应该类似于 "armv7, armv7s, arm64" 以支持从 iphone4s 到 iphone6 的所有设备
  • 你有 iPhone4s 的所有资产吗?请记住,它有另一个屏幕尺寸。您将需要其他启动画面图片...
  • 是的,我已经拥有了所有这些,所以我现在应该为 iOS7 设置该设置,它仍然会在商店中显示 iPhone 6 的标签?
【解决方案2】:

我们应该保留部署目标 7.0,以适用于 iOS7。 也使用基本 SDK 8.1。

if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    // iOS 7 code here

    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
     UIUserNotificationTypeBadge |
     UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];

else
    // Pre-iOS 7 code here

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];


endif

【讨论】:

  • 那会删除警告吗?我有基本 sdks 8.1 和部署 iOS7,但在应用商店中并没有说我的目标是 iPhone 6?
  • iPhone 6 是另一回事。要支持 iPhone 6,您需要添加 iPhone 6(和 6 Plus)启动图像。
【解决方案3】:

我建议使用 iOS 7.1 作为最小部署目标。我知道这很有趣,但这就是向后兼容 iOS 8 的方式。

【讨论】:

    【解决方案4】:
    -(BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version
    

    [链接]https://developer.apple.com/LIBRARY/ios/documentation/Cocoa/Reference/Foundation/Classes/NSProcessInfo_Class/index.html

    我也在 docSet 中发现了这一点 UIDevice 文档

         UIDevice *dev = [UIDevice currentDevice];
         NSString* OSVer = [dev systemVersion];
         NSLog(@"%@",OSVer);
    

    如果需要,您可以检查字符串,因为 isOperatingSystem 是 IOS 8 及更高版本,我认为它在 IOS ~7 上会返回 no

    【讨论】:

    • 看看这里也可能有帮助[链接]stackoverflow.com/questions/3339722/how-to-check-ios-version
    • 我读到 IOS 8 @selector(isOperatingSystemAtLeas....) 会使 IOS7 设备崩溃(我猜不是很聪明),但您可以检查它是否响应选择器以及是否响应您是否至少知道您使用的是 8 或更新版本。或者使用上面提到的 UIDevice...。
    【解决方案5】:

    当您想要支持多个版本时,可以使用弃用警告。
    如果方法可用,您需要检查。所以测试选择器:

    if ([application respondsToSelector:@selector(registerForRemoteNotificationTypes:)]) {
      NSLog(@"It's ok");
    } else {
      NSLog(@"Use another method");
    }
    

    更新: 如果您仍想禁用这些警告(不推荐),请更新您的目标设置:

    【讨论】:

    • 所以你是说没有办法摆脱这些警告?
    • @iqueqiorio,没有。我必须明白,API 的变化和弃用警告是开发的结果。
    • @iqueqiorio,但是!您可以为您的目标禁用弃用警告。
    • 这是不正确的。仅当您指定的部署目标中不推荐使用该方法时,才会出现弃用警告。也就是说,当您使用的东西在您支持的所有版本中都已弃用时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 2015-03-23
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多