마지막으로 알아 냈습니다. 더 좋은 방법이있을 수 있습니다 (그렇다면 차임하세요!)하지만 다음과 같이 작동하는 것 같습니다. 내를 Obj-C (NSObject
유도 된) 클래스에서 -되는 나는 WebView
에 대한 참조를 전달 - 나는 다음과 같은 스크립트 접근 방법 정의 :
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
- (void) search:(NSString *)prefix withCallback:(WebScriptObject *)callback;
... 두 개의 인수를하기위한 것입니다 : 검색 할 문자열 및 결과를 처리하는 익명의 함수 콜백. 그것은 thusly 히 구현 것 :
- (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);
}
이 실제로 목표 - C 블록을 통해, 비동기 적으로 잘 작동하지만, 요점 위입니다. 희망은 다른 사람을 돕는다!
감사합니다; 이것은 명확히 나를 도왔다! – tenfour