iOS判断推送权限开关是否打开

根据 [[UIApplication sharedApplication] enabledRemoteNotificationTypes] 的返回值来进行判断,该返回值是一个枚举值,如下:

typedef enum {
UIRemoteNotificationTypeNone = 0,
UIRemoteNotificationTypeBadge = 1 << 0,
UIRemoteNotificationTypeSound = 1 << 1,
UIRemoteNotificationTypeAlert = 1 << 2,
UIRemoteNotificationTypeNewsstandContentAvailability = 1 << 3,
} UIRemoteNotificationType;

如果是 UIRemoteNotificationTypeNone ,则可以认为推送开关没有打开,反之亦然。

不过在iOS8中苹果对其做出了调整,需要使用下面的代码进行判断:

if (is_ios_8) {
UIUserNotificationSettings *mySet = [[UIApplication sharedApplication] currentUserNotificationSettings];
mySet.types; // <– 这里是一个枚举值
}

枚举值有如下:

typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) {
UIUserNotificationTypeNone = 0, // the application may not present any UI upon a notification being received
UIUserNotificationTypeBadge = 1 << 0, // the application may badge its icon upon a notification being received
UIUserNotificationTypeSound = 1 << 1, // the application may play a sound upon a notification being received
UIUserNotificationTypeAlert = 1 << 2, // the application may display an alert upon a notification being received
} NS_ENUM_AVAILABLE_IOS(8_0);