Xcode 升级到 Xcode 9后,集成时若提示下述错误:
- [backcolor=transparent]Main[backcolor=transparent] [backcolor=transparent]Thread[backcolor=transparent] [backcolor=transparent]Checker[backcolor=transparent]:[backcolor=transparent] UI API called on a background thread
请检查工程中,是否在后台线程(非主线程)调用 AppKit、UIKit相关的API,比如iOS 10+ 请求通知权限时,[application registerForRemoteNotifications];在回调非主线程中执行,则Xcode 9会报上述错误。
- [backcolor=transparent][[backcolor=transparent]_notificationCenter requestAuthorizationWithOptions[backcolor=transparent]:[backcolor=transparent]UNAuthorizationOptionAlert[backcolor=transparent] [backcolor=transparent]|[backcolor=transparent] [backcolor=transparent]UNAuthorizationOptionBadge[backcolor=transparent] [backcolor=transparent]|[backcolor=transparent] [backcolor=transparent]UNAuthorizationOptionSound[backcolor=transparent] completionHandler[backcolor=transparent]:^([backcolor=transparent]BOOL granted[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]NSError[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] [backcolor=transparent]_Nullable[backcolor=transparent] error[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]granted[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]// granted
- [backcolor=transparent] [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"User authored notification."[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 向APNs注册,获取deviceToken
- [backcolor=transparent] [backcolor=transparent][[backcolor=transparent]application registerForRemoteNotifications[backcolor=transparent]];
- [backcolor=transparent] [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]// not granted
- [backcolor=transparent] [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"User denied notification."[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent]}];
应修改为:
- [backcolor=transparent][[backcolor=transparent]_notificationCenter requestAuthorizationWithOptions[backcolor=transparent]:[backcolor=transparent]UNAuthorizationOptionAlert[backcolor=transparent] [backcolor=transparent]|[backcolor=transparent] [backcolor=transparent]UNAuthorizationOptionBadge[backcolor=transparent] [backcolor=transparent]|[backcolor=transparent] [backcolor=transparent]UNAuthorizationOptionSound[backcolor=transparent] completionHandler[backcolor=transparent]:^([backcolor=transparent]BOOL granted[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]NSError[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] [backcolor=transparent]_Nullable[backcolor=transparent] error[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]granted[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]// granted
- [backcolor=transparent] [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"User authored notification."[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 向APNs注册,获取deviceToken
- [backcolor=transparent] dispatch_async[backcolor=transparent]([backcolor=transparent]dispatch_get_main_queue[backcolor=transparent](),[backcolor=transparent] [backcolor=transparent]^{
- [backcolor=transparent] [backcolor=transparent][[backcolor=transparent]application registerForRemoteNotifications[backcolor=transparent]];
- [backcolor=transparent] [backcolor=transparent]};
- [backcolor=transparent] [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]// not granted
- [backcolor=transparent] [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"User denied notification."[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent]}];