Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 试图标记应用程序图标,但未标记';t收到用户授予的标记应用程序的权限:iOS 8 Xcode 6_Objective C_Xcode_Ios8_Badge - Fatal编程技术网

Objective c 试图标记应用程序图标,但未标记';t收到用户授予的标记应用程序的权限:iOS 8 Xcode 6

Objective c 试图标记应用程序图标,但未标记';t收到用户授予的标记应用程序的权限:iOS 8 Xcode 6,objective-c,xcode,ios8,badge,Objective C,Xcode,Ios8,Badge,我正在检查我的应用程序与iOS 8的兼容性,我在控制台中获得以下登录信息“试图标记应用程序图标,但尚未收到用户标记应用程序的权限”。谁能帮我摆脱这个警告。是的,我的应用程序在应用程序图标和选项卡栏图标上显示徽章。iOS 8有一个名为RegisterNotificationSettings:的应用程序方法。部分文档说,“如果你的应用程序在后台显示警报、播放声音或标记图标,你必须在启动周期内调用此方法,以请求以这些方式向用户发出警报的权限。”你可以使用 if(SYSTEM_VERSION_LESS_

我正在检查我的应用程序与iOS 8的兼容性,我在控制台中获得以下登录信息“试图标记应用程序图标,但尚未收到用户标记应用程序的权限”。谁能帮我摆脱这个警告。是的,我的应用程序在应用程序图标和选项卡栏图标上显示徽章。

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

你可以使用

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)
对于推送通知,我认为它会解决这个问题,在我的模拟器上,我得到了这个警告,因为它不支持推送,并且我建议用户拒绝该权限,然后您将再次收到该警告。 谢谢。

您可以使用

    #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

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

参见WWDC 2014年会议视频: , (纯文字版) 和 :

用户可以更改设置中每个
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;

下面是我在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

与检查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

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

对于“快速者”,上述代码为:

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
    }
}

你唯一需要的是

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

}

你们在模拟器上使用它吗?不,我在用这个设备测试我的应用程序。@Souviccse它在模拟器上工作吗?谢谢@Phillip的快速回复。它在iOS 6和iOS 7中也能工作吗?或者我们需要对此进行检查吗?检查您正在运行的操作系统版本。如果查看其标题中的方法,它会显示“NS\u AVAILABLE\u IOS(8\u 0)”,因此您不能在早期版本上调用它。默认情况下,不会声明
系统版本\u大于或等于(…)
宏。我得到一个警告。您正在使用此方法吗?是的,我正在使用相同的要点。不需要#ifdef u IPHONE 8 0,因为除非您实际使用ios 8或更高版本,否则不会调用该委托方法。您也可以使用
if([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]))
检测是否可以调用iOS 8.0方法
NSFoundationVersionNumber
是首选。对于像我这样的新手,这会出现在
didFinishLaunchingWithOptions
功能中的
AppDelegate.swift
文件中,当应用程序启动时出现的警报消息会询问所有通知类型,即使你从列表中删除了一些通知类型。正如苹果公司常说的,不要使用#定义或版本来测试功能,使用选择器:respondsToSelector。。正如mikplus指出的那样。
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
    }
}
if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_8_0)       {
// here you go with iOS 8
} else {

}