1

i am using WKWebview to show my local htm file. Everything was good, but when i added local picture to my htm files, pictures didn't seem. Lots of stackoverflow user had similar problem but there is no clear answer. here is my code ;

    var a = dic["adi"]?.asString()
    var path = NSBundle.mainBundle().pathForResource(a, ofType: "htm" )
    var url = NSURL(fileURLWithPath:path!)
    var request = NSURLRequest(URL:url!)

    var theConfiguration = WKWebViewConfiguration()

    theWebView = WKWebView(frame:self.view.frame, configuration: theConfiguration)

    var error:NSError?
    let text2 = String(contentsOfFile: path!, encoding:NSUTF8StringEncoding, error: &error)

    if let theError = error
    {
        print("\(theError.localizedDescription)")
    }

    theWebView!.loadHTMLString(text2!, baseURL: nil)
    self.view.addSubview(theWebView!)

i hope you can help me. i am waiting your answers. thank you.

2 Answers 2

2

It's a bug in WKWebView.

You can wait for 8.2 with fixes or use dirty hacks (local HTTP server).

Sign up to request clarification or add additional context in comments.

Comments

2

The trick to display local images within local HTML files with WKWebView is defining the baseURL within the loadHTMLString method.

My code example is in Objective-C but you will get the idea:

    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    NSURL *baseURL = [NSURL fileURLWithPath: resourcePath];

    [self.webView loadHTMLString: htmlBodyString baseURL: baseURL ];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.