UIApplication.registerForRemoteNotifications() must be called from main thread only

前端 未结 7 1266
猫巷女王i
猫巷女王i 2020-11-30 00:23

Xcode 9 (iOS 11) showing me an error/warning while registering for Push (remote) notification.

Here is error message

And here is code, I\'ve tried:<

7条回答
  •  情书的邮戳
    2020-11-30 00:38

    The error message is pretty clear: dispatch registerForRemoteNotifications to the main thread.

    I would use the granted parameter and handle the error accordingly

    center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
            if granted {
                  DispatchQueue.main.async {
                      UIApplication.shared.registerForRemoteNotifications()
                  }
            } else {
               print(error!)
               // handle the error
            }
    }
    

提交回复
热议问题