使用未声明的类型 UNAuthorizationOptions

Use of undeclared type UNAuthorizationOptions

我正在尝试使用 Firebase 来处理推送通知。我已经安装了 Firebase pod('Firebase/Core' 和 'FirebaseMessaging' pods)。

并且在我将 Firebase 导入项目后

import Firebase

我已经像这样配置了 Firebase 应用程序(代码是从官方文档复制的):

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
-> Bool {FIRApp.configure() }

之后我尝试使用此代码(代码是从官方文档复制的):

if #available(iOS 10.0, *) {
            let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_,_ in })

            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self
            // For iOS 10 data message (sent via FCM)
            FIRMessaging.messaging().remoteMessageDelegate = self

        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()

但是我从标题中得到错误:

Use of undeclared type UNAuthorizationOptions

我也有与 UNUserNotificationCenter class 相关的相同错误。

我正在使用 Swift 2.2 和 Xcode 7.3.1

这个错误的原因是什么?

UserNotifications.framework 可从 iOS 10 开始使用,而您正在使用 Xcode 7.3 表示使用 iOS 9 及更低版本,因此您无需添加那if #available(iOS 10.0, *) {,只写其他部分直接注册远程通知。

let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

您需要 import UserNotifications 在调用这些框架之前。 Nirav D 说的是真的,它是 iOS 10 中的新框架,还应该记得 select 正确的部署目标。