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