【发布时间】:2016-10-05 15:56:25
【问题描述】:
(这是我在这里的第一篇文章,对于我所犯的任何错误,我深表歉意) 我有在 iOS 模拟器上完美运行的代码,但是一旦我在真实设备上运行它,它就会在线显示白屏并出现此错误:com.apple.WebKit.WebContent:113:找不到指定的服务
这是我将 pdf 文件从 firebase 下载到设备的代码:
@IBAction func downloadButtonPressed (sender: UIButton) {
print("zzz download button pressed")
let directoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) //FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
if directoryPath.count > 0 {
let documentsDirectory = directoryPath[0]
let pathURL = URL(fileURLWithPath: documentsDirectory)
let fileURL = pathURL.appendingPathComponent("\(fixture.id).pdf")
let httpsReference = FIRStorage.storage().reference(forURL: fixture.onlineManual)
print("zzz urlManual: \(fixture.onlineManual)")
let downloadTask = httpsReference.write(toFile: fileURL) {(URL, error) in
if error != nil {
print("downloadError with pdf")
print(error?.localizedDescription)
} else {
print("zzz donwload successful: \(URL!)")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let newManual = NSEntityDescription.insertNewObject(forEntityName: "OfflineManuals", into: context)
newManual.setValue(self.fixture.id, forKey: "id")
newManual.setValue("\(URL!)", forKey: "manual")
do {
try context.save()
print("zzz manual saved to device")
} catch {
print("zzz error saving manual to core data")
}
}
}
}
}
这是从设备加载文件并将其显示在 WKWebView 中的代码
func loadRequest() {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
if documentsPath.count > 0 {
let documentsDirectory = documentsPath[0]
let restorePath = documentsDirectory + "/\(manualURL!).pdf"
let url = URL(fileURLWithPath: restorePath)
print("zzz url for local file: \(url)")
let req = URLRequest(url: url)
webView.load(req)
}
}
感谢您的阅读!
【问题讨论】:
标签: swift swift3 ios10 xcode8 wkwebview