registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later

前端 未结 15 1741
时光说笑
时光说笑 2020-11-29 15:09

When trying to register for push notifications under iOS 8.x:

application.registerForRemoteNotificationTypes(UIRemoteNotificationType.Alert | UIRemoteNotific         


        
15条回答
  •  隐瞒了意图╮
    2020-11-29 15:32

    As you described, you will need to use a different method based on different versions of iOS. If your team is using both Xcode 5 (which doesn't know about any iOS 8 selectors) and Xcode 6, then you will need to use conditional compiling as follows:

    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        // use registerUserNotificationSettings
    } else {
        // use registerForRemoteNotificationTypes:
    }
    #else
    // use registerForRemoteNotificationTypes:
    #endif
    

    If you are only using Xcode 6, you can stick with just this:

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        // use registerUserNotificationSettings
    } else {
        // use registerForRemoteNotificationTypes:
    }
    

    The reason is here is that the way you get notification permissions has changed in iOS 8. A UserNotification is a message shown to the user, whether from remote or from local. You need to get permission to show one. This is described in the WWDC 2014 video "What's New in iOS Notifications"

提交回复
热议问题