ios – 调试输出提示我应该申请应用程序徽章权限

ios – 调试输出提示我应该申请应用程序徽章权限,第1张

概述我做了一个非常简单的应用程序,可以在后台运行一个计时器运行。如果应用程序仍在后台并且计时器结束,它将发送本地通知并将应用程序徽章设置为1.当我启动应用程序时,我总是清除它。我注意到安装Xcode 6后,每次启动该应用程序时都会收到此消息: “尝试标记应用程序图标但尚未获得用户对应用程序的许可” 很明显,文字是由我的应用程式产生,将徽章设为0,以将其清除。我在哪里设置这些权限或要求他们?现在被认为是 我做了一个非常简单的应用程序,可以在后台运行一个计时器运行。如果应用程序仍在后台并且计时器结束,它将发送本地通知并将应用程序徽章设置为1.当我启动应用程序时,我总是清除它。我注意到安装Xcode 6后,每次启动该应用程序时都会收到此消息:

“尝试标记应用程序图标但尚未获得用户对应用程序的许可”

很明显,文字是由我的应用程式产生,将徽章设为0,以将其清除。我在哪里设置这些权限或要求他们?现在被认为是推送通知吗?

问题已经解决,答案在下面发布。最重要的是,您需要从用户那里获得任何通知的确认,而以前只有推送通知才是真实的。

解决方法 我最终没有使用应用程序徽章,我放弃了我在这里平均发布的初始代码段。由于仍然有人阅读和评论这个问题,我将在这里添加我的工作当前解决方案。它包含对iOS7的检查,但我不使用回调方法。此外,此版本不仅要求应用程序徽章权限。

UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categorIEs:nil];[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

这是我现在用的

.h文件

#import <Foundation/Foundation.h>@interface NotificationPermissionHandler : NSObject+ (voID)checkPermissions;+ (bool)canSendNotifications;@end

.m文件:

#import "NotificationPermissionHandler.h"@implementation NotificationPermissionHandlerstatic const UIUserNotificationType USER_NOTIFICATION_TYPES_required = UIUserNotificationTypeAlert | UIUserNotificationTypeSound;static const UIRemoteNotificationType REMOTE_NOTIFICATION_TYPES_required = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;+ (voID)checkPermissions;{    bool isIOS8OrGreater = [[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)];    if (!isIOS8OrGreater)    {        [NotificationPermissionHandler iOS7AndBelowPermissions];        return;    }    [NotificationPermissionHandler iOS8AndAbovePermissions];}+ (voID)iOS7AndBelowPermissions{    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:REMOTE_NOTIFICATION_TYPES_required];}+ (voID)iOS8AndAbovePermissions;{    if ([NotificationPermissionHandler canSendNotifications])    {        return;    }    UIUserNotificationSettings* requestedSettings = [UIUserNotificationSettings settingsForTypes:USER_NOTIFICATION_TYPES_required categorIEs:nil];    [[UIApplication sharedApplication] registerUserNotificationSettings:requestedSettings];}+ (bool)canSendNotifications;{    UIApplication *application = [UIApplication sharedApplication];    bool isIOS8OrGreater = [application respondsToSelector:@selector(currentUserNotificationSettings)];    if (!isIOS8OrGreater)    {        // We actually just don't kNow if we can,no way to tell programmatically before iOS8        return true;    }    UIUserNotificationSettings* notificationSettings = [application currentUserNotificationSettings];    bool canSendNotifications = notificationSettings.types == USER_NOTIFICATION_TYPES_required;    return canSendNotifications;}@end

这是我的第一个解决方案

我把它作为初步讨论的参考。此代码未被维护。

UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge];[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

您也可以通过以下方式将权限叠加到一个请求中:

UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categorIEs:nil];[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

此外,从iOS 8起,可以确定用户允许哪种警报:

UIUserNotificationSettings* notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];if (notificationSettings.types == UIUserNotificationTypeBadge){     // change the badge}

我最终使用这个代码:

- (BOol)application:(UIApplication *)application dIDFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];    if (![defaults objectForKey:@"first_run"])    {        [self setDefaults];    }    [self askAlertPermissions];    if ([self canChangeBadge])    {         [self setBadge:0];    }    return YES;}- (voID)setDefaults;{    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];    [defaults setobject:[NSNumber numberWithBool:NO] forKey:@"alerts_allowed"];    [defaults setobject:[NSDate date] forKey:@"first_run"];    // More defaults if needed    [defaults synchronize];}- (voID)askAlertPermissions;{    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categorIEs:nil];    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];}// This will be called only after confirming your settings- (voID)application:(UIApplication *)application dIDRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings;{    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];    // There is also a built in method to find out if the user has appropriate settings,you might want to use that instead if you just want to kNow what the setting is    [defaults setobject:[NSNumber numberWithBool:YES] forKey:@"alerts_allowed"];}- (bool)canChangeBadge;{    UIUserNotificationSettings* notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];    return notificationSettings.types == UIUserNotificationTypeBadge;}

更多阅读:

https://developer.apple.com/library/prerelease/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref

总结

以上是内存溢出为你收集整理的ios – 调试输出提示我应该申请应用程序徽章权限全部内容,希望文章能够帮你解决ios – 调试输出提示我应该申请应用程序徽章权限所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1073141.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存