我正在检查我的应用程序与iOS 8的兼容性,我正在关注登录控制台"尝试标记应用程序图标,但尚未获得用户的许可以标记应用程序&#34 ; 。任何人都可以帮我摆脱这个警告。是的,我的应用程序在App Icon和TabBar Icon上显示徽章。
答案 0 :(得分: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
答案 1 :(得分:16)
Apple为注册通知和使用徽章制作了新的API。
参见WWDC 2014会话视频: 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)
UI应用程序实例中提供了currentUserNotificationSettings方法,它将为您提供最新的用户通知首选项。
使用徽章编号:
[self setApplicationBadgeNumber:0];
而不是
application.applicationIconBadgeNumber = 0;
答案 2 :(得分: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
答案 3 :(得分:7)
我在寻找Swift的解决方案时遇到了这个答案。我做了以下(假设iOS 8):
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
UIApplication.sharedApplication().registerForRemoteNotifications()
答案 4 :(得分:5)
不是检查IOS版本,而是检查UIUserNotificationSettings是否存在并注册BadgeType,就像我们以前用过远程通知一样。
Class userNotification = NSClassFromString(@"UIUserNotificationSettings");
if (userNotification)
{
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
}
答案 5 :(得分: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
答案 6 :(得分:3)
iOS 8有一个名为registerUserNotificationSettings:
的应用程序方法。部分文档说:“如果您的应用在后台显示警报,播放声音或标记其图标,则必须在启动周期中调用此方法,以请求以这些方式提醒用户的权限。”
答案 7 :(得分:1)
您可以使用
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)
对于推送通知,我认为它会解决它,在我的情况下在模拟器上我得到这个警告,因为它不支持推送我用户拒绝许可,而不是再次你会有这个警告。 谢谢。
答案 8 :(得分:0)
+ (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;
}
答案 9 :(得分:0)
" 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
}
}
答案 10 :(得分:0)
你唯一需要的是
if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_8_0) {
// here you go with iOS 8
} else {
}