我使用WebViewJavascriptBridge进行JS和本机代码之间的交互。我发现UIWebView有时会变慢,所以我试图切换到WKWebView。
使用UIWebView时项目很好。
var bridge:WebViewJavascriptBridge!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.webView = UIWebView(frame: self.viewport.frame)
self.view.addSubview(webView!)
let url = NSBundle.mainBundle().URLForResource("index", withExtension: "html", subdirectory: "www")
webView!.loadRequest(NSURLRequest(URL: url!))
webView!.scrollView.bounces = false
self.bridge = WebViewJavascriptBridge(forWebView: webView, handler: { (data, callback) -> Void in
callback("msg from bridge")
})
}
工作编译器列表:
但是当我改为WKWebViewJavascriptBridge时,它在链接时会崩溃:
var bridge:WKWebViewJavascriptBridge!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.webView = WKWebView(frame: self.viewport.frame)
self.view.addSubview(webView!)
let url = NSBundle.mainBundle().URLForResource("index", withExtension: "html", subdirectory: "www")
webView!.loadRequest(NSURLRequest(URL: url!))
webView!.scrollView.bounces = false
self.bridge = WKWebViewJavascriptBridge(forWebView: webView, handler: { (data, callback) -> Void in
callback("msg from bridge")
})
}
错误讯息:
Undefined symbols for architecture arm64:
"_OBJC_CLASS_$_WKWebViewJavascriptBridge", referenced from:
type metadata accessor for __ObjC.WKWebViewJavascriptBridge in ViewController.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
在链接之前,似乎没有编译那些.m文件。
请劝告。
答案 0 :(得分:1)
Phew,最后通过编辑桥接头来实现它。
在档案WKWebViewJavascriptBridge.h
中,找到以下这些行:
#if (__MAC_OS_X_VERSION_MAX_ALLOWED > __MAC_10_9 || __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1)
#define supportsWKWebKit
#endif
然后移除#if #endif
对,只需将#define supportsWKWebKit
位移开即可。但确保您使用的是iphone 7.1或更高版本,或者它不支持WKWebkit 。