registerForRemoteNotificationTypes:iOS 8.0 及更高版本不支持 [英] registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later

查看:28
本文介绍了registerForRemoteNotificationTypes:iOS 8.0 及更高版本不支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iOS 8.x 下尝试注册推送通知时:

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

application.registerForRemoteNotificationTypes(UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound)

我收到以下错误:

registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later.

任何想法是什么新方法?当我在 iOS 7.x 上运行这个 Swift 应用程序时它确实有效.

Any ideas what is the new way of doing it? It does work when I run this Swift app on iOS 7.x.

编辑

在 iOS 7.x 上,当我包含得到的条件代码时(SystemVersion 条件或 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000)

On iOS 7.x when I include the conditional code I get (either SystemVersion conditional or #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000)

dyld: Symbol not found: _OBJC_CLASS_$_UIUserNotificationSettings

推荐答案

正如您所描述的,您将需要根据不同的 iOS 版本使用不同的方法.如果您的团队同时使用 Xcode 5(它不知道任何 iOS 8 选择器)和 Xcode 6,那么您将需要使用条件编译,如下所示:

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

如果您只使用 Xcode 6,则可以坚持使用:

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

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

这里的原因是在 iOS 8 中您获得通知权限的方式发生了变化.UserNotification 是显示给用户的消息,无论是从远程还是从本地.你需要获得许可才能展示一个.这在 WWDC 2014 视频 iOS 通知中的新功能"中有所描述

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"

这篇关于registerForRemoteNotificationTypes:iOS 8.0 及更高版本不支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆