2014-10-28 13 views
7

Im utilizzando le seguenti righe di codice per ottenere gettone per notifica push,Token Per pushnotification iOS 8 vs iOS7 PhoneGap

ho aggiunto prossime righe per supportate in iOS 8, ma quando si aggiungono queste righe l'IPA funziona su iOS 8, ma non su iOS7 su iOS7 l'applicazione viene chiusa immediatamente dopo l'apertura.

- (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

Qual è l'eccezione che si vede quando si esegue l'applicazione su iOS 7? – ebi

+0

registerUserNotificationSettings:]: selettore non riconosciuto rinviati esempio 0x16d71c60 2014/10/31 12: 56: 00,899 ClickMobileCDV [1875 60b] *** terminazione app causa eccezione non identificata 'NSInvalidArgumentException', motivo: '- [registerUserNotificationSettings UIApplication:] : selettore non riconosciuto inviato all'istanza 0x16d71c60 ' *** Primo stack di chiamate throw: –

risposta

7

La soluzione:

#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

il #ifdef è una direttiva pre-processore. Puoi spedire un binario con quello all'App Store e funzionerà sia per iOS7 che per iOS8? – mikebz

10

Dal IO8, registerForRemoteNotificationTypes è stato sconsigliato. rgisterUserNotificationSettings: con registerForRemoteNotifications.

Testing con o senza il registerUserNotificationSettings: API è disponibile in fase di esecuzione, significa che stai correndo su 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

Questa dovrebbe essere la risposta accettata – kgiannakakis

+0

Soluzione perfetta! –

Problemi correlati