最后,这个数字是这样。 也许会有更好的办法(如果是的话,请chim!)但似乎有以下几条。 在我的奥贝吉-C(NSObject
-derived)类别中,我提到了。 WebView
我界定了以下便于使用的文字方法:
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
- (void) search:(NSString *)prefix withCallback:(WebScriptObject *)callback;
......打算提出两个论点:试图寻找证据,并匿名要求处理结果。 执行:
- (void) search:(NSString *)prefix withCallback:(WebScriptObject *)callback
{
// Functions get passed in as WebScriptObjects, which give you access to the function as a JSObject
JSObjectRef ref = [callback JSObject];
// Through WebView, you can get to the JS globalContext
JSContextRef ctx = [[view mainFrame] globalContext];
// In my case, I have a JSON string I want to pass back into the page as a JavaScript object
JSValueRef obj = JSValueMakeFromJSONString(ctx, JSStringCreateWithCFString((__bridge CFStringRef)responseString));
// And here s where I call the callback and pass in the JS object
JSObjectCallAsFunction(ctx, ref, NULL, 1, &obj, NULL);
}
This actually works asynchronously as well, through Objective-C blocks, but the gist is above. Hope it helps someone else!