Objective c 如何在IOS中集成Javascript
如何在objective c中集成JavaScript?Objective c 如何在IOS中集成Javascript,objective-c,uiwebview,xcode7,javascriptcore,Objective C,Uiwebview,Xcode7,Javascriptcore,如何在objective c中集成JavaScript? 下面是我的JavaScript代码: <!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/mi
下面是我的JavaScript代码:
<!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>
WebView委托方法:
-(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]);
}
但是在这里,JavaScriptalert(“helloworld!”+strMapID)代码>正在调用两次。第一次显示正确的MapID,但第二次显示未定义的MapID。
我不知道为什么alert打了两次电话。正因为如此,我的网络视图中出现了500个错误
请引导我解决我的问题
提前谢谢 首先我们需要在我调用的委托方法中设置UIWebView,然后设置UIWebViewstringByEvaluatingJavaScriptFromString
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
CGRect frame = webview.frame;
frame.size.height = height;
webview.frame = frame;
//If you want to set font size and body of HTML
int fontSize = 80;
NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
[webview stringByEvaluatingJavaScriptFromString:jsString];
NSString *jsWebViewFontFamilyString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.fontFamily = 'Helvetica'"];
[webview stringByEvaluatingJavaScriptFromString:jsWebViewFontFamilyString];
//For passing parameter from Objective c to JavaScript
NSString *strMapId = @"1367"; //Your ID
NSString *jsCallBack = [NSString stringWithFormat:@"mapInit('%@')",strMapId];
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];
}
然后如果你想导航或做任何事情
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
switch (navigationType)
{
case UIWebViewNavigationTypeLinkClicked:
//When User tapped a link.
break;
case UIWebViewNavigationTypeOther:
//Some other action occurred.
break;
case UIWebViewNavigationTypeFormSubmitted:
//user submitted a form.
break;
case UIWebViewNavigationTypeBackForward:
//User tapped the back or forward button. .
break;
case UIWebViewNavigationTypeReload:
//User tapped the reload button.
break;
case UIWebViewNavigationTypeFormResubmitted
//User resubmitted a form. .
break;
}
return YES;
}
这有帮助吗:?这不是我的专业领域。谢谢@0aslam0,但是这个链接对我没有帮助。我想知道如何将这个html代码转换成javascript函数。在本例中,我希望通过目标c传递mapInit(n)方法中的参数。很抱歉@user3182143,但我希望在一个js文件中实现我的JavaScript代码,并希望在目标c中传递参数mapInit(n)
方法。你明白我的问题吗?不明白。你能告诉我如何在我的场景中使用这个链接答案吗?N意味着什么?你为什么要通过N?我的答案和你的webview DidFininsLoad答案是正确的。我想你可能在java脚本代码中弄错了
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
switch (navigationType)
{
case UIWebViewNavigationTypeLinkClicked:
//When User tapped a link.
break;
case UIWebViewNavigationTypeOther:
//Some other action occurred.
break;
case UIWebViewNavigationTypeFormSubmitted:
//user submitted a form.
break;
case UIWebViewNavigationTypeBackForward:
//User tapped the back or forward button. .
break;
case UIWebViewNavigationTypeReload:
//User tapped the reload button.
break;
case UIWebViewNavigationTypeFormResubmitted
//User resubmitted a form. .
break;
}
return YES;
}