您的位置:首页 > 移动开发 > IOS开发

iOS开发-iOS8.0之后获取用户通知权限

2017-06-27 17:52 260 查看
现如今的iOS开发中,除了大厂的老程序还需要兼容iOS8之前的系统版本之外,新项目基本都只支持到iOS 8 了, 这就意味着之前版本获取用户通知权限的方式不再试用,系统文件是这么描述的

// Returns the enabled types, also taking into account any systemwide settings; doesn't relate to connectivity.
- (UIRemoteNotificationType)enabledRemoteNotificationTypes NS_DEPRECATED_IOS(3_0, 8_0, "Use -[UIApplication isRegisteredForRemoteNotifications] and UserNotifications Framework's -[UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:] to retrieve user-enabled remote notification and user notification settings") __TVOS_PROHIBITED;

这个属性在iOS 8之后便不再被支持,因此我们也不好再通过该属性进行通知权限的获取。

iOS 8之后获取通知权限的方法是:

UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
/*
settings.types 有以下几种状态
0 => none 不允许通知 UIUserNotificationTypeNone
1 => badge 只允许应用图标标记 UIUserNotificationTypeBadge
2 => sound 只允许声音 UIUserNotificationTypeBadge
3 => sound + badge 允许声音+应用图标标记
4 => alert 只允许提醒 UIUserNotificationTypeAlert
5 => alert + badge 允许提醒+应用图标标记
6 => alert + sound 允许提醒+声音
7 => alert + sound + badge 三种都允许
*/


于是,获取后,我们就可通过types的值来判断用户是否开启了允许通知和开启的何种通知,之后的操作就请大家按自己需要进行了,这里再加一个跳转到设置里打开自己程序对应设置项的步骤:

NSURL *settingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingURL];

这里所有操作仅限于iOS 8之后,如果项目需要兼容之前版本,请自行查找其他文章。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息