I’ve a native app and it has a webView screen which loads some contents from URL(dynamic).
So, webView asks/send messege to native to select image and provided those URL.
For this, I’m using PHPickerViewController to select image from gallary, copy file/s using following function and save file/s to document folder in app.
picker.delegate = nil
picker.dismiss(animated: true) { [weak self] in
let dispatchGroup: DispatchGroup = DispatchGroup()
var orderIDArr: [String] = []
var orderMap = [String : String]()
for result in results {
let uniqueID = result.assetIdentifier ?? UUID().uuidString
orderIDArr.append(uniqueID)
dispatchGroup.enter()
result.itemProvider.loadFileRepresentation(forTypeIdentifier: UTType.image.identifier) { url, error in
if let url = url{
let fileName = url.lastPathComponent
orderMap[uniqueID] = url.absoluteString
guard let destFileURL = self!.getDocumentsDirectory()?.appendingPathComponent(fileName) else {
return
}
NSLog("new location of, sourceFileURL:(url) destFileURL:(destFileURL)")
do {
/// even convert image to data and then write
//try data.write(to: destFileURL)
try FileManager.default.moveItem(at: url, to: destFileURL)
} catch {
print("Error = (error)")
}
NSLog("url:(url)")
dispatchGroup.leave()
} else {
dispatchGroup.leave()
}
}
}
dispatchGroup.notify(queue: .main) { [weak self] in
var arrMedia: [String] = []
for url in orderIDArr {
arrMedia.append(url)
}
self?.delegate?.pickedMedias(orderMap) /// here i send URL to webView using evaluateScript() , no need to mention pickedMedias() function.
}
}
Now come to main problem, when I send URL(path of my document directory) to webView, it can load for the first time.
In the second time, If I select the same image, and same moveItem function is call, it shows an error that file is already exist in the path. This is normal behaviour.
try FileManager.default.moveItem(at: url, to: destFileURL)
But the problem is, as I have the image in document and WebView tries to reload that, It can not reload the file from the second attemps. Irony is, Image file also get deleted from document directory after a few seconds automatically. I don’t call any delete function.
So summary is:
- Only first time when image is saved to local and webView can load image from document directory.
- From the second time, the image gets deleted from document and webView could not load it.
- For testing, If I just copy file to document directory, it does not delete any file. The file gets deleted when I send URL/path to WebView from the second time.
By the way, i’ve added all the permission in Plist.
Example URLs are:
file:///Users/jamshed/Library/Developer/CoreSimulator/Devices/BC52266D-9FA5-4121-AD90-B52D95737554/data/Containers/Data/Application/06A268CB-80F6-4626-94E1-43111D225615/tmp/.com.apple.Foundation.NSItemProvider.dbD1nE/IMG_0005.jpeg
file:///Users/jamshed/Library/Developer/CoreSimulator/Devices/BC52266D-9FA5-4121-AD90-B52D95737554/data/Containers/Data/Application/06A268CB-80F6-4626-94E1-43111D225615/Documents/IMG_0005.jpeg
So, my question,
Is there any reason or problem when webView try to loads image from document ?
Or Is there any rules to provide path?
Or the PHPickerViewController -> loadFileRepresentation function is wrong for this scenario?
Advance tons of thanks if someone could help me. I’m just struggling for this 🙁