2017-07-01 1 views
-1

メインビューsequeのdetailViewControllerを表示しようとしています。Iプッシュ通知をタップした後でdetailedViewControllerを開くことができません

appdelegate.swiftの関連部分:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], 
       fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
    print("Notification: basic delegate (background fetch)") 
    // If you are receiving a notification message while your app is in the background, 
    // this callback will not be fired till the user taps on the notification launching the application. 
    // Print message ID. 
    if let messageID = userInfo[gcmMessageIDKey] { 
     print("2 Message ID: \(messageID)") 
     print("2 message : \(userInfo)") 
     openPostDetail(userInfo: userInfo) 
    } 
    completionHandler(UIBackgroundFetchResult.newData) 
} 

func openPostDetail(userInfo: [AnyHashable: Any]) { 


    let post_id = userInfo["post_id"] as? String 

    print("post_id: \(post_id) ") 

    let baseURL = URL(string: websiteUrl) 
    //89169?_embed 
    let postId = "89169" 
    let url = URL(string: "wp-json/wp/v2/posts/\(postId)?_embed", relativeTo: baseURL) 
    var newPost :Posts? 
    URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in 
     guard let data = data, error == nil else { return } 
     do { 
      let json: NSDictionary = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! NSDictionary 
      newPost = Posts(post: json) 
      print("author: \(String(describing: newPost?.postAuthor))") 


      let sb = UIStoryboard(name: "Main", bundle: nil) 
      let rootVC = sb.instantiateViewController(withIdentifier: "mainView") as! NewsViewController 
      let otherVC = sb.instantiateViewController(withIdentifier: "postDetailIdn") as! PostDetailTableViewController 
      otherVC.post = newPost! 

      rootVC.navigationController?.pushViewController(otherVC, animated: true) 



     } catch let error as NSError { 
      print(error) 
     } 
    }).resume() 


} 

` 私はプッシュ通知をタップ私はこのエラーを取得: は、メインスレッドまたはWeb以外のスレッドからのWebロックを取得しようとしました糸。これは、セカンダリスレッドからUIKitを呼び出した結果である可能性があります。今すぐクラッシュする...

ありがとうございます。

答えて

0

メインスレッドのUIを更新する必要があります。

DispatchQueue.main.async { 
     let sb = UIStoryboard(name: "Main", bundle: nil) 
        var navigationController = UIApplication.sharedApplication().keyWindow?.rootViewController as! UINavigationController 
        let otherVC = sb.instantiateViewController(withIdentifier: "postDetailIdn") as! PostDetailTableViewController 
        otherVC.post = newPost! 

        navigationController?.pushViewController(otherVC, animated: true) 
} 
+0

ありがとうございます、このソリューションは私のエラーを修正することができます。私は新しい問題に出会った。私のアプリケーションでは何も変わりません。まだ私は私のpostDetailViewControllerを見ることができません。最後のコマンドを変更した場合、この1つのself.window?.rootViewController = rootVC; ナビゲーションバーのないpostDetailedViewControllerが見えます。しかし、私はメインのポストリストのコントローラに戻ってくるナビゲーションバーが必要です.. – Ahmeric

+0

self.window?.rootViewController = rootVC これをした場合、あなたのナビゲーションコントローラは破壊されます、あなたが直面している問題は何ですか? –

+0

ナビゲーションバーでpostDetailViewControllerを開きたいと思います。ポストアイテムの1つがmainViewControllerのポストリストでクリックされたときのように開かれます。 あなたが 'self.window?.rootViewController = rootVC;'を使うと言ったように、ナビゲーションバーが消える – Ahmeric

関連する問題