How to integrate JavaScript in objective c?
Here, is my JavaScript code:
<!DOCTYPE HTML>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="https://maps.micello.com/webmap/v0/micellomap.js"></script>
<script type="text/javascript">
micello.maps.init("ZccEicxOED0xicnQFrnkK7uzXJJUog",mapInit);
function mapInit(strMapID) {
alert("Hello World!"+strMapID);
var mapControl = new micello.maps.MapControl('mapElement');
var mapDataObject = mapControl.getMapData();
mapDataObject.loadCommunity(strMapID);
}
</script>
<style type="text/css">
html, body { height: 100%; width: 100%; margin: 0; overflow: hidden; }
#mapElement{ width:100%; height:100%; }
</style>
</head>
<body>
<div id="mapElement"></div>
</body>
</html>
I am using this JavaScript code in my Objective-C in WebView. Here, is my objective-c code:
- (void)viewDidLoad {
NSLog(@"strMapID = %@",strMapID);
NSString *path;
NSBundle *thisBundle = [NSBundle mainBundle];
path = [thisBundle pathForResource:@"SomeHTML" ofType:@"html"];
NSURL *instructionsURL = [NSURL fileURLWithPath:path];
[webView loadRequest:[NSURLRequest requestWithURL:instructionsURL]];
}
WebView delegate methods:
-(void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"start");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView1 {
NSLog(@"finish");
[webView1 stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"mapInit(%@)",strMapID]];
//[webView stringByEvaluatingJavaScriptFromString:@"mapInit()"];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(@"Error for WEBVIEW: %@", [error description]);
}
But here, the JavaScript alert("Hello World!"+strMapID);
is calling two times. first time it is showing correct MapID but second time it's showing undefined.
I don't know why alert is calling 2 times. Because of this it's giving me 500 error in my webview.
Please guide me to solve my Problem.
Thanks in Advance.