【发布时间】:2020-08-15 11:20:32
【问题描述】:
我正在尝试从 firebase 数据库中获取 pdf 的 URL,并通过 UIButton (iOS) 将该 URL 显示为 PDF。 我正在尝试不同的方法,但我无法让它发挥作用。 任何帮助将不胜感激
import UIKit
导入 SafariServices 导入 Firebase 数据库 导入 Firebase 导入 WebKit
类 ModelsInfoController: UIViewController , UIWebViewDelegate,UIScrollViewDelegate, WKUIDelegate { var WKwebView: WKWebView! var dbRef:数据库参考! var nextUrl = "https://www.google.co.uk/"
let infoBttn: UIButton = {
let infoBttn = UIButton (type: .system)
infoBttn.setTitle("More Info", for: .normal)
infoBttn.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: .normal)
infoBttn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
infoBttn.addTarget(self, action: #selector(retrieveUrl), for: .touchUpInside)
return infoBttn
}()
@objc func retrieveUrl () {
dbRef = Database.database().reference().child("models")
dbRef.observe(.value) {
(snapshot) in
let value = snapshot.value as! NSDictionary
let url = value["url"]!
self.nextUrl = (url as! String)
print (self.nextUrl)
print ("webview is about to load")
let request = URLRequest (url: URL(string: self.nextUrl)!)
self.WKwebView.load(request)
self.WKwebView.addObserver(self, forKeyPath: #keyPath(WKWebView.isLoading), options: .new, context: nil)
}
print ("function observe is called")
}
我收到了这个错误:
无法将“NSNull”(0x102d85dd0)类型的值转换为“NSDictionary”(0x102d856c8)
它没有从数据库中获取正确的 URL(对于不同的模型,有不同的 URL)。
我修复了这个错误,但是当我按下按钮时仍然没有打开 pdf。
【问题讨论】:
-
你能发布你当前的代码吗?
-
@CongNguyen 我做到了,谢谢
-
show that URL as a PDF via UIButton是什么意思? -
@CongNguyen 我想从数据库中获取 url,当按下按钮时显示该特定孩子的 pdf
-
你想用WKWebView来显示PDF内容对吧?
标签: ios swift firebase firebase-realtime-database