swift 使用WKDownload从WKWebview处理文件下载

kq0g1dla  于 2023-04-28  发布在  Swift
关注(0)|答案(1)|浏览(312)

我正在尝试从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]无法请求文件的默认共享模式URL:file:/Users/user/Library/Developer/CoreSimulator/Devices/5 FD 5D 8D 3 - 9 C13 -4DFB-A434- 119 B430 C29 B7/data/Containers/Data/Application/B4232467- 20 FC-4269-B6 B5 - 19 F07 EC 1A 41 B/tmp/20230421%20175423%20filename。png错误:错误域=NSOSStatusErrorDomain Code=-10814“(null)”UserInfo={_LSLine=1538,_LSFunction=runEvaluator}
[ShareSheet]仅支持CKShare和SWY类型的加载选项。
[ShareSheet]获取URL的项目时出错:file:///Users/user/Library/Developer/CoreSimulator/Devices/5 FD 5D 8D 3 - 9 C13 -4DFB-A434- 119 B430 C29 B7/data/Containers/Data/Application/B4232467- 20 FC-4269-B6 B5 - 19 F07 EC 1A 41 B/tmp/20230421%20175423%20filename。png:(null)
[ShareSheet]获取URL的文件提供程序域时出错:file:///Users/user/Library/Developer/CoreSimulator/Devices/5 FD 5D 8D 3 - 9 C13 -4DFB-A434- 119 B430 C29 B7/data/Containers/Data/Application/B4232467- 20 FC-4269-B6 B5 - 19 F07 EC 1A 41 B/tmp/20230421%20175423%20filename。png:(null)
[ShareSheet]加载documentURL的元数据时出错:file:///Users/user/Library/Developer/CoreSimulator/Devices/5 FD 5D 8D 3 - 9 C13 -4DFB-A434- 119 B430 C29 B7/data/Containers/Data/Application/B4232467- 20 FC-4269-B6 B5 - 19 F07 EC 1A 41 B/tmp/20230421%20175423%20filename。png error:Error Domain=NSFileProviderInternalErrorDomain Code=0“No valid file provider found from 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.”UserInfo={NSLocalizedDescription=从URL文件中找不到有效的文件提供程序:///Users/user/Library/Developer/CoreSimulator/Devices/5 FD 5D 8D 3 - 9 C13 -4DFB-A434- 119 B430 C29 B7/data/Containers/Data/Application/B4232467- 20 FC-4269-B6 B5 - 19 F07 EC 1A 41 B/tmp/20230421%20175423%20filename。png.}

mkshixfv

mkshixfv1#

终于找到问题了。这不是上面的swift代码中的问题,实际上是iOS safari上的javascript中的问题。
我在javascript控制台中收到以下警告:
[警告]画布面积超过最大限制(宽度 * 高度〉16777216)。(studio.js,第3593行)
我把这个画布转换成一个dataURL,然后下载它,但是由于这个警告,dataURL是空的,这就是为什么我在最初的问题中看到0 Bytes文件。我需要降低画布的质量,然后一切都按预期工作。
更多信息可以在这里找到:
https://pqina.nl/blog/canvas-area-exceeds-the-maximum-limit/

相关问题