码迷,mamicode.com
首页 > 移动开发 > 详细

iOS-解决iOS8及以上设置applicationIconBadgeNumber报错的问题

时间:2016-11-07 22:02:23      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:ios8   报错   badge   setting   types   dev   ber   oat   reg   

 

在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报错的问题

标签:ios8   报错   badge   setting   types   dev   ber   oat   reg   

原文地址:http://www.cnblogs.com/yongdaimi/p/6040711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!