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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25973364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:27:08  来源:igfitidea点击:

Attempting to badge the application icon but haven't received permission from the user to badge the application : iOS 8 Xcode 6

objective-cxcodeios8badge

提问by Hemant Chittora

I am checking my app compatibility with iOS 8, I am getting following Log in console "Attempting to badge the application icon but haven't received permission from the user to badge the application". Can anyone please help me to get rid of this warning. And Yes, my app shows Badges on App Icon and TabBar Icon.

我正在检查我的应用程序与 iOS 8 的兼容性,我收到以下登录控制台“尝试标记应用程序图标,但尚未收到用户标记应用程序的许可”。任何人都可以帮助我摆脱这个警告。是的,我的应用程序在应用程序图标和标签栏图标上显示徽章。

采纳答案by Hemant Chittora

Here is What I did in my AppDelegate

这是我在 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

回答by KepPM

Apple makes new API for registering notifications and working with badges.

Apple 开发了用于注册通知和使用徽章的新 API。

See WWDC 2014 session video: https://developer.apple.com/videos/wwdc/2014/?id=713, http://asciiwwdc.com/2014/sessions/713(text version) and https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/registerUserNotificationSettings:

参见 WWDC 2014 会议视频:https: //developer.apple.com/videos/wwdc/2014/ ?id =713http: //asciiwwdc.com/2014/sessions/713(文字版)和 https://developer .apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/registerUserNotificationSettings

User can change permissions for every UIUserNotificationType (UIUserNotificationTypeBadge, UIUserNotificationTypeSound, UIUserNotificationTypeAlert)in Settings.

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

Before changing badge you must check permissions.

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

Code sample from my AppDelegate:

来自我的 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)

The currentUserNotificationSettings method is available in the UI application instance and will give you the most up-to-date user notification preferences.

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

Working with badge number:

使用徽章编号:

[self setApplicationBadgeNumber:0];

instead of

代替

application.applicationIconBadgeNumber = 0;

回答by user2885077

You can use

您可以使用

    #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

回答by djbp

I came across this answer whilst looking for a solution in Swift. I have done the following (assuming iOS 8):

我在 Swift 中寻找解决方案时遇到了这个答案。我已完成以下操作(假设为 iOS 8):

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

回答by Ilker Baltaci

Rather than checking the IOS Version i would check if the UIUserNotificationSettings exists and register for BadgeType like we used to do with remote notifications.

而不是检查 IOS 版本,我会检查 UIUserNotificationSettings 是否存在并注册 BadgeType,就像我们过去对远程通知所做的那样。

Class userNotification = NSClassFromString(@"UIUserNotificationSettings");

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

回答by Subathra D

If you want use Local Notification use Below Code:

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

#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

回答by Phillip Mills

iOS 8 has an application method called registerUserNotificationSettings:. Part of the docs say, "If your app displays alerts, play sounds, or badges its icon while in the background, you must call this method during your launch cycle to request permission to alert the user in those ways."

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

回答by souvickcse

You can use

您可以使用

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)

For push notification, I think it will solve it, in my case on simulator I get this warning as it doesn't support push and i the user reject the permission than again you will have that warning. Thank You.

对于推送通知,我认为它会解决它,就我而言,在模拟器上我收到此警告,因为它不支持推送,而我用户拒绝了该权限,您将再次收到该警告。谢谢你。

回答by Lucien

+ (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;
}

回答by ingconti

for "swifters" the above code:

对于“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
    }
}