I am trying to download a file from a WKWebview and show an iOS Share Sheet so that the user can decide what to do with the file. Everything works as expected in the browser.
My iOS code was previously working, but doesn't seem to be working anymore (haven't tested in a while, but I am on iOS 16.4 right now). The share sheet pops up correctly and I am able to save the file. However, it appears that the file is not being saved to the system correctly because it prints "Optional(0 bytes)" in the downloadDidFinish function, and downloaded file is 0 as well.
My code as well as the errors are below.
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)
}
Errors:
[ShareSheet] Failed to request default share mode for fileURL: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 error:Error Domain=NSOSStatusErrorDomain Code=-10814 "(null)" UserInfo={_LSLine=1538, _LSFunction=runEvaluator}
[ShareSheet] Only support loading options for CKShare and SWY types.
[ShareSheet] error fetching item for 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] error fetching file provider domain for 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] error loading metadata for documentURL: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 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=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.}