移动推送 iOS : 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."); } }];
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。