将值从xcode传递给javascript

时间:2011-02-15 05:39:09

标签: javascript objective-c xcode

我在我的应用中使用谷歌api v3。要找到方向,我需要通过我当前的位置&目标到java脚本。 google api v3使用java脚本来使用地图功能。

我使用以下来传递变量。但没有奏效。

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"Handler.method(%@);", str]];

如何将变量从obj c传递给javascript .. ???

1 个答案:

答案 0 :(得分:0)

我通过网址

将值传递到我的网页浏览应用中的原生html页面
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@?bob=123&frank=321&tom=213", base]];

NSURLRequest *req = [NSURLRequest requestWithURL:url];

[webView loadRequest:req];

该值已通过URL传递给html页面和javascript 在Javascript使用以下函数来获取值

function gup( name )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];

}

现在使用值

的参数名称调用此函数
var userLat=(gup( 'bob' ));

这将返回值123

希望这有助于..... :)。