js 与 uiwebview objective c 交互

来源:互联网 发布:设备流程图制作软件 编辑:程序博客网 时间:2024/06/06 01:15

网络上已经有相当多文章,自己备用:


1)objective c 调用js

[webview stringByEvaluatingJavaScriptFromString:jsString];

[win callWebScriptMethod:@"addImage"
            withArguments:args];
refs:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/JavaScriptFromObjC.html


2)js 回调objective c

利用uiwebview的delegate,但其中只有出现网页跳转时才会call到,所以有人在js中利用fake url跳转来实现回调,

webView:shouldStartLoadWithRequest:navigationType:

refs:

http://stackoverflow.com/questions/1662473/how-to-call-objective-c-from-javascript

这个soluton存在问题,所以有改良方法。

refs:(推荐)

http://blog.techno-barje.fr/post/2010/10/06/UIWebView-secrets-part3-How-to-properly-call-ObjectiveC-from-Javascript/


3)完整点功能的  jsbridge-to-cocoa

This api http://code.google.com/p/jsbridge-to-cocoa/ does it for you. It is very lightweight.

You can pass images, strings and arrays from javascript to objective-C.





0 0