如果在iOS 8.0及更高版本中不支持registerForRemoteNotificationTypes:则构buildiOS版本
如果设备注册通知的方式发生了重大变化,并且我们不能再使用registerForRemoteNotificationTypes:如果我们不能使用Xcode 6testing版,我们如何构build新版本的应用程序来支持iOS 8? 我们将不得不build立并提交Xcode 6 GM版本的发布date,以便用户继续获取推送通知?
iOS 8已更改通知注册。 所以你需要检查设备版本,然后你需要注册通知设置。(请检查这个链接。)我在Xcode 6上尝试这个代码,它为我工作。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; } return YES; }
您可能要考虑使用respondsToSelector而不是检查系统版本:
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]){ [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else{ [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; }
#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
根据苹果文档registerForRemoteNotificationTypes:
在iOS 8中已被弃用,您可以使用registerForRemoteNotifications
和registerUserNotificationSettings:
Xcode 6 beta和iOS 8 beta是预发布的软件。 testing版仅用于开发和testing。 新的应用程序和应用程序更新必须与发布版本的Xcode和iOS一起构build,才能提交到App Store。
if ([[[ UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; }
你可以使用这个,
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) { // for iOS 8 [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [application registerForRemoteNotifications]; } else { // for iOS < 8 [application registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)]; } // RESET THE BADGE COUNT application.applicationIconBadgeNumber = 0;
在AppDelegate的didFinishLaunchingWithOptions函数中写入这段代码
if([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)]) { UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { UIRemoteNotificationType types = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:types]; } [self.window makeKeyAndVisible]; return YES;