2014-10-28 44 views
7

IM,令牌pushnotification iOS8上的VS使用獲得令牌的推送通知下面的代碼行ios7的PhoneGap

我加入了下一行的支持iOS8上,但是當加入這些線路的IPA工作在iOS8上但不在ios7上,ios7上的應用程序在打開後立即關閉。

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions 
{ 

#ifdef __IPHONE_8_0 
    //Right, that is the point 
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge 
                         |UIRemoteNotificationTypeSound 
                         |UIRemoteNotificationTypeAlert) categories:nil]; 
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
#else 
    //register to receive notifications 
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; 
#endif 

#ifdef __IPHONE_8_0 
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 
{ 
    //register to receive notifications 
    [application registerForRemoteNotifications]; 
} 

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler 
{ 
    //handle the actions 
    if ([identifier isEqualToString:@"declineAction"]){ 
    } 
    else if ([identifier isEqualToString:@"answerAction"]){ 
    } 
} 
#endif 
+0

在iOS 7上運行應用程序時看到的異常是什麼? – ebi 2014-10-28 17:34:51

+0

registerUserNotificationSettings:]:無法識別的選擇器發送到實例0x16d71c60 2014-10-31 12:56:00.899 ClickMobileCDV [1875:60b] ***終止應用程序,由於未捕獲異常'NSInvalidArgumentException',原因:' - [UIApplication registerUserNotificationSettings:] :無法識別的選擇器發送到實例0x16d71c60' ***第一次拋出調用堆棧: – 2014-10-29 08:59:35

回答

7

解決辦法:

#ifdef __IPHONE_8_0 
    if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) { 
     UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil]; 
     [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
    } 
#else 
    //register to receive notifications 
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; 
#endif 
+1

#ifdef是預處理器指令。你可以發佈一個二進制文件到App Store,它可以同時適用於iOS7和iOS8嗎? – mikebz 2015-01-31 18:38:07

10

由於IO8,registerForRemoteNotificationTypes已被棄用。 rgisterUserNotificationSettings:與registerForRemoteNotifications。

測試是否registerUserNotificationSettings:API在運行時可用,意味着您在iOS8上運行。

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { 
     UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; 
     [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; 
     [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    } else { 
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 
    } 
+1

這應該是被接受的答案 – kgiannakakis 2015-03-21 14:58:34

+0

完美的解決方案! – 2015-05-14 23:06:56

相關問題