开发者社区> 问答> 正文

移动推送 iOS : Main Thread Checker: UI API called on a

移动推送 iOS : Main Thread Checker: UI API called on a background thread,如何解决?

展开
收起
保持可爱mmm 2020-03-28 00:02:23 889 0
1 条回答
写回答
取消 提交回答
  • 请检查工程中,是否在后台线程(非主线程)调用 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."); } }];

    2020-03-28 00:05:45
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载