123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- //
- // OJSWebViewController.swift
- // OJASwiftKitDemo
- //
- // Created by luxiaoming on 2017/12/23.
- // Copyright © 2017年 luxiaoming. All rights reserved.
- //
- import UIKit
- import WebKit
- class OJSWebViewController: UIViewController {
- fileprivate lazy var webView: WKWebView = {
-
- let preferences = WKPreferences()
- preferences.minimumFontSize = 48
- let config = WKWebViewConfiguration()
- config.preferences = preferences
- config.userContentController.add(self, name: "loginCallback")
- config.userContentController.add(self, name: "shareClick")
-
- let webView = WKWebView(frame: self.view.bounds, configuration: config)
- webView.navigationDelegate = self
- webView.uiDelegate = self
- return webView
-
- }()
- }
- // MARK: - Lifecycle
- extension OJSWebViewController {
-
- override func viewDidLoad() {
- super.viewDidLoad()
- webView.frame = self.view.bounds
- self.view.addSubview(webView)
-
-
-
-
- // webView.configuration.userContentController.add(self, name: "loginCallback")
- // webView.configuration.userContentController.add(self, name: "getUid")
- // if let url = URL(string: "http://www.duowan.com/mlogin/index.html?from=app") {
- // let request = URLRequest(url: url)
- // webView.load(request)
- // }
-
- // if let url = URL(string: "http://www.duowan.com/applogin/index.html") {
- // let request = URLRequest(url: url)
- // webView.load(request)
- // }
-
- // if let url = URL(string: "http://172.16.15.158:8080/iosTest.html") {
- // let request = URLRequest(url: url)
- // webView.load(request)
- // }
- // NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"index.html" ofType:nil];
- // NSURL *fileURL = [NSURL fileURLWithPath:urlStr];
- // [self.webView loadFileURL:fileURL allowingReadAccessToURL:fileURL];
-
- let urlString = Bundle.main.path(forResource: "index2", ofType: "html")
- let fileUrl = URL(fileURLWithPath: urlString!)
- if #available(iOS 9.0, *) {
- webView.loadFileURL(fileUrl, allowingReadAccessTo: fileUrl)
- } else {
- // Fallback on earlier versions
- }
-
- }
-
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- }
- // MARK: - PrivateMethod
- private extension OJSWebViewController {
-
-
- func setupForJavaScriptAction(webView: WKWebView) {
-
- }
-
- func runJSFunc() {
- let aaa = "aaa"
- let jsString = "setLocation('\(aaa)')"
- webView.evaluateJavaScript(jsString) { (result, error) in
- if let error = error {
- print(error)
- } else {
- print("result is \(result.debugDescription)")
- }
- }
- }
-
- }
- extension OJSWebViewController: WKUIDelegate {
-
- func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
- let alert = UIAlertController(title: message, message: message, preferredStyle: .alert)
-
- let cancelActiion = UIAlertAction(title: "ok", style: .cancel, handler: { (action: UIAlertAction) in
- completionHandler()
- })
- alert.addAction(cancelActiion)
-
- self.present(alert, animated: true, completion: nil)
-
- }
-
- }
- extension OJSWebViewController: WKNavigationDelegate {
-
- func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
- NSLog(navigation.description)
-
- }
-
- func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
- NSLog(error.localizedDescription)
- }
-
- func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
- NSLog(error.localizedDescription)
- }
-
-
- func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
- if let url = navigationAction.request.url,
- let scheme = url.scheme,
- let host = url.host,
- scheme == "haleyaction" {
-
- NSLog("\(host)")
-
- if host == "scanClick" {
-
- } else if host == "shareClick" {
-
- } else if (host == "getLocation") {
- self.runJSFunc()
- } else if (host == "setColor") {
-
- } else if (host == "payAction") {
-
- } else if (host == "shake") {
-
- } else if (host == "goBack") {
-
- }
-
- decisionHandler(.cancel)
- return
- }
-
-
- decisionHandler(.allow)
- }
- }
- extension OJSWebViewController: WKScriptMessageHandler {
-
- func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
- if message.name == "loginCallback" {
- NSLog("\(message.name): \(message.body)")
- }
- else if message.name == "shareClick" {
- NSLog("\(message.name): \(message.body)")
- }
- else if message.name == "getUid" {
- self.webView.evaluateJavaScript("getUidCallback(\(123))", completionHandler: { (success, error) in
- print(success)
- print(error)
-
- })
- }
- }
-
- }
|