Thlbaut
Thlbaut

Reputation: 649

Can't load javascript in UIWebView

I'm trying to load a javascript file but it doesn't work.

My code :

NSString *html = [NSString stringWithFormat:@"<html><head></head><body><script type=\"text/javascript\" src=\"http://mywebsite.com/file.js\"></script></body></html>"];
[_webView loadHTMLString:html baseURL:nil];

This code is supposed to display a video, but nothing happens. This code displays correctly the video in Desktop browser, my JS is correct.

Upvotes: 1

Views: 2717

Answers (3)

Hiren Varu
Hiren Varu

Reputation: 1477

use url in baseURL

NSURL *base=[NSURL URLWithString:@"website.com"];

Upvotes: 4

Nirav Gadhiya
Nirav Gadhiya

Reputation: 6342

Write following code in - webViewDidFinishLoad:

NSString * strJavaScript = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://mywebsite.com/file.js"] encoding:NSUTF8StringEncoding error:nil];
[_webView stringByEvaluatingJavaScriptFromString:strJavaScript];

Upvotes: 0

ares777
ares777

Reputation: 3628

Do not use string description. Instead use:

      [_webView loadHTMLString:html baseURL:nil];

Upvotes: 0

Related Questions