gpt4 book ai didi

ios - Swift - 在 ios 13 上下载文件错误

转载 作者:行者123 更新时间:2023-11-28 05:37:29 37 4
gpt4 key购买 nike

我有一个可以从 WebView 下载文件的应用程序。它在 ios 12 中运行良好,但无法正常工作。我遇到了错误

Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.

This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.

这是我的 view.controller 代码:

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {
print(request.url as Any)
if request.url!.absoluteString.range(of: "/download/") != nil {
let extention = request.url!.absoluteString.slice(from: "&fileextension=", to: "&")?.lowercased()
var name = request.url!.absoluteString.slice(from: "&name=", to: "&")?.lowercased()
name = name?.replacingOccurrences(of: "+", with: " ")
DownlondFromUrl(request.url! as URL,name!, extention!)
return false
}
return true
}
func DownlondFromUrl(_ url: URL,_ name:String,_ extensionfile:String){
// Create destination URL
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
let destinationUrl = documentsUrl!.appendingPathComponent(name + ".\(extensionfile)")

//Create URL to the source file you want to download
let fileURL = url

let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)

let request = URLRequest(url:fileURL)

let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
let dataFromURL = NSData(contentsOf: tempLocalUrl)
dataFromURL?.write(to: destinationUrl, atomically: true)

let alert = UIAlertController.init(title: "Download", message: "File download Successful. Do you want open file ", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Open", style: .default , handler:{ (UIAlertAction)in
let fileBrowser = FileBrowser()
self.present(fileBrowser, animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "Share", style: .default , handler:{ (UIAlertAction)in

let activityViewController = UIActivityViewController(activityItems: [destinationUrl], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler:{ (UIAlertAction)in
}))

self.present(alert, animated: true, completion: {
print("completion block")
})
} else {
print("Error took place while downloading a file. Error description: %@", error?.localizedDescription as Any);
}
}
task.resume()
}
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.isNavigationBarHidden = true;
}

我找到了这篇文章 ( Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread ),但我是一个初学者,不确定如何实现它。

最佳答案

您必须在主线程上运行任何访问 UI 的代码。

由于 URLSession 任务在后台线程上运行,您必须添加一个 DispatchQueue block

DispatchQueue.main.async {
let alert = UIAlertController.init(title: "Download", message: "File download Successful. Do you want open file ", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Open", style: .default , handler:{ action in
let fileBrowser = FileBrowser()
self.present(fileBrowser, animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "Share", style: .default , handler:{ action in

let activityViewController = UIActivityViewController(activityItems: [destinationUrl], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel)

self.present(alert, animated: true, completion: {
print("completion block")
})
}

注意:

UIAlertAction 闭包中的参数必须是实例而不是类型。如果未使用该参数,您可以将其替换为下划线字符(例如 handler:{ _ in)

关于ios - Swift - 在 ios 13 上下载文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58180874/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com