不久之后iPhone 6/6 plus就会在国内如雨后春笋般遍地开花了。iOS 8早已现行一步,不过有的开发者也注意到了在iOS 8上推送通知的注册方式有所变化,报错提示为:
1
|
registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later.
|
之后国外有开发者在使用了新的解决方案注册成功后发现在iPhone 6上仍然不能运行。鉴于iPhone 6/6 Plus将在国内开售,我们大可未雨绸缪一下。
iOS 8因为改变了推送消息的注册方式,所以在有推送需求的应用开发时,需要有些与以前不同的修改。然后可以试试在appDelegate.m文件的didFinishLaunchingWithOptions方法中加入下面一段代码以避免在iPhone 6/6 Plus上出现问题:
1
2
3
4
5
6
7
8
9
10
11
12
|
#ifdef __IPHONE_8_0 //这里主要是针对iOS 8.0,相应的8.1,8.2等版本各程序员可自行发挥,如果苹果以后推出更高版本还不会使用这个注册方式就不得而知了…… if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
} #else UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; #endif |
然后在appDelegate.m中加入此方法:
1
2
3
4
5
6
|
#ifdef __IPHONE_8_0 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { [application registerForRemoteNotifications];
} #endif |