English 中文(简体)
我怎样才能将贾瓦语传回功能传给科拉/奥比-C?
原标题:How can I pass a JavaScript callback function to Cocoa/Obj-C?

我试图通过 Java文在网上通话中的功能参考资料,因此,我的目标——C法典在过时的奥杰-C方法完成后可以重新加入这一功能。 我看到如何通过简单类型的文件——说明、编号等——但似乎无法说明如何通过某种职能参考,例如通过某种职能参考。

window.myRootObject.myObjCSelector("some-string", function(data) { 
  console.log(data); 
});

我把谷歌 around起来,但继续走得很短,而且 have在docs(yet)中已经提到这一点。 在科恰开展工作,但爱能做同样的事。

Thanks in advance for any help!

Edit: The function argument should be anonymous -- hopefully this clarifies. :)

最佳回答

最后,这个数字是这样。 也许会有更好的办法(如果是的话,请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!

问题回答

或许,如果它具有全球非匿名功能,你可以尝试将功能名称传至奥杰-C,然后评价像<条码>。





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签