0

This is how I load my wkwebview:

    let url = URL(fileURLWithPath: Bundle.main.path(forResource: "webviews/helloworld", ofType: "html")!)
    self.webview!.loadFileURL(url, allowingReadAccessTo: url)

All is fine on the simulator but when try the app on my iPhone the downloaded images saved in the documents directory are not loaded.

This is how I get the file url of the image.

    let fileManager = FileManager.default
    let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let pathComponent = filename

    return directoryURL.appendingPathComponent(pathComponent).absoluteString

This would return something like this: file:///Users/Joshua/Library/Developer/CoreSimulator/Devices/9FE87399-6EBD-4DF3-BC6A-FD844DF62833/data/Containers/Data/Application/C1E250A4-823E-4590-8BDE-3891666CA728/Documents/57a8dd7255723c964658262d43c169c1

I have the same problem as this guy: WKwebview : Cannot view app documents images in app web view iOS swift

Community
  • 1
  • 1
blake
  • 11
  • 7
  • can you also publish a piece of html file? where external resources are declared – heximal Dec 13 '16 at 10:03
  • the problem may be in filename case-sensitivity. Simulator is case-insensetive, but real iOS devices are case sensetive – heximal Dec 13 '16 at 10:05
  • i mean, if you mention some resource like – heximal Dec 13 '16 at 10:07
  • @heximal something like – blake Dec 13 '16 at 10:16
  • @heximal 57a8dd7255723c964‌​658262d43c169c1 is a the filename of the image – blake Dec 13 '16 at 10:17
  • 1
    this is wrong, you can't know absolute path unless you construct html file on-the-fly. my advice: use relative paths. in other words, use just , webView will do the rest – heximal Dec 13 '16 at 10:25
  • 1
    and of course, file:///Users/Joshua/Library/Developer/CoreSimulator/De‌​vices/9FE87399-6EBD-‌​4DF3-BC6A-FD844DF628‌​33/data/Containers/D‌​ata/Application/C1E2‌​50A4-823E-4590-8BDE-‌​3891666CA728/Documen‌​ts/ on the device will be completely different – heximal Dec 13 '16 at 10:27
  • @heximal sorry. I add the src part using javascript img.attr("src", path); – blake Dec 13 '16 at 10:32
  • @heximal I also use scriptMessageHandlers in conjunction with evaluateJavaScriptmethod to pass the path from swift to javascript. – blake Dec 13 '16 at 10:33
  • I see no reason why you should do it so complicated. use relative paths, Luke) – heximal Dec 13 '16 at 11:01
  • They have different file paths i believe – blake Dec 14 '16 at 01:43

2 Answers2

0
func getDirectoryPath() -> String {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory
}
func getImage(){
        let fileManager = NSFileManager.defaultManager()
        let imagePAth = (self.getDirectoryPath() as NSString).stringByAppendingPathComponent("yourImageName.jpg")// Your image name must be same as when you save image in directory 
        if fileManager.fileExistsAtPath(imagePAth){
            self.imageView.image = UIImage(contentsOfFile: imagePAth)
            // or do whatever you want with image. 
        }else{
            print("No Image")
        }
    }
0

So what I did to fix the problem is to transfer the webviews directory to the /Libraries directory and store the photos I downloaded to /Libraries/Caches

    let functionName:String = "moveFileToLibrary"
    let library = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0]
    let bundle = Bundle.main.path(forResource: "webviews", ofType: "")!

    let fileManager = FileManager.default

    if fileManager.isWritableFile(atPath: library.path)
    {
        if self.debug {print("\(functionName) @ \(self.className) => \(library.path) : File is writable")}
    }
    else
    {
        if self.debug {print("\(functionName) @ \(self.className) => \(library.path) : File is read-only")}
    }

    if fileManager.isWritableFile(atPath: bundle)
    {
        if self.debug {print("\(functionName) @ \(self.className) => \(bundle) : File is writable")}
    }
    else
    {
        if self.debug {print("\(functionName) @ \(self.className) => \(bundle) : File is read-only")}
    }

    if !fileManager.fileExists(atPath: library.appendingPathComponent("webviews").path)
    {
        do
        {
            try fileManager.copyItem(atPath: bundle, toPath: library.appendingPathComponent("webviews").path)
            if self.debug {print("\(functionName) @ \(self.className) => Webviews folder copied!")}
        }
        catch let error
        {
            if self.debug {print("\(functionName) @ \(self.className) => Error Writing Webviews folder: \(error.localizedDescription)")}
        }
    }
    else
    {
        if self.debug {print("\(functionName) @ \(self.className) => Webviews folder exists. Continue woth life.")}
    }
blake
  • 11
  • 7