iOS開發 --- 推送 SDK: Main Thread Checker: UI API called on a background thread
阿新 • • 發佈:2019-01-31
Xcode 升級到 Xcode 9後,整合時若提示下述錯誤:
MainThreadChecker: UI API called on a background thread
請檢查工程中,是否在後臺執行緒(非主執行緒)呼叫 AppKit、UIKit相關的API,比如iOS 10+ 請求通知許可權時,[application
registerForRemoteNotifications];
在回撥非主執行緒中執行,則Xcode 9會報上述錯誤。
[_notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert
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.");
}
}];