ios - 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)
}
}
解决方案
推荐阅读
- arrays - Laravel 雄辩,其中定义的值介于数据库中的 json 范围值之间
- php - 搜索栏的多个字段不起作用
- reactjs - 我应该通过 Id 从 API 获取还是过滤获取的数据
- html - 为什么该函数在我的每个组件中运行两次
- c++ - C++ - 为什么这种使用地图的斐波那契记忆的实现如此缓慢?
- r - 如何将标题添加到多个列
- string - “换行”和“换行”之间的技术区别?
- angular - 在 Rxjs 6 中重写 Rxjs 4
- firebase - 在具有唯一数据库 ID 的 Firebase 实时数据库中创建数据库
- wordpress - 我想知道是否有插件或如何做,以便该多站点网络上的站点在特定日期