에서 JSContext을 얻는 방법, 나는 통해 JSContext
를 얻을 수 있습니다.있는 UIWebView에서 WKWebView
WKWebView에 JSContext을 얻을 수있는 방법이 있나요? 사전에
감사합니다. 레이아웃과 자바 스크립트가 다른 프로세스에 처리되기 때문에
에서 JSContext을 얻는 방법, 나는 통해 JSContext
를 얻을 수 있습니다.있는 UIWebView에서 WKWebView
WKWebView에 JSContext을 얻을 수있는 방법이 있나요? 사전에
감사합니다. 레이아웃과 자바 스크립트가 다른 프로세스에 처리되기 때문에
당신은 문맥을 얻을 수 없습니다.
대신, 웹보기 구성에 스크립트를 추가하고 스크립트 메시지 핸들러로 뷰 컨트롤러 (또는 다른 객체)을 설정합니다.
지금과 같이 자바 스크립트에서 메시지를 보내
window.webkit.messageHandlers.interOp.postMessage(message)
스크립트 메시지 핸들러가 콜백을받을 것이다 :
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message{
NSLog(@"%@", message.body);
}
그래, 난 이미이 방법을 사용했습니다. evaluateJavaScript를 통해 js 콜백을 호출 할 수 있습니다. – Xiong
버그 보고서를 열어야합니다. 애플은 중간에 마술을 할 수 있고 여전히 기능을 제공 할 수 있습니다. 프로젝트가 오픈 소스이기 때문에 누군가가 프로젝트를 수행하고 끌어 오기 요청을 게시 할 수 있습니다. –
단점은 실행 중 발생하는 JavaScript 오류에 대한 정보를 얻는 방법이없는 것 같습니다. –
XWebView이 문제에 대한 좋은 솔루션입니다. 네이티브와 JS 사이에 다리를 놓을 수 있고 바인딩 스타일의 API를 제공 할 수 있습니다.
는 정말 멋진 보이지만 아이폰 OS는 단지 참고하시기 바랍니다 ... – shmim
그러나 실제로 JavaScriptCore는 – NaveenKumar
과의 포기하지 않을 액세스, 그것은 대신 응용 프로그램의 과정의 렌더링 과정에 있기 때문에 WKWebView의 JSContext에 액세스하는 것은 불가능하다. – soflare
구성이처럼 wkwebview과 유사한 패턴으로 스크립트에서 따라 처리기 및 포스트 메시지를 추가
NSString *myScriptSource = @"alert('Hello, World!')";
WKUserScript *s = [[WKUserScript alloc] initWithSource:myScriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
WKUserContentController *c = [[WKUserContentController alloc] init];
[c addUserScript:s];
// Add a script message handler for receiving "buttonClicked" event notifications posted from the JS document using window.webkit.messageHandlers.buttonClicked.postMessage script message
[c addScriptMessageHandler:self name:@"buttonClicked"];
WKWebViewConfiguration *conf = [[WKWebViewConfiguration alloc] init];
conf.userContentController = c;
WKWebView *webview = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:conf];
[self.view addSubview:webview];
webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Do any additional setup after loading the view, typically from a nib.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
[webview loadRequest:request];
메소드 이름
#pragma mark -WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([message.name isEqualToString:@"buttonClicked"]) {
self.buttonClicked ++;
}
// JS objects are automatically mapped to ObjC objects
id messageBody = message.body;
if ([messageBody isKindOfClass:[NSDictionary class]]) {
NSString* idOfTappedButton = messageBody[@"ButtonId"];
[self updateColorOfButtonWithId:idOfTappedButton];
}
}
와 스크립트 메시지 핸들러 "WKScriptMessageHandler"를 구현하고 메시지 양식을 게시 js like this
var button = document.getElementById("clickMeButton");
button.addEventListener("click", function() {
var messgeToPost = {'ButtonId':'clickMeButton'};
window.webkit.messageHandlers.buttonClicked.postMessage(messgeToPost);
},false);
JS ca에 대한 해결책을 찾았습니까? 뭐라구? JS 컨텍스트에 대한 기본 객체를 바인딩 JS 콜백을 처리하기 위해 더 편리하지만 –