Reputation: 25
I have an dummy HTML file that is meant to load my javascript files so I can use the functions in them:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
<script type="text/javascript" src="js/makeTable.js"></script>
</head>
<body>
</body>
</html>
I would like to use the functions in makeTable.js in my Xcode code by using the stringByEvaluatingJavascriptFromString function for the UIWebView. All of the parameters that I need to pass are being passed correctly, which I know from using the debugger in Xcode. I can't figure out why the javascript isn't loading and I've tried using alerts to see if it loads.
In Xcode when I'm trying to load my html file in the viewDidLoad method my code looks like this
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"loadingWeb"
ofType:@"html"
inDirectory:@"/htdoc" ];
NSString *html = [NSString stringWithContentsOfFile:htmlPath
encoding:NSUTF8StringEncoding
error:nil];
[_webView loadHTMLString:html
baseURL:[NSURL fileURLWithPath:
[NSString stringWithFormat:@"%@/htdoc/",
[[NSBundle mainBundle] bundlePath]]]];
[_webView loadHTMLString:@"<script>alertMeWithMyCustomFunction('I am');</script>" baseURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/htdoc/",[[NSBundle mainBundle] bundlePath]]]];
}
In the UIWebView, nothing shows up but a blank screen. In the makeTable.js file the alertMeWithMyCustomFunction method looks like this:
function alertMeWithMyCustomFunction(text) {
alert(text+' -> in lib.js');
}
Basically, how am I able to use javascript functions and pass them parameters from Xcode using the stringByEvaluatingJavascriptFromString method because in the end I'd like something like this line of code to work:
NSString* script = [NSString stringWithFormat:@"table = makeTable( %d, %@, %@, %@);", i, [albumObj.moments[i] latitude], [albumObj.moments[i] longitude], jpgPath];
[_webView stringByEvaluatingJavaScriptFromString:script];
All of the JS functions I want to use are in the makeTable.js file. I just need to be able to interact with this file in the UIWebView because all of my data is in Xcode, but I need to work with it in JS
Upvotes: 0
Views: 1686
Reputation: 1406
The problem is that loadHTMLString:baseURL:
removes the HTML that was previously in the UIWebView before adding the new HTML. So when, in your viewDidLoad
, you call loadHTMLString:baseURL:
a second time to inject a script tag, it clears the HTML that you just added from loadingWeb.html. Why not use stringByEvaluatingJavaScriptFromString:
instead of injecting a script tag?
Also, if you didn't know, you can use the web inspector in Safari to inspect and debug your HTML/CSS/JS in your UIWebView. This has been a lifesaver for me when working with UIWebViews. Refer to this answer for instructions.
Upvotes: 2