iOS - 判断用户是否允许推送通知

本文介绍了一种在iOS设备上检查应用是否被允许发送本地推送通知的方法。该方法适用于iOS7及以上的系统版本,并提供了相应的Objective-C代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(iOS8中 用户开启的推送通知类型 对应的是 UIUserNotificationType (下边代码中 UIUserNotificationSettings 的types属性的类型) ,iOS7对应的是UIRemoteNotificationType)

那么如何获得呢,在iOS8中是通过types属性,[[UIApplication sharedApplication] currentUserNotificationSettings].types

如果用户没有允许推送,types的值必定为0

1 /**
 2  *  check if user allow local notification of system setting
 3  *
 4  *  @return YES-allowed,otherwise,NO.
 5  */
 6 + (BOOL)isAllowedNotification {
 7     //iOS8 check if user allow notification
 8     if ([UIDevice isSystemVersioniOS8]) {// system is iOS8
 9         UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
10         if (UIUserNotificationTypeNone != setting.types) {
11             return YES;
12         }
13     } else {//iOS7
14         UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
15         if(UIRemoteNotificationTypeNone != type)
16             return YES;
17     }
18     
19     return NO;
20 }

下面这个方法我是添加在UIDecive的Category中的,用于判断当前系统版本是大于iOS8还是小于iOS8的

 1 /**
 2  *  check if the system version is iOS8
 3  *
 4  *  @return YES-is iOS8,otherwise,below iOS8
 5  */
 6 + (BOOL)isSystemVersioniOS8 {
 7     //check systemVerson of device
 8     UIDevice *device = [UIDevice currentDevice];
 9     float sysVersion = [device.systemVersion floatValue];
10     
11     if (sysVersion >= 8.0f) {
12         return YES;
13     }
14     return NO;
15 }

 

转载于:https://www.cnblogs.com/blogfantasy/p/5213168.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值