Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swift5 使用WKWebView跟h5没法交互 #398

Open
JianWenXie opened this issue Feb 10, 2020 · 4 comments
Open

swift5 使用WKWebView跟h5没法交互 #398

JianWenXie opened this issue Feb 10, 2020 · 4 comments

Comments

@JianWenXie
Copy link

用UIWebView可以实现跟h5交互
但是用WKWebView就没没响应
以下是iOS的方法调用
import UIKit
import WebKit
import WKWebViewJavascriptBridge
/// 工单报修
class VKBaseWebViewCtrl: VKBaseViewCtrl {

var web_url:String!
let webView = WKWebView(frame: CGRect(), configuration: WKWebViewConfiguration())
var bridge: WKWebViewJavascriptBridge!
override func viewDidLoad() {
    super.viewDidLoad()
    setupUI()
}
func setupUI() {
    webView.frame = view.bounds
    webView.navigationDelegate = self
    view.addSubview(webView)
    let url = URL.init(string: web_url)
    let request = URLRequest.init(url: url!)
    webView.load(request)
    // setup bridge
    bridge = WKWebViewJavascriptBridge(webView: webView)
    bridge.isLogEnable = true
    bridge.register(handlerName: "testiOSCallback") { (paramters, callback) in
    print("testiOSCallback called: \(String(describing: paramters))")
    callback?("Response from testiOSCallback")
    }
    bridge.call(handlerName: "testJavascriptHandler", data: ["foo": "before ready"], callback: nil)
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    webView.frame = view.bounds
}

 init(weburl:String) {
    self.web_url = weburl
    super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}
extension VKBaseWebViewCtrl: WKUIDelegate,WKNavigationDelegate,UIWebViewDelegate{
// func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// hideHud()
// }
//
// func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
// hideHud()
// Log(message: "加载失败")
// }
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("webViewDidStartLoad")
}

  func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
      print("webViewDidFinishLoad")
  }

}

@zhusongyu
Copy link

跟swift5应该没关系,我用的好好的

@housenkui
Copy link

我已经重构了这个库.
如果您遇到麻烦,可以尝试使用它.
下载地址:
https://github.com/housenkui/WebViewJavascriptBridge
它更轻,使用更简单. 10分钟之内应该可以完成接入.

@Dast1Woop
Copy link

bridge 强引用试试,var gBridge: WKWebViewJavascriptBridge? ... gBridge = bridge

@Dast1Woop
Copy link

try this:
import Foundation
import WebKit
import WebViewJavascriptBridge

protocol WebviewWithBridgePr{
var webviewWithBridgeTuple:(webview:WKWebView,bridge:WKWebViewJavascriptBridge)?{set get}

func initWebviewAndBridge(urlStr:String) -> (WKWebView,WKWebViewJavascriptBridge)?

}

extension WebviewWithBridgePr{
func initWebviewAndBridge(urlStr:String) -> (WKWebView,WKWebViewJavascriptBridge)? {

// let config = WKWebViewConfiguration.init()
// let webv = WKWebView.init(frame: <#T##CGRect#>, configuration: T##WKWebViewConfiguration)
let webv = WKWebView.init()

    let url = URL.init(string: urlStr)
    
    guard let url = url else{
        return nil
    }
    
    let rqst = URLRequest.init(url: url, cachePolicy: Foundation.URLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 60)
    
    webv.load(rqst)
    
    let bridge = WKWebViewJavascriptBridge.init(for: webv)
    
    guard let bridge = bridge else{
        return nil
    }
    
    WKWebViewJavascriptBridge.enableLogging()
    
    return (webv, bridge)
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants