iOS PHPickerResult URL gets invalidated/deleted when image try to display using WKWebView

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:

  1. Only first time when image is saved to local and webView can load image from document directory.
  2. From the second time, the image gets deleted from document and webView could not load it.
  3. 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 🙁

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật