我正在尝试从 WKWebview 下载文件并显示 iOS 共享表,以便用户可以决定如何处理该文件。在浏览器中一切都按预期工作。
我的 iOS 代码以前可以工作,但似乎不再工作了(有一段时间没有测试,但我现在使用的是 iOS 16.4)。共享表正确弹出,我可以保存文件。但是,文件似乎没有正确保存到系统中,因为它在 downloadDidFinish 函数中打印“Optional(0 bytes)”,下载的文件也是 0。
我的代码以及错误如下。
var filePathDestination: URL?
func download(_ download: WKDownload, decideDestinationUsing response: URLResponse, suggestedFilename: String, completionHandler: @escaping (URL?) -> Void) {
let temporaryDir = NSTemporaryDirectory()
let now = NSDate()
let nowTimeStamp = getTimeStamp(dateToConvert: now)
let fileName = temporaryDir + nowTimeStamp + suggestedFilename
let url = URL(fileURLWithPath: fileName)
filePathDestination=url
completionHandler(url)
}
func downloadDidFinish(_ download: WKDownload) {
let data = try? Data(contentsOf: filePathDestination!)
print(data)
let items = [filePathDestination!]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
ac.modalPresentationStyle = UIModalPresentationStyle.popover
ac.preferredContentSize = CGSize(width: self.view.bounds.size.width/2.0, height: self.view.bounds.size.height/2.0)
ac.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
ac.popoverPresentationController?.sourceView = self.view;
let midx=self.view.bounds.midX;
let midy=self.view.bounds.midY;
let rect=CGRect(x: 0.5*midx, y: 0.5*midy, width: midx, height: midy);
ac.popoverPresentationController?.sourceRect = rect;
ac.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.size.width / 2.0, y: self.view.bounds.size.height / 3.0, width: 1.0, height: 1.0)
present(ac, animated: true, completion: nil)
}
错误:
[ShareSheet] 无法为文件请求默认共享模式20FC-4269-B6B5-19F07EC1A41B/tmp/20230421%20175423%20filename.png error:Error Domain=NSOSStatusErrorDomain Code=-10814 "(null)" UserInfo={_LSLine=1538, _LSFunction=runEvaluator}
[ShareSheet] 只支持CKShare和SWY类型的加载选项
[ShareSheet] 错误获取 URL 项目:file:///Users/user/Library/Developer/CoreSimulator/Devices/5FD5D8D3-9C13-4DFB-A434-119B430C29B7/data/Containers/Data/Application/B4232467-20FC-4269- B6B5-19F07EC1A41B/tmp/20230421%20175423%20filename.png : (null)
[ShareSheet] 错误获取 URL 的文件提供程序域:file:///Users/user/Library/Developer/CoreSimulator/Devices/5FD5D8D3-9C13-4DFB-A434-119B430C29B7/data/Containers/Data/Application/B4232467-20FC- 4269-B6B5-19F07EC1A41B/tmp/20230421%20175423%20filename.png : (null)
[ShareSheet] 加载文档 URL 的元数据时出错:file:///Users/user/Library/Developer/CoreSimulator/Devices/5FD5D8D3-9C13-4DFB-A434-119B430C29B7/data/Containers/Data/Application/B4232467-20FC-4269- B6B5-19F07EC1A41B/tmp/20230421%20175423%20filename.png 错误:错误域=NSFileProviderInternalErrorDomain 代码=0 “从 URL 文件中找不到有效的文件提供程序:///Users/user/Library/Developer/CoreSimulator/Devices/5FD5D8D3-9C13 -4DFB-A434-119B430C29B7/data/Containers/Data/Application/B4232467-20FC-4269-B6B5-19F07EC1A41B/tmp/20230421%20175423%20filename.png。” UserInfo={NSLocalizedDescription=未从 URL file:///Users/user/Library/Developer/CoreSimulator/Devices/5FD5D8D3-9C13-4DFB-A434-119B430C29B7/data/Containers/Data/Application/B4232467-20FC 找到有效的文件提供程序-4269-B6B5-19F07EC1A41B/tmp/20230421%20175423%20filename.png.}