0

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)
    }
}
farmerhnf
  • 13
  • 1
  • 2
  • What happens when you try to use the database's URL, i.e. _what's the error_? How/where/when do you set the URL on the web view? Are you on the main thread when changing the URL? Are you sure the db contains valid URLs? – dr_barto Aug 21 '18 at 17:49
  • is set the URL with this line "if let url = URL(string: "https://afu.moebn.com/good-week-bad-week-bafa-nl-2018-week-twelve/")" however the issue that i'm facing is that i cant think how to be able to input the different urls that are called from firebase, as there is a different URL for every article. I was able to do this with all the other aspects that were given in the mapArticles function which lets me put all the imgs etc into the collection view but I cant think how to do something similar for the URL's – farmerhnf Aug 21 '18 at 19:10

0 Answers0