haven't received permission

iOS8里对权限的控制更为严格了,就是标题中的permission

以前下给icon弄个数字提醒什么的,直接一句简单的:

[UIApplication sharedApplication].applicationIconBadgeNumber=20;

就搞定了,现在这么直接iOS不干了,没有反应了,还给你一个警告

Attempting to badge the application icon but haven't received permission from the user to badge the application

大概意思就是给应用程序添加数字提醒(badge),但是你没有这个权限。

 

用户通知设置

既然没有权限那咱就加权限呗。其实类似这种给应用程序加数字提醒的功能也是一中通知(Notification),有一个类叫做UIUserNotificationSettings,从名字就能看出是一个用户界面的通知设置相关的类,这哥们里面就一个静态方法

+ (instancetype)settingsForTypes:(UIUserNotificationType)types

                      categories:(NSSet *)categories; // instances of UIUserNotificationCategory

在UIApplication里也有它的身影@interface UIApplication (UIUserNotificationSettings),乖乖人家可直接成了UIApplication的扩展了!具体使用如下

UIUserNotificationSettings *settting=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settting];
    
    [UIApplication sharedApplication].applicationIconBadgeNumber=20;
View Code

写到这里,icon上就出现了badge上设置的数字了。细看下settingsForTypes里的传参类型

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);

可见这是8.0的新玩意,看样子在一个更重要的场合:推送里面也得注册相应的UIUserNotificationType才能正常工作喽……

posted @ 2015-03-23 14:58  luseike  阅读(480)  评论(0编辑  收藏  举报