尝试标记应用程序图标,但尚未获得用户的许可,以标记应用程序:iOS 8 Xcode 6

我正在检查我的应用程序与iOS 8的兼容性,我得到以下login控制台“试图徽章应用程序图标,但没有收到来自用户的权限来标记应用程序” 。 任何人都可以请帮我摆脱这个警告。 是的,我的应用程序显示应用程序图标和TabBar图标上的徽章。

这是我在AppDelegate中做的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // registering for remote notifications [self registerForRemoteNotification]; return YES; } - (void)registerForRemoteNotification { if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) { UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert; UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; } } #ifdef __IPHONE_8_0 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { [application registerForRemoteNotifications]; } #endif 

苹果公司为注册通知和使用徽章制作了新的API。

请参阅WWDC 2014会话video: https : //developer.apple.com/videos/wwdc/2014/ ? id = 713 , http : //asciiwwdc.com/2014/sessions/713 (文本版本)和https:// developer .apple.com / library / ios / documentation / UIKit / Reference / UIApplication_Class / index.html#// apple_ref / occ / instm / UIApplication / registerUserNotificationSettings :

用户可以在设置中更改每个UIUserNotificationType (UIUserNotificationTypeBadge, UIUserNotificationTypeSound, UIUserNotificationTypeAlert)权限。

更换徽章之前,您必须检查权限。

我的AppDelegate中的代码示例:

 - (BOOL)checkNotificationType:(UIUserNotificationType)type { UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]; return (currentSettings.types & type); } - (void)setApplicationBadgeNumber:(NSInteger)badgeNumber { UIApplication *application = [UIApplication sharedApplication]; if(SYSTEM_VERSION_LESS_THAN(@"8.0")) { application.applicationIconBadgeNumber = badgeNumber; } else { if ([self checkNotificationType:UIUserNotificationTypeBadge]) { NSLog(@"badge number changed to %d", badgeNumber); application.applicationIconBadgeNumber = badgeNumber; } else { NSLog(@"access denied for UIUserNotificationTypeBadge"); } } } #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) 

currentUserNotificationSettings方法在UI应用程序实例中可用,并将为您提供最新的用户通知首选项。

使用徽章号码:

 [self setApplicationBadgeNumber:0]; 

代替

 application.applicationIconBadgeNumber = 0; 

您可以使用

  #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; } #else [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; #endif 

我遇到了这个答案,同时在Swift中寻找解决scheme。 我做了以下(假设iOS 8):

 UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil)) UIApplication.sharedApplication().registerForRemoteNotifications() 

而不是检查IOS版本,我会检查UIUserNotificationSettings是否存在,并注册BadgeType,就像我们用远程通知一样。

 Class userNotification = NSClassFromString(@"UIUserNotificationSettings"); if (userNotification) { UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; } 

如果你想使用本地通知使用下面的代码:

 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; #else [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; #endif 

iOS 8有一个名为registerUserNotificationSettings:的应用程序方法registerUserNotificationSettings: 部分文档说:“如果您的应用程序在后台显示警报,播放声音或标记其图标,则必须在启动周期中调用此方法,以便请求以这些方式提醒用户的权限。

您可以使用

 if(SYSTEM_VERSION_LESS_THAN(@"8.0")) { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; } else { [[UIApplication sharedApplication] registerForRemoteNotifications]; } .... #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) 

对于推送通知,我认为这将解决它,在我的情况下,在模拟器我得到这个警告,因为它不支持推,我用户拒绝的权限,而不是你会有这样的警告。 谢谢。

对于“swifters”上面的代码:

 final func checkNotificationType(type : UIUserNotificationType) -> Bool { let application = UIApplication.sharedApplication() if application.respondsToSelector(Selector("registerUserNotificationSettings:")) { // iOS8 and above let currentSettings : UIUserNotificationSettings = application.currentUserNotificationSettings() let types = currentSettings.types return types.rawValue & type.rawValue > 0 }else{ return true } } 
 + (BOOL)canBadgeTheApp { BOOL canBadgeTheApp; if ([UIDevice currentDevice].systemVersion.doubleValue >= 8) { UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; canBadgeTheApp = ((types & UIRemoteNotificationTypeBadge) != 0); } else { canBadgeTheApp = YES; } return canBadgeTheApp; } 

你唯一需要的是

 if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_8_0) { // here you go with iOS 8 } else { }