1

ローカルの通知で作業していますが、特定のviewControllerを表示しようとしていますが、このフォーラムで見つけたものを試してみました。 this picture here: にそしてここAppDelegate.swiftのソースコードは次のとおりです。通知アクションから特定のビューコントローラを表示する

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

    print("didReceive Method called") 

    if response.actionIdentifier == "actionOne" { 
     DispatchQueue.main.async(execute: { 
      self.notificationAction1() 
     }) 
    } else if response.actionIdentifier == "actionTwo" { 
     DispatchQueue.main.async(execute: { 
      self.notificationAction2() 
     }) 

    } else if response.actionIdentifier == "actionThree" { 

    } 
    completionHandler() 
} 

func notificationAction1() { 
    redirectToVC() 
} 

func redirectToVC() { 
    let toVC = VersesViewController() 
    if self.window != nil && self.window?.rootViewController != nil { 
     let rootVC = self.window?.rootViewController! 
     if rootVC is UINavigationController { 
      (rootVC as! UINavigationController).pushViewController(toVC, animated: true) 
     } else { 
      rootVC?.present(toVC, animated: true, completion: { 
       //Do something 
      }) 
     } 
    } 
} 

コード(特にredirectToVC()法)と間違って何ですか? 助けていただければ幸いです。

+0

現在のところ動作していますか? – Mannopson

+0

ソースコードの上にある画像を見ると、ビューコントローラは空白になっています。 –

答えて

1

これについての回答が見つかりました。基本的にAppDelegateからのビューコントローラを表示しています。

func redirectToVC() { 
    let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
    let initialViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "versesVC") as UIViewController 
    self.window = UIWindow(frame: UIScreen.main.bounds) 
    self.window?.rootViewController = initialViewController 
    self.window?.makeKeyAndVisible() 
} 

あなたは現在の画面上の特定のビューコントローラを提示する必要があるときはいつでもあなたは、コントローラを表示する場合、それは問題ではないですOpening view controller from app delegate using swift

+0

それは完璧に動作しますか? – Mannopson

+0

私もrootViewControllerをこのように設定しましたが、問題は私のデフォルトのrootViewはランチャーアニメーションビューです。アニメーションが表示され、別のビューにリダイレクトされ、rootViewとして設定されます。しかし、私は私のrootViewを 'didReceive response:'にリセットすると、特定のViewControllerを一目でわかり、古いrootViewControllerに戻ります。古いrootViewControllerに戻らないようにするにはどうすればいいですか? –

0

のおかげで、ナビゲーションに押し通すか、以下使用することができ、両方の場合には、提示される可能性がありますあなたの問題を解決するコード。

    var appdelgateObj = UIApplication.shared.delegate as! AppDelegate 
        if let destinationVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: “ToPresentVC”) as? ToPresentVC { 
         if let window = appdelgateObj.window , let rootViewController = window.rootViewController { 
          var currentController = rootViewController 
          while let presentedController = currentController.presentedViewController { 
           currentController = presentedController 
          } 
          currentController.present(destinationVC, animated: true, completion: nil) 
         } 
        } 
関連する問題