0
我需要更新我們的應用程序以支持iOS 9和iOS 10,所以我的問題是使用UNUserNotificationCenter來執行PushNotifications。iOS 10/9推送通知
所以與iOS 9我們有返回UIUserNotificationSettings結果像
- (BOOL)alertEnabled {
UIUserNotificationSettings *theSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
return [theSettings types] & UIUserNotificationTypeAlert;
}
與iOS 10我不喜歡的東西
- (void)userNotificationsAuthorization :(void (^)(BOOL alertIsActive))completion {
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
completion(settings.alertSetting == UNNotificationSettingEnabled);
}];
}
調用它,並且通過完成處理得到它的方法。
我的問題:是有一些可能使用getNotificationSettingsWithCompletionHandler和返回的價值,而不是說我可以用它爲我的alertEnabled方法完成處理?
非常感謝。
更新:
用這種方法它的工作
- (BOOL)alertIsEnabled {
__block BOOL alertIsActive = NO;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
alertIsActive = settings.alertSetting == UNNotificationSettingEnabled;
dispatch_semaphore_signal(sem);
}];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
}
else {
UIUserNotificationSettings *theSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
alertIsActive = [theSettings types] & UIUserNotificationTypeAlert;
}
return alertIsActive;
}
但也許有更好的解決方案