首页 > 解决方案 > UIWebView 从 firebase 数据库调用 URL

问题描述

我不知道这是否是我只是缺少一个明显的解决方案或什么的情况,但我希望你能指出我正确的方向。

我有一个 firebase 数据库,它提供文章的信息,例如名称、img 等,它还保存显示实际文章的网站 URL,我正在使用 UIWebView 来显示带有相关文章的网站。

我现在让 WebView 按预期工作,但是 URL 是硬编码的,因此只显示一篇文章,我正在了解如何让文章 URL 从 firebase 数据库传递到我的代码中并使用WebView 并根据用户按下的文章显示不同的文章。

我已经包含了处理下面的 firebase 请求的代码以及设置 WebView 的代码。

任何帮助,将不胜感激。

    func fetchArticles() {

    collectionView.dataSource = self
    collectionView.delegate = self

    let db = Firestore.firestore()



    db.collection("articles").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                let article = Article()
                self.mapArticle(document: document, article: article)
                self.articles.append(article)
            }

            self.collectionView.reloadData()
        }
    }

}

func mapArticle(document: QueryDocumentSnapshot, article: Article) {
    var data = document.data()
    article.title = data["title"].toString
    article.ArticleImageName = data["article_Img"].toString
    article.leauge = data["leauge"].toString
    article.authorProfilePic = data["author_img"].toString
    article.uploadDate = data["upload_Date"].toString
    article.URL = data["URL"].toString
    //...
}




func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
    return articles.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as! hubArticle

    let article = articles[indexPath.row]

    cell.titleLable.text = article.title
    cell.authorProfileView.loadImageUsingUrlString(urlString: article.authorProfilePic)
    cell.subTitleLabel.text = article.leauge
    cell.hubArticleThumbnailImageView.loadImageUsingUrlString(urlString: article.ArticleImageName)

    return cell
}

这是 WebView 的代码

    //sets up webview
 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    webView = UIWebView(frame: UIScreen.main.bounds)
    webView.delegate = self
    self.addSubview(webView)
    if let url = URL(string: "https://afu.moebn.com/good-week-bad-week-bafa-nl-2018-week-twelve/") {
        let request = URLRequest(url: url)
        webView.loadRequest(request)
    }
}

标签: iosswiftfirebaseuiwebviewuiwebviewdelegate

解决方案


推荐阅读