iOS与javascript的交互
一.Objective-C调用javascript方法并获取返回值
html代码如下
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(187, 44, 162);"><html></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(187, 44, 162);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><head></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(209, 47, 27);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2"><meta </span><span style="font-variant-ligatures: no-common-ligatures; color: #967e34">http-equiv</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">=</span>"content-type"<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #967e34">content</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">=</span>"text/html;charset=utf-8"<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2"><title></span><span style="font-family: 'Heiti SC Light';">方法调用</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2"></title></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(209, 47, 27);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2"><script </span><span style="font-variant-ligatures: no-common-ligatures; color: #967e34">type</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">=</span>"text/javaScript"<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">function</span> returnString(){</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> document.getElementById(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"mytext"</span>).value;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> }</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"> </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">function</span> testClick(parameter1,parameter2){</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> alert(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"appneme:"</span>+<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"function:"</span>+parameter1+<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">":"</span>+parameter2);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> document.location = <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"appneme:"</span> + <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"function:"</span> + parameter1 + <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">":"</span> + parameter2;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> }</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2"></script></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(187, 44, 162);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span></head></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"> </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(187, 44, 162);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><body></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(187, 44, 162);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><p><input <span style="font-variant-ligatures: no-common-ligatures; color: #967e34">type</span>=<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"text"</span> <span style="font-variant-ligatures: no-common-ligatures; color: #967e34">id</span>=<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"mytext"</span> <span style="font-variant-ligatures: no-common-ligatures; color: #967e34">value</span>=<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"Name"</span>/></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(209, 47, 27);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2"><input </span><span style="font-variant-ligatures: no-common-ligatures; color: #967e34">type</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">=</span>"button"<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #967e34">id</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">=</span>"button"<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #967e34">value</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">=</span>"<span style="font-family: 'Heiti SC Light';">获取数据</span>"<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #967e34">onclick</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">=</span>"testClick('ddddd','aaaaa');"<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">/></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(187, 44, 162);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span></body></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(187, 44, 162);"></html></p>
OC代码如下
- (void)viewDidLoad {
[super viewDidLoad];
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="color: rgb(112, 61, 170);">NSString</span> *basePath = [[<span style="color: rgb(112, 61, 170);">NSBundle</span> <span style="color: rgb(61, 29, 129);">mainBundle</span>]<span style="color: rgb(61, 29, 129);">bundlePath</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="color: rgb(112, 61, 170);">NSString</span> *helpHtmlPath = [basePath <span style="color: rgb(61, 29, 129);">stringByAppendingPathComponent</span>:<span style="color: rgb(209, 47, 27);">@"index.html"</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="color: rgb(112, 61, 170);">NSURL</span> *url = [<span style="color: rgb(112, 61, 170);">NSURL</span> <span style="color: rgb(61, 29, 129);">fileURLWithPath</span>:helpHtmlPath];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);"><span style="color: rgb(0, 0, 0);"> </span>//<span style="font-family: 'Heiti SC Light';">加载本地</span>html<span style="font-family: 'Heiti SC Light';">文件</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(79, 129, 135);">_webview</span><span style="color: rgb(0, 0, 0);"> </span>loadRequest<span style="color: rgb(0, 0, 0);">:[</span><span style="color: rgb(112, 61, 170);">NSURLRequest</span><span style="color: rgb(0, 0, 0);"> </span>requestWithURL<span style="color: rgb(0, 0, 0);">:url]];</span></p>}<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;">- (<span style="color: rgb(187, 44, 162);">void</span>)webViewDidFinishLoad:(<span style="color: rgb(112, 61, 170);">UIWebView</span> *)webView{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(61, 29, 129);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> *str = [webView </span>stringByEvaluatingJavaScriptFromString<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"postStr();"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"JS</span><span style="font-family: 'Heiti SC Light'; color: rgb(209, 47, 27);">返回值:</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">%@"</span>,str);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">}</p>二.Objective-C监听HTML获取数据button并返回
OC监听事件代码如下
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(120, 73, 42);">#pragma mark - UIWebViewDelegate</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">- (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">BOOL</span>)webView:(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">UIWebView</span> *)webView shouldStartLoadWithRequest:(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSURLRequest</span> *)request navigationType:(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">UIWebViewNavigationType</span>)navigationType{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span> *requestString = [[request <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">URL</span>] <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">absoluteString</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSArray</span> *components = [requestString <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">componentsSeparatedByString</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@":"</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">if</span> ([components <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">count</span>] > <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">1</span> &&</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> [(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span> *)[components <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">objectAtIndex</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">0</span>] <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">isEqualToString</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"appneme"</span>]) {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">if</span>([(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span> *)[components <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">objectAtIndex</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">1</span>] <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">isEqualToString</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"function"</span>])</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"%@"</span>,[components <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">objectAtIndex</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">2</span>]);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"%@"</span>,[components <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">objectAtIndex</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">3</span>]);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> }</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">NO</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> }</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">YES</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">; </span>// Return YES to make sure regular navigation works as expected.</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">}</p>三.oc中获取html其他信息
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
NSLog(@"%@",currentURL);
NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"%@",title);
本文详细介绍了Objective-C如何调用JavaScript方法并获取返回值,包括本地文件加载、HTML按钮事件监听以及数据传递过程。
3710

被折叠的 条评论
为什么被折叠?



