Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
37
rated 0 times [  43] [ 6]  / answers: 1 / hits: 15398  / 13 Years ago, mon, september 5, 2011, 12:00:00

Is there a way to inject a local JS file or a JS file that's store on my server into any web page loaded by UIWebView?



Say I navigate to google.com in that UIWebView, it renders that page and also runs my JS file (local or on my webserver)?


More From » objective-c

 Answers
30

The following code will inject javascript locally



- (void)injectJavascript:(NSString *)resource {
NSString *jsPath = [[NSBundle mainBundle] pathForResource:resource ofType:@js];
NSString *js = [NSString stringWithContentsOfFile:jsPath encoding:NSUTF8StringEncoding error:NULL];

[self.webView stringByEvaluatingJavaScriptFromString:js];
}


Just pass in the name of the file before the js, eg.



[self injectJavascript:@script];


To do inject javascript from your server you can download it first and load it into the web view in a similar manner.


[#90251] Sunday, September 4, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
admin

Total Points: 468
Total Questions: 103
Total Answers: 103

Location: Equatorial Guinea
Member since Sun, Feb 14, 2021
3 Years ago
;