针对刘海屏的设置
if #available(iOS 11.0, *) {
web.scrollView.contentInsetAdjustmentBehavior = .never
} else {
// Fallback on earlier versions
}
import UIKit
import WebKit
class HomeBannerClickController: BaseController {
var str = ""
// MARK: - Properties
private lazy var web: WKWebView = {
var webView = WKWebView()
//以下代码适配大小 var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}
let jScript = ""
let wkUScript = WKUserScript(source: jScript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let wkUController = WKUserContentController()
wkUController.addUserScript(wkUScript)
let wkWebConfig = WKWebViewConfiguration()
wkWebConfig.userContentController = wkUController
wkWebConfig.userContentController.add(self, name: "callbackHandler")
webView = WKWebView(frame: CGRect.zero, configuration: wkWebConfig)
webView.backgroundColor = UIColor.white
return webView
}()
private lazy var progressView: UIProgressView = UIProgressView()
// MARK: - View LifeCycle
override func loadView() {
super.loadView()
self.view.addSubview(web)
self.view.addSubview(progressView)
}
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 11.0, *) {
web.scrollView.contentInsetAdjustmentBehavior = .never
} else {
// Fallback on earlier versions
}
web.addObserver(self, forKeyPath:"estimatedProgress", options: NSKeyValueObservingOptions.new, context:nil)
web.navigationDelegate = self
/// NetWork
self.request()
}
deinit {
web.removeObserver(self, forKeyPath:"estimatedProgress")
web.navigationDelegate = nil
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
web.snp.makeConstraints { (make) in
make.left.top.right.equalToSuperview()
if #available(iOS 11.0, *) {
make.bottom.equalToSuperview()
} else {
make.bottom.equalToSuperview()
}
}
progressView.snp.makeConstraints { (make) in
make.left.top.right.equalTo(web)
make.height.equalTo(2)
}
progressView.tintColor = UIColor.init(red: 245/255, green: 77/255, blue: 117/255, alpha: 1)
progressView.isHidden = true
}
// MARK: - Events
// MARK: - Model
let a = UserDefaultsManager_CN.queryData(Defaults_User_Token)
// MARK: - NetWork
func request() {
if str == "" {
} else {
let url = URL(string: self.str)
let request = URLRequest(url: url!)
web.load(request)
}
}
}
// MARK: - WKNavigationDelegate
extension HomeBannerClickController: WKNavigationDelegate, WKScriptMessageHandler {
override func observeValue(forKeyPath keyPath:String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
// 加载进度
if keyPath == "estimatedProgress" {
let newprogress = change?[.newKey]!as! Double
let oldprogress = change?[.oldKey]as? Double ?? 0.0
//不要让进度条倒着走...有时候goback会出现这种情况
if newprogress < oldprogress {
return
}
if newprogress == 1 {
progressView.isHidden = true
progressView.setProgress(0, animated:false)
} else {
progressView.isHidden = false
progressView.setProgress(Float(newprogress), animated:true)
}
}
}
func webView(_ webView:WKWebView, didFinish navigation: WKNavigation!) {
progressView.isHidden = true
progressView.setProgress(0, animated:false)
}
func webView(_ webView:WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
progressView.isHidden = true
progressView.setProgress(0, animated:false)
}
/// WKScriptMessageHandler
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
NJLog(message: message.body)
// var info = message.body as! Dictionary<String, Any>
// NJLog(message: message.name)
// NJLog(message: message.frameInfo.request.url?.absoluteString)
if (message.name == "callbackHandler") {
NJLog(message: "JavaScript is sending a message \(message.body)")
let info = message.body as! Dictionary<String, Any>
let type = info["type"]! as! String
if type == "address" {
// 收货地址
let con = MyShouhuoAddressController()
self.navigationController?.pushViewController(con, animated: true)
} else if type == "store-info" {
// 门店资料
let con = MyInfoStoreDataController()
con.navigationItem.title = "门店资料"
self.navigationController?.pushViewController(con, animated: true)
} else if type == "order" {
let order_token = info["order_token"]! as! String
let surprise_type = info["surprise_type"]! as! String
NJLog(message: order_token)
NJLog(message: surprise_type)
let con = OrderConfirmViewController()
con.hidesBottomBarWhenPushed = true
con.order_token = order_token
con.surprise_type = "\(surprise_type)"
self.navigationController?.pushViewController(con, animated: true)
}
}
}
}
网友评论