首页 > 代码库 > iOS-解决iOS8及以上设置applicationIconBadgeNumber报错的问题
iOS-解决iOS8及以上设置applicationIconBadgeNumber报错的问题
在IOS7中设置applicationIconBadgeNumber不会有什么问题,但是直接在IOS8中设置applicationIconBadgeNumber会报错。因为在IOS8中要想设置applicationIconBadgeNumber,需要用户的授权,在IOS8中,需要加上下面的代码:
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
但是这两句如果是在IOS7的系统中运行的话就会报错,所以需要先判断IOS的版本,完整代码如下:
float version = [[[UIDevice currentDevice] systemVersion] floatValue]; if (version >= 8.0) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; }
如果是注册消息推送的话,需要这样写:
float version = [[[UIDevice currentDevice] systemVersion] floatValue]; if (version >= 8.0) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [application registerForRemoteNotifications]; }
iOS-解决iOS8及以上设置applicationIconBadgeNumber报错的问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。