UIWebView to call the URL from a firebase database

129 Views Asked by At

I don't know if this is the case of me just missing an obvious solution or what but i'm hoping you can point me in the right direction.

I have a firebase database which provides the information for the articles such as the name, img etc it also hold the website URL which is where the actual article is shown, i'm using a a UIWebView to display the website with the relevant article.

I have the WebView working as expected right now, but the URL is hardcoded in therefore only displaying the one article, and i'm at a pass as to how to get the article URL to pass from the firebase database into my code and work with the WebView and show different articles dependent on what article is pressed by the user.

I have included both the code that handles the firebase request below as well as the code that sets up the WebView.

Any help would be appreciated.

    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
}

and this is the code for the 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)
    }
}
0

There are 0 best solutions below