【问题标题】:I Can not open a detailedViewController after tapping the push notification点击推送通知后我无法打开详细视图控制器
【发布时间】:2017-07-01 21:14:49
【问题描述】:

我正在尝试显示主视图序列的 detailViewController。

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 lock。这可能是从辅助线程调用 UIKit 的结果。现在崩溃了...

提前谢谢你

【问题讨论】:

    标签: ios swift firebase push-notification


    【解决方案1】:

    您需要在主线程上更新 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)
    }
    

    【讨论】:

    • 谢谢,这个解决方案可以修复我的错误。但我遇到了一个新问题。我的应用程序没有任何变化。我仍然看不到我的 postDetailViewController。如果我改变最后一个命令,用这个 self.window?.rootViewController = rootVC;我可以看到没有导航栏的 postDetailedViewController。但我还需要导航栏才能返回主帖子列表控制器..
    • self.window?.rootViewController = rootVC 如果你这样做了,那么你的导航控制器就会被破坏,你遇到了什么问题,解释一下。
    • 我想用导航栏打开 postDetailViewController。当它在 mainViewController 的帖子列表中单击其中一个帖子项目时打开。正如你所说,当我使用self.window?.rootViewController = rootVC; 时,导航栏消失了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    相关资源
    最近更新 更多