Ios UIWebView从firebase数据库调用URL
我不知道这是不是我错过了一个明显的解决方案,或者什么,但我希望你能给我指出正确的方向 我有一个firebase数据库,它提供了文章的信息,如名称、img等。它还保存了网站URL,这是实际文章的显示位置。我使用UIWebView来显示包含相关文章的网站 我的WebView现在可以正常工作,但是URL是硬编码的,因此只显示一篇文章,我正在考虑如何将文章URL从firebase数据库传递到我的代码中,并使用WebView,根据用户按下的文章显示不同的文章 我已经包括了下面处理firebase请求的代码以及设置WebView的代码 任何帮助都将不胜感激Ios UIWebView从firebase数据库调用URL,ios,swift,firebase,uiwebview,uiwebviewdelegate,Ios,Swift,Firebase,Uiwebview,Uiwebviewdelegate,我不知道这是不是我错过了一个明显的解决方案,或者什么,但我希望你能给我指出正确的方向 我有一个firebase数据库,它提供了文章的信息,如名称、img等。它还保存了网站URL,这是实际文章的显示位置。我使用UIWebView来显示包含相关文章的网站 我的WebView现在可以正常工作,但是URL是硬编码的,因此只显示一篇文章,我正在考虑如何将文章URL从firebase数据库传递到我的代码中,并使用WebView,根据用户按下的文章显示不同的文章 我已经包括了下面处理firebase请求的代码
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)
}
}
当您尝试使用数据库的URL时会发生什么情况,即错误是什么?如何/在何处/何时在web视图上设置URL?更改URL时,您是否处于主线程上?是否确定数据库包含有效的URL?是否使用以下行设置URL“if let URL=URL(字符串:”)“然而,我面临的问题是,我无法思考如何输入从firebase调用的不同URL,因为每篇文章都有不同的URL。我可以用mapArticles函数中给出的所有其他方面来实现这一点,该函数允许我将所有IMG等放在集合视图中,但我想不出如何为URL的