如何使用WKWebView,JavaScript將消息發送到Objective-C並返回值。WKWebView JavaScript將消息發送到Objective-C並返回值
這樣的:
令結果= window.webkit.messageHandlers.name.postMessage();
如何使用WKWebView,JavaScript將消息發送到Objective-C並返回值。WKWebView JavaScript將消息發送到Objective-C並返回值
這樣的:
令結果= window.webkit.messageHandlers.name.postMessage();
我找到了另一種方式,通過WKUserScriptInjectionTimeAtDocumentStart參數爲JS到站點的localStorage的。 是這樣的:
NSString *token = [NSString stringWithFormat:@"localStorage.setItem(\"accessToken\",'%@');",@"74851c23358c"];
WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:sendToken injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
[config.userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"iOS"];
[config.userContentController addUserScript:wkUScript];
WKWebView *webview = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) configuration:config];
添加具有名稱名稱的腳本消息處理機 導致在使用用戶內容控制器的所有web視圖的所有幀被定義JavaScript函數window.webkit.messageHandlers.name.postMessage(消息體)。
https://developer.apple.com/reference/webkit/wkusercontentcontroller/1537172-add
這不是我想要的結果。 –