iOS开发 --- 推送 SDK: Main Thread Checker: UI API called on a background thread

Xcode 升级到 Xcode 9后,集成时若提示下述错误:

 
   
  1. Main Thread Checker: UI API called on a background thread

请检查工程中,是否在后台线程(非主线程)调用 AppKit、UIKit相关的API,比如iOS 10+ 请求通知权限时,[application registerForRemoteNotifications];在回调非主线程中执行,则Xcode 9会报上述错误。

 
   
  1. [_notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
  2. if (granted) {
  3. // granted
  4. NSLog(@"User authored notification.");
  5. // 向APNs注册,获取deviceToken
  6. [application registerForRemoteNotifications];
  7. } else {
  8. // not granted
  9. NSLog(@"User denied notification.");
  10. }
  11. }];

应修改为:

 
   
  1. [_notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
  2. if (granted) {
  3. // granted
  4. NSLog(@"User authored notification.");
  5. // 向APNs注册,获取deviceToken
  6. dispatch_async(dispatch_get_main_queue(), ^{
  7. [application registerForRemoteNotifications];
  8. };
  9. } else {
  10. // not granted
  11. NSLog(@"User denied notification.");
  12. }
  13. }];

你可能感兴趣的:(iOS,调试,iOS,第三方库/工具)