WKWebView - load gallery pictures

Hi,


I use the last Xcode last version, and Swift for create an application but I have a little problem.

Application interface is make with HTML load in WKWebView, and I use javascript to dialog between the core's app and the html (UI).


The app transfert an array of string containing image's absolute path (get with requestContentEditingInput) to the HTML. Goal is to show images in the HTML page.

So I get absolute path to image (like file:///var/mobile/Media/DCIM/100APPLE/IMG_0002.PNG) , but HTML page don't show it, show simply a blank picture...

I think it's security problem, but how to show this pictures??

Can you help me ?

I try to authorize access picture for WKWebView like this :


let urlacces = URL(string:"file:///var/mobile/Media/DCIM/100APPLE/")!
webview.loadFileURL(url, allowingReadAccessTo: urlacces)


Thanks,

The files must be in the document directory.

I implemented the following to retrieve a document:


        let documentDirUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
        let fileNameWithExtension = "IMG_0002.PNG"
        let indexFileUrl = documentDirUrl.appendingPathComponent(fileNameWithExtension)
        if FileManager.default.fileExists(atPath: indexFileUrl.path) {
            webView.loadFileURL(indexFileUrl, allowingReadAccessTo: documentDirUrl)
        }


for details and how to handle if your images are in subdirectory of documentDirectory by just adding for instance

.appendingPathComponent("100APPLE-APP")

to documentDirUrl


Have a look here

https://forums.developer.apple.com/thread/100125

Hi,


No, look the post's title "gallery pictures", I want to show the Iphone gallery pictures.

So, my html cannot be in the same directory.


Thanks,

I don't think you can access the gallery like this (privacy protection).


You have to ask user to import pictures first, through a call to


        pickMediaFromSource(UIImagePickerController.SourceType.photoLibrary)


and then copy in your app documentDirectory

Accepted Answer

I don"t want copy all pictures in my document directory, it's very bad solution.

I already ask permission to Iphone to access picture for my application.


Maybe I try to get contain of picture base64 encoded and push it to html..it's very ugly but I don't have the choice with Apple .

Sure, if you want to copy all photo, it would be a very bad and tedious solution. That would work for just a small selection.


May be you could try to access via iCloud.

No guarantee, I never used it yet.

Far worse than copying files.

WKWebView - load gallery pictures
 
 
Q