简体   繁体   English

使用IOS加载JS函数stringByEvaluatingJavaScriptFromString

[英]Load JS Function stringByEvaluatingJavaScriptFromString with IOS

I have a simple javascript function with two variables for the geolocation. 我有一个简单的JavaScript函数,其中包含两个用于地理位置的变量。 I need to pass two variables that I already have in my iOS application to this javascript function. 我需要将我的iOS应用程序中已有的两个变量传递给此javascript函数。

When I send the code via console in the browser everything is OK: 当我通过浏览器中的控制台发送代码时,一切正常:

geoCallBackSuccess({coords:{'longitude':9.598470,'latitude':50.903697}})

Is my code correct? 我的代码正确吗?

In the simulator I see no effect... 在模拟器中,我看不到任何效果...

NSString * param  = @"{coords:{'longitude':9.598470,'latitude':50.903697}}";
NSString * jsCallBack = [NSString stringWithFormat:@"geoCallBackSuccess(%@)",param];
NSLog(@"viewDidLoad - jsCallBack: %@", jsCallBack);
[self.offerUrl stringByEvaluatingJavaScriptFromString:jsCallBack];

Log: 日志:

viewDidLoad - jsCallBack: geoCallBackSuccess({coords:{'longitude':9.598470,'latitude':50.903697}}) viewDidLoad-jsCallBack:geoCallBackSuccess({coords:{'longitude':9.598470,'latitude':50.903697}})

For example, this works for me: 例如,这对我有用:

[self.offerUrl  stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"anzeige\").innerHTML=\"Hello World\";"];

I want to do the same as I do in Android: 我想做的和我在Android中做的一样:

@java...

public void getLocation() {
// magic
if (locationOk) { 
   webview.loadUrl("javascript:geoCallBackSuccess({coords:{'longitude':9.598470,'latitude':50.903697}})");
} else { webview.loadUrl("javascript:geoCallBackError({code:})");
}
}

Thanks 谢谢

我相信您在函数名称后的字符串中只丢失了一个分号,分号-

NSString * jsCallBack = [NSString stringWithFormat:@"geoCallBackSuccess(%@);",param];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM