Ich möchte ein Skript mit WKWebview
API injizieren. Aus irgendeinem Grund funktioniert es nicht und ich kann es nicht herausfinden. Ich habe Debugging in Safari
Entwicklerkonsole versucht, und ich kann den Code JavaScript
dort entweder nicht finden.WKUserScript funktioniert nicht
Implementation-Code wie folgt:
NSString *js = @"document.body.style.background = \"#FF0000\";";
NSString *myScriptSource = @"alert('Hello, World!')";
WKUserScript *s = [[WKUserScript alloc] initWithSource:myScriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
WKUserContentController *c = [[WKUserContentController alloc] init];
[c addUserScript:s];
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.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
[webview loadRequest:request];
});
Haben Sie also eine Antwort auf Ihre Frage gefunden? – Bastian
@Bastian können Sie versuchen mit meiner Lösung, wenn dies Ihnen hilft –