2017-03-14 108 views
2

我的主要错误是这样的:火力地堡通知斯威夫特3

"Argument of #selector refers to instance method 'tokenRefreshNotification' that is not exposed to Objective-C"

这是我的AppDelegate:

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 
    let gcmMessageIDKey = "gcm.message_id" 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

     GMSServices.provideAPIKey("appKey") 

     FIRApp.configure() 

     //Firebase 
     if #available(iOS 10.0, *) { 
      // For iOS 10 display notification (sent via APNS) 
      UNUserNotificationCenter.current().delegate = self 

      let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 
      UNUserNotificationCenter.current().requestAuthorization(options: authOptions, 
                    completionHandler: {_, _ in}) 

      // For iOS 10 data message (sent via FCM) 
      FIRMessaging.messaging().remoteMessageDelegate = self 
     } else { 
      let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [ .alert, .badge, .sound], categories: nil) 
      application.registerUserNotificationSettings(settings) 
     } 

     application.registerForRemoteNotifications() 


     // Add observer for iNSTANCEid TOKEN REFRESH CALLBACK. 
     NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: .firInstanceIDTokenRefresh, object: nil) 

     // Override point for customization after application launch. 
     return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) 
    } 
    // [START refresh_token] 
    func tokenRefreshNotification(_ notification: Notification) 
    { 
     if let refreshedToken = FIRInstanceID.instanceID().token() { 
      print("InstanceID Token \(refreshedToken)") 
     } 

     // Connect to FCM since connection may have failed when attempted before having a token 
     connectToFCM() 
    } 
    // [END refresh_token] 

    // [START connect_to_fcm] 
    func connectToFCM() 
    { 
     // Won't connect since there is no token 
     guard FIRInstanceID.instanceID().token() != nil else { 
      return 
     } 

     // Disconnect previous FCM connection if it exists 
     FIRMessaging.messaging().disconnect() 

     FIRMessaging.messaging().connect(completion: {(error) in 
      if error != nil { 
       print("Unable to connect with FCM \(error)") 
      } else { 
       print("Connected to FCM") 
      } 
     }) 

    } 
    // [END connect_to_fcm] 

错误行是这样的:

NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: .firInstanceIDTokenRefresh, object: nil) 

如果我添加@objc to tokenRefreshNotification我得到一个新的错误:

"Method cannot be [email protected] because the type of the parameter cannot be represented in Objective C"

+0

我也收到这个错误 - 你修好了吗? –

+0

我也有这个问题。你有什么解决方案吗? – user816

回答

0

的选择是错误的,应该是

NotificationCenter.default.addObserver(self, selector: #selector(tokenRefreshNotification:), name: .firInstanceIDTokenRefresh, object: nil) 

必须有一个“:”在选择结束。

此外方法签名似乎是错误的,应该是

@objc func tokenRefreshNotification(notification: Notification) { 

下划线实在是太多了。

+0

这不能解决问题 –

相关问题