使用未声明的类型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() 

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

使用未声明的类型UNAuthorizationOptions

我也有与UNUserNotificationCenter类相关的错误。

我使用的是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中的一个新框架,也应该记得选择正确的部署目标。