为了传递变量,我们使用UIWebView
' 方法stringByEvaluatingJavaScriptFromString:
我怎样才能传递一个对象?
为了传递变量,我们使用UIWebView
' 方法stringByEvaluatingJavaScriptFromString:
我怎样才能传递一个对象?
For passing an object from Obj-C to JS, you have no choice but to serialize and deserialize it. The most fitting data format would naturally be JSON. This tutorial will help you: http://www.raywenderlich.com/5492/working-with-json-in-ios-5
From your question I gather that you don't just want to talk to JavaScript but to the JavaScript context inside of a UIWebView. Sadly, you can't directly access the JSContext object of a UIWebView the way you can access it in Cocoa on the Mac. But if you just want to bridge from Obj-C to JavaScript WITHOUT using a UIWebView, you can so by shipping your own version of JavaScriptCore with your app. If you do that, you can move Obj-C and JS objects back and forth between the two worlds. This is a demo project that demonstrates this: https://github.com/jfahrenkrug/AddressBookSpy/blob/master/AddressBookSpy/AddressBookSpy/ABSEngine.m
我分叉了 phoboslab 的 repo并将其更新为 WebKit 的最新版本(截至上周五)。它与 iOS 5 和 6 兼容,并包括 iOS 7 中的 Objective-C 到 Javascript 桥(不使用私有框架或函数)。
你可以在 GitHub 上找到 repo: https ://github.com/darionco/JavaScriptCore-iOS
为了方便起见,我还在包含库 (.a) 和所需头文件的存储库中添加了一个 ZIP 文件。
您可以在此处找到有关如何使用桥接器传递对象的教程:http: //www.steamclock.com/blog/2013/05/apple-objective-c-javascript-bridge/
干杯!