我在互联网上到处寻找如何用IOS 8创建本地通知。我发现了很多文章,但没有人解释如何确定用户是否设置了“警报”。有人能帮帮我吗!我宁愿使用目标C而不是Swift。
发布于 2014-09-25 21:32:45
您可以使用UIApplication
的currentUserNotificationSettings来检查它。
if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){ // Check it's iOS 8 and above
UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (grantedSettings.types == UIUserNotificationTypeNone) {
NSLog(@"No permiossion granted");
}
else if (grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert ){
NSLog(@"Sound and alert permissions ");
}
else if (grantedSettings.types & UIUserNotificationTypeAlert){
NSLog(@"Alert Permission Granted");
}
}
希望这有帮助,如果你需要更多的信息,请告诉我。
发布于 2015-09-30 20:36:41
要详细说明艾伯特的答案,不需要在Swift中使用rawValue
。由于UIUserNotificationType
符合OptionSetType
,所以可以执行以下操作:
if let settings = UIApplication.shared.currentUserNotificationSettings {
if settings.types.contains([.alert, .sound]) {
//Have alert and sound permissions
} else if settings.types.contains(.alert) {
//Have alert permission
}
}
您可以使用括号[]
语法组合选项类型(类似于在其他语言中组合选项标志的按位或|
运算符)。
发布于 2016-05-31 02:31:50
快速使用guard
guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() where settings.types != .None else {
return
}
https://stackoverflow.com/questions/26051950
复制