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

Hem*_*ora 46 xcode objective-c badge ios8

我正在检查我的应用程序与iOS 8的兼容性,我正在关注登录控制台"尝试标记应用程序图标但未获得用户的许可以标记应用程序".任何人都可以帮我摆脱这个警告.是的,我的应用程序在应用程序图标和TabBar图标上显示徽章.

Hem*_*ora 43

这是我在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
Run Code Online (Sandbox Code Playgroud)

  • 默认情况下不声明`SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(...)`宏.我正在收到警告.你使用这个[gist](https://gist.github.com/alex-cellcity/998472)? (3认同)
  • 不需要#ifdef __IPHONE_8_0,因为除非您实际使用的是ios 8或更高版本,否则不会调用该委托方法. (3认同)
  • 你也可以使用`if([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings :)])`来检测你是否可以调用iOS 8.0方法 (2认同)

Kep*_*pPM 16

Apple为注册通知和使用徽章制作了新的API.

请参阅WWDC 2014会话视频:https://developer.apple.com/videos/wwdc/2014/ id = 713, http: //asciiwwdc.com/2014/sessions/713(文本版)和 https://开发人员.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)
Run Code Online (Sandbox Code Playgroud)

UI应用程序实例中提供了currentUserNotificationSettings方法,它将为您提供最新的用户通知首选项.

使用徽章编号:

[self setApplicationBadgeNumber:0];
Run Code Online (Sandbox Code Playgroud)

代替

application.applicationIconBadgeNumber = 0;
Run Code Online (Sandbox Code Playgroud)


小智 9

您可以使用

    #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
Run Code Online (Sandbox Code Playgroud)


djb*_*jbp 7

我在寻找Swift的解决方案时遇到了这个答案.我做了以下(假设iOS 8):

UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
UIApplication.sharedApplication().registerForRemoteNotifications()
Run Code Online (Sandbox Code Playgroud)


Ilk*_*aci 5

而不是检查的IOS版本,我会检查是否存在UIUserNotificationSettings并注册BadgeType就像我们使用了远程通知来做.

Class userNotification = NSClassFromString(@"UIUserNotificationSettings");

if (userNotification)
{
    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
}
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您想使用本地通知,请使用以下代码:

#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
Run Code Online (Sandbox Code Playgroud)