0

I try:

let body = "<body>.. ANYTHING ..</body>"
self.webView.loadHTMLString(body, baseURL: NSURL(string: "www.google.com")!)

or

self.webView.loadHTMLString(body, baseURL: nil)

If I print:

 self.webView.stringByEvaluatingJavaScriptFromString("document.body.outerHTML")!

This print:

<body></body>

That is not my entire html..

Any ideas?

notice

body is get from

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
                            if error == nil {
    let body = NSString(data: data, encoding: NSUTF8StringEncoding)
}

and yes, is not empty

2 Answers 2

2

if you call

self.webView.stringByEvaluatingJavaScriptFromString("document.body.outerHTML")!

right after calling loadHTMLString is desired behavior because UIWebView loads content asynchronously. Set up UIWebView's delegate in viewDidLoad and then in delegate method call this evaluateJS...

func webViewDidFinishLoad(webView: UIWebView) 
{
    let body = self.webView.stringByEvaluatingJavaScriptFromString("document.body.outerHTML")!
    println(body)
}
Sign up to request clarification or add additional context in comments.

2 Comments

as far as I know - nope. To evaluate this JS String on loaded content you have to make sure, that it's loaded - and only way to make sure that it is loaded it's inside this delegate method
Thanks, I solved in this way (is the solution that i wanted to avoid btw)
0

save your html file into your project and then load the html in webview, please see below , if this doesnt help please check this :- link

import UIKit


class ViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let localfilePath = NSBundle.mainBundle().URLForResource("test", withExtension: "html");
        let myRequest = NSURLRequest(URL: localfilePath!);
        webView.loadRequest(myRequest);
        //or use as below 
        let myHTMLString:String! = "<h1>Hello Pizza!</h1>"
        webView.loadHTMLString(myHTMLString, baseURL: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



}

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.