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:
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}
Error/Warning Line:
UIApplication.shared.registerForRemoteNotifications()
How to resolve this?
Hope this will help
TL;DR:
All UI manipulations should be done in the Main Thread to avoid problems. If failed to do so, Main Thread Checker (Newly introduced debugging feature in XCode 9) will produce issues at Runtime. So wrap your code in Main Thread block like below to avoid glitches and runtime warnings.
In Xcode releases before ver. 9, the warnings related to main thread would get printed in the console area textually. Anyway, you can optionally disable (not a recommended approach) the Main Thread Checker in the Diagnostic settings in Edit Scheme.
Explanation:
Apple introduced a new debugging option in XCode 9 for checking issues at Runtime for UIKit and other API's that manipulate UI elements. If there's any change to the UI elements from UIKit API at Runtime, without a Main thread block, it is highly likely to cause UI glitches and crashes. The Main Thread Checker is enabled by default to catch those issues at runtime. You can disable Main Thread Checker in the Edit Scheme window just like below, although it is not really recommended to do so:
If you have any older SDK's or Frameworks, when updating to Xcode 9, you may face this warning since some of the UIKit method calls wouldn't have been wrapped in Main Thread. Updating them to latest version would fix the issue (if the developer is aware of it and fixed it).
Quote from XCode 9 beta release notes:
This is also correct way to do in Swift 4.0
The error message is pretty clear: dispatch
registerForRemoteNotifications
to the main thread.I would use the
granted
parameter and handle theerror
accordinglyFor Objective C, the below code works
This is what worked for me. Courtesy of @Mason11987 in the accepted comment above.