iOS7和iOS8注册Parse通知,编译器警告。

3
我已经编写了一个应用程序,使其能够在iOS8中接收Parse通知,最近发现这些通知在iOS 9中无法正常工作,因此不得不更改我的代码,如下所示(在AppDelegate.m文件中):
viewDidLoad:
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        NSLog(@"Requesting permission for push notifications...iOS8"); // iOS 8
        UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                        UIUserNotificationTypeBadge |
                                                        UIUserNotificationTypeSound);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
        [application registerUserNotificationSettings:settings];
        [application registerForRemoteNotifications];
    } else {
        NSLog(@"Registering device for push notifications..."); // iOS 7 and earlier
        [UIApplication.sharedApplication registerForRemoteNotificationTypes:
         UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |
         UIRemoteNotificationTypeSound];
    }

并且:

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // Store the deviceToken in the current Installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    [currentInstallation saveInBackground];
}

我测试了iOS的两个版本,看起来都运行良好。但是,如果我在Xcode中将部署目标更改为iOS8或更高版本,我会收到以下编译器警告:

AppDelegate.m:62:10: 'UIRemoteNotificationTypeAlert' is deprecated: first deprecated in iOS 8.0 - Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.

AppDelegate.m:62:42: 'UIRemoteNotificationTypeBadge' is deprecated: first deprecated in iOS 8.0 - Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.

AppDelegate.m:63:10: 'UIRemoteNotificationTypeSound' is deprecated: first deprecated in iOS 8.0 - Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.

/AppDelegate.m:61:42: 'registerForRemoteNotificationTypes:' is deprecated: first deprecated in iOS 8.0 - Please use registerForRemoteNotifications and registerUserNotificationSettings: instead

看到这些警告是否正常?是因为我在更高的部署目标中工作吗?还是我需要在代码中做出改变?看起来很奇怪,我不应该看到这些警告。任何指针都将不胜感激。谢谢!

2个回答

8
如果您将部署目标更改为iOS 8,则无需检查是否为iOS 8(因为您的应用程序仅适用于iOS 8设备),因此会出现这些警告。因此,请像这样注册:
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

我将发布适用于iOS 7及以上版本的应用。有一件事让我感到困惑,如果我要开发适用于iOS 7及以上版本的应用,我需要将部署目标设置为iOS 7吗? - KexAri
是的,只需将部署目标保留为iOS 7即可。 - Hossam Ghareeb
明白了,谢谢。模拟器会在哪个版本上运行? - KexAri
如果您设置为iOS 7,则可以选择模拟器版本iOS 7或iOS 8。如果模拟器中没有iOS 7,请转到Xcode->首选项->下载选项卡->然后下载缺失的模拟器。 - Hossam Ghareeb

2

是的,这种方法在iOS8中已被弃用,但您可以使用以下代码来检查iOS7和iOS8上的条件并进行管理:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

此外,在此基础上,添加以下代码行。
if ([launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"]) {

    [self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];

}

在parse.com上注册设备时,请确保您的applicationKey和ClientKey是正确的,除此之外,您的注册设备代码块是正确的,但这不会影响iOS的弃用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接