使用部署目标8.1和XCODE 6.1的远程通知

时间:2015-03-11 07:19:56

标签: ios objective-c xcode6 apple-push-notifications

我想使用部署目标8.1和xcode 6.1的远程通知 我正在使用以下代码

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}

但它在其他部分显示了我deprecated方法....所以现在我怎么能摆脱它... 我试过的其他代码是:

enter image description here

4 个答案:

答案 0 :(得分:2)

你可以忽略这样的弃用警告:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#pragma clang diagnostic pop

    }
#else
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#endif

请参阅this post

答案 1 :(得分:0)

希望以下代码可以满足您的需求

if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
    // iOS 8 Notifications
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [application registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeNewsstandContentAvailability |
        UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}

答案 2 :(得分:0)

方法registerForRemoteNotificationTypes:是从iOS 8.0中删除的。由于您使用iOS 8.1 SDK进行编译,因此您将收到警告。这是一种预期的行为,您的应用程序将正常运行,因为当且仅当您的iOS版本低于8.0且定义了方法registerForRemoteNotificationTypes:时,您的应用程序才会使用registerUserNotificationSettings:

如果您需要避免任何折旧警告,可以使用-Wno-deprecated-declarations标志来禁止警告。

答案 3 :(得分:0)

确定。我可以理解您使用部署目标8.1的问题。 只需将部署目标更改为小于或等于7,否则删除else部分,因为无需在8.1目标中定义此目标