2017-07-01 51 views
-1

我想顯示一個主視圖seque的detailViewController。 appdelegate.swift的我點擊推送通知後無法打開detailedViewController

相關的部分:

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() 


} 

` 當我在推送通知挖掘我得到這個錯誤: 試圖從比主線程或網絡中的其他線程獲取網絡鎖定線。這可能是從輔助線程調用UIKit的結果。現在崩潰...

感謝您提前

回答

0

您需要更新主線程上的用戶界面。

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。如果我改變最後一個命令,用這個self.window?.rootViewController = rootVC; 我可以看到沒有導航欄的postDetailedViewController。但我需要導航欄還回到主要帖子列表控制器.. – Ahmeric

+0

self.window?.rootViewController = rootVC 如果你這樣做,那麼你的導航控制器將被銷燬,你面臨的問題是什麼,解釋它。 –

+0

我想用導航欄打開postDetailViewController。當它在mainViewController的發佈列表中單擊其中一個發佈項時打開。正如你所說當我使用'self.window?.rootViewController = rootVC;'導航欄消失 – Ahmeric

相關問題