0

私のアプリには 2 種類の初期状態があります。

  1. ログイン
  2. ダッシュボードとその他のものを含むログイン後。

そのようにAppdelegateからビューを切り替えると、私のアプリは正常に動作します

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        let userDataExist = CommonUserFunction.isUserDataExist() as Bool

        if userDataExist == true {

            let homeViewController = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "HomeView") as? HomeViewController
            let navigationController = UINavigationController()
            navigationController.viewControllers = [homeViewController!]

            self.window!.rootViewController = navigationController
        }
        else{

            let loginController = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "LoginView") as? LoginController
            let navigationController = UINavigationController()
            navigationController.viewControllers = [loginController!]

            self.window!.rootViewController = navigationController
        }

        self.window?.makeKeyAndVisible()

        return true
 }

プッシュ通知を処理する必要があるときに問題に直面しています。これらの方法でiOS 10でプッシュ通知を受け取りました

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,  willPresent notification: UNNotification, withCompletionHandler   completionHandler: @escaping (_ options:   UNNotificationPresentationOptions) -> Void) {
    print("Handle push from foreground")
    // custom code to handle push while app is in the foreground

    print(notification.request.content.userInfo)
}

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

   // custom code to handle push while app is in the foreground
    print(response.notification.request.content.userInfo)
}

通知トレイから通知をタップすると、これらの 2 つのメソッドが起動されますが、アプリはフォアグラウンドまたはバックグラウンドです。通知をタップした後、詳細ページを表示する必要があります。didFinishLaunchingWithOptions メソッドで以前に行ったような詳細ページを設定しようとしました

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

    let pushResponse = response.notification.request.content.userInfo as NSDictionary

    let rtype = pushResponse.object(forKey: "rtype") as? String
    let rid = pushResponse.object(forKey: "rid") as? String

    if rtype != nil {

        if rtype == "5" {

            if rid != nil {
                 let noticeDetailsViewController = self.storyboard?.instantiateViewController(withIdentifier: "NoticeDetailsView") as? NoticeDetailsController
                 noticeDetailsViewController!.id = rtype!
                 noticeDetailsViewController?.fromPush = true
                 let navigationController = UINavigationController()
                 navigationController.viewControllers = [noticeDetailsViewController!]
                 self.window!.rootViewController = navigationController
            }
        }
    }
}

そのたびにアプリがクラッシュします。これを克服する方法。助けてください。前もって感謝します。

4

1 に答える 1