WKUserScript就是帮助我们完成JS注入的类,它能帮助我们在页面填充前或js填充完成后调用。
let key = (String(format: "localStorage.setItem(\"token\",'%@');localStorage.setItem(\"project_id\",'%@');localStorage.setItem(\"user_id\",'%@');",User.defaultUser.token!,User.defaultUser.getProjectId(),(String(format: "%@",User.defaultUser.id!))))
letwkUScript =WKUserScript(source: key, injectionTime:WKUserScriptInjectionTime.atDocumentStart, forMainFrameOnly:false)
let config = WKWebViewConfiguration()
letde =WeakScriptMessageDelegate(delegate:selfasWKScriptMessageHandler)
config.userContentController.add(de!, name:"iOS")
config.userContentController.addUserScript(wkUScript)
webView=WKWebView(frame: .zero, configuration: config)
webView.scrollView.delegate = self
网友评论