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;
}
但也许有更好的解决方案