8

リモート通知を受け取り、通知の種類に応じてナビゲーションコントローラのビューコントローラを変更します。アプリが実行されていないときにリモート通知を処理するとクラッシュする

アプリがフォアグラウンドにあるとき、またはアプリがバックグラウンドにあるが完全に閉じられていないとき(マルチタスクバーから)はすべて正常に機能する。

しかし、アプリが終了し、リモート通知を受け取ると、アプリが開くとすぐにクラッシュします。 ViewControllerをセットアップする方法が間違っていますか?

ここにいくつかのコードがあります。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary 
*)launchOptions { 
    // Push required screens into navigation controller 

     UILocalNotification *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; 

    //Accept push notification when app is not open 
    if (remoteNotif) {  
     [self handleRemoteNotification:application userInfo:remoteNotif.userInfo]; 
     return YES; 
    } 

    [window addSubview:navigationController.view]; 
    [window makeKeyAndVisible]; 

    return YES; 
} 

-(void) handleRemoteNotification:(UIApplication *)application userInfo:(NSDictionary *)userInfo { 
    application.applicationIconBadgeNumber = 0; 

NSMutableArray *viewControllers = [NSMutableArray array]; 
    [viewControllers addObject:driverWaitViewController]; 
    [viewControllers addObject:newJobsViewController]; 

    [navigationController setViewControllers:viewControllers]; 
} 

答えて

15

を私はこれが解決だ、と私は思ったようには、ビュー・コントローラとは何の関係もありません。

問題は次のとおりです。 remoteNotif自体ではなく、remoteNotif.userInfoで送信していました。また、remoteNotifは明らかにUILocalNotification型ではありません。これはNSDictionaryオブジェクトです。

UILocalNotification *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; 

[self handleRemoteNotification:application userInfo:remoteNotif.userInfo]; 

あなたはXcodeのデバッグモードから起動するアプリを閉じた場合、及び場合アプリはプッシュ通知(クローズドアプリ)を起動したときに

NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; 

[self handleRemoteNotification:application userInfo:remoteNotif]; 
2

通知を受け取ったときにアプリケーションを適切に初期化していません。これに方法:didFinishLaunchingWithOptions:アプリケーションを変更し

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions { 
    // Push required screens into navigation controller 

    NSDictionary *notif= [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; 

    [window addSubview:navigationController.view]; 
    [window makeKeyAndVisible]; 

    //Accept push notification when app is not open 
    if (notif) {  
     [self handleRemoteNotification:application userInfo:notif]; 
    } 

    return YES; 
} 
+0

返信いただきありがとうございます。しかし何か他の問題。どうやって解決したのか見てください。 – Prasanna

+0

ええ、私はそれが奇妙だと思ったが、私はそれに追いつかなかった。ごめんなさい。 – vakio

7

であるべきあなたの携帯電話は、Macに接続されている(xcodeのデバッグモードではまだあなたの携帯電話)クラッシュされます。電話を抜いてこのシナリオをテストしてください。

+0

これは今私の問題を解決しましたが、それはなぜですか?私には意味がない – Evils

+0

あなたは私のためにも意味がないと言った。私はここでデバイスとMacとの接続を考えています。 – damithH

+0

これは私のために働いた!絶対に意味をなさない。 –

関連する問題