开发者

passing objective c variable to javascript in ios

开发者 https://www.devze.com 2023-01-15 07:35 出处:网络
How to pass a ObjectiveC string Variable to a javascript function? I ha开发者_开发百科ve just started with javascript.. so plz 4give my ignorance...NSString * param= @\"foo\";

How to pass a ObjectiveC string Variable to a javascript function? I ha开发者_开发百科ve just started with javascript.. so plz 4give my ignorance...


NSString * param  = @"foo";   
NSString * jsCallBack = [NSString stringWithFormat:@"myFunc('%@')",param];
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];


In Objective-C

NSString *str  = @"hi ya";   
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"myFunc('%@')",str]];

Now call this function in the js file:

function myFunc(str)
{
   alert(str);
}

These steps should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号