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

现如今的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之后,如果项目需要兼容之前版本,请自行查找其他文章。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值