2017-10-25 8 views
0

アプリケーションが終了状態で、プッシュ通知を受け取ったとき。私はアプリを正常に読み込ませたいと思っていますが、閉じるボタンを追加したrootViewControllerにはviewControllerが表示されます。このボタンをタップすると、私は単にviewControllerを却下したいだけです。通知がタップされたときのrootViewController上の現在のviewController

func userNotificationCenter(_ center: UNUserNotificationCenter, 
           didReceive response: UNNotificationResponse, 
           withCompletionHandler completionHandler: @escaping() -> Void) { 
    let tabBarController = self.window!.rootViewController as! UITabBarController 
    self.window?.rootViewController = tabBarController 
    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
    apptVC = storyboard.instantiateViewController(withIdentifier: "NotificationVC") as! NotificationsViewController 
    window?.makeKeyAndVisible() 
    window?.rootViewController?.present(apptVC, animated: true, completion: nil) 
completionHandler() 
    } 
} 

しかし残念ながら、私はアプリがクラッシュすることを実行します。私はこれをやった通知からアプリを開いたときに呼び出されるメソッドで

firebaseを使用してプッシュ通知を実装しました。私はアプリが正常に実行されるようにしたいのですが、ユーザーが通知から来ているときに起動時にルートコントローラに表示コントローラを表示するのと同じです。ここ

UPDATE
私のストーリーボードのスクリーンショットです: enter image description here

+1

エラーメッセージが参考になるお役に立てば幸いです。あなたのストーリーボードと 'makeKeyAndVisible'コードはここに表示されません。どこでも 'didFinishLaunching'にあるはずです。この関数では、最後のコード行のみが必要です。 – Joe

+0

@Joeあなたが示唆したようにコードを追加しましたが、それでも同じです。アプリケーションが終了状態になるとエラーが表示され、そこから実行されていないエラーがxCodeで印刷されることはありません。 –

+0

@ChaudhryTalhaこれは完全に間違っています!あなたは、起動オプションではないデリゲートメソッドが必要になります – Mannopson

答えて

1

だから、最初のタブバーコントローラをinsatiateする必要がありますタブバーコントローラのviewControllersとして:

ナビゲーションコントローラーはこのビューコントローラーが存在する必要がありますか?たとえば:

tabBarController.selectedViewController == firstNavigationController { 
    firstNavigationController.present(NotificationVC, animated: true, completion: nil) 
} 

それは最後のものです:

self.window = UIWindow.init(frame: UIScreen.main.bounds) 
self.window?.rootViewController = tabBarController 
self.window?.makeKeyAndVisible() 

それはちょうど勧告ので、私は、だから私はpresent()という関数を作成している1 UNavigationController

を使用しています。

func present() { 

let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil) 
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController 
let firstNavigationController = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController 
let NotificationVC = storyboard.instantiateViewController(withIdentifier: "NotificationVC") as! NotificationsViewController 

tabBarController.viewControllers = [firstNavigationController] 

tabBarController.selectedViewController == firstNavigationController { 
     firstNavigationController.present(NotificationVC, animated: true, completion: nil) 
} 

self.window = UIWindow.init(frame: UIScreen.main.bounds) 
self.window?.rootViewController = tabBarController 
self.window?.makeKeyAndVisible() 


    } 
} 

しかし、それは有用ではないのですがでも、私は、これは無用だと思います。通知がタップされたときはいつでも対応が必要です。そして、我々はこのようにアクションidentifierをチェックする必要があります:

extension AppDelegate: UNUserNotificationCenterDelegate { 

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

     switch response.actionIdentifier { 
     case UNNotificationDefaultActionIdentifier: 
      self.present() 
      completionHandler() 

     default: 
      break; 
     } 
    } 
} 

は、それがクラッシュから

+0

ありがとうございます:) –

1

たぶんあなたrootViewコントローラがnilです。 appDelegateの終了仕上げでrootviewControllerを指定したとしても、クラスを提示する前にrootview_Controllerを設定してみてください。それらすべてを

let firstNavigationController = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController 

let NotificationVC = storyboard.instantiateViewController(withIdentifier: "NotificationVC") as! NotificationsViewController 

とmake:

let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil) 
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController 

そして、あなたの希望のビューコントローラとUINavigationControllersのすべてをinsatiate:

func userNotificationCenter(_ center: UNUserNotificationCenter, 
            didReceive response: UNNotificationResponse, 
            withCompletionHandler completionHandler: @escaping() -> Void) { 
viewController = storyboard.instantiateViewControllerWithIdentifier("YourRootVC") as UIViewController 
self.window?.rootViewController = viewController 

window?.rootViewController?.present(yourPusHNotificationVC, animated: true, completion: nil) 
    completionHandler() 
     } 
    } 
+0

私のルートビューコントローラはタブバーコントローラです。それでクラスを作るべきですか? –

+0

@ChaudhryTalhaあなたのタブバーコントローラを 'rootViewController'にしてください。 – Mannopson

+0

@Mannopson Jeeson_7が言及したように私が行った更新された質問を見てくださいが、結果はまだ同じです。 –

関連する問題