<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1">
<!-- // 设备的缩放 -->
<meta name="format-detection" content="telephone=no">
<!-- //禁止自动识别数字为手机号码 -->
<meta name="apple-mobile-web-app-capable" content="yes">
<!-- // 去掉苹果工具栏和菜单栏 -->
<meta name="apple-touch-fullscreen" content="yes">
<!-- //全屏 -->
<title>测试</title>
<script src="js/jquery.js"></script>
<script src="js/responsive.js"></script>
<style type="text/css" media="screen">
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
position: relative;
}
div.navi {
text-align: center;
margin-left: 0;
margin-top: 0;
height: 0.88rem;
width: 100%;
position: fixed;
background: #fff;
z-index: 999;
}
.navi img {
position: absolute;
top: 0.1rem;
height: 0.68rem;
}
#back {
left: 0;
}
#share {
right: 0;
}
.navi p {
width: 10rem;
height: 100%;
text-align: center;
font-size: 0.78rem;
}
.method {
padding-top: 0.5rem;
margin-left: 0.5rem;
margin-right: 0.5rem;
position: relative;
}
.method span {
font-size: 0.8rem;
width:5rem;
}
#audioBox {
text-align: center;
padding-top: 0.5rem;
margin-left: 0.5rem;
margin-right: 0.5rem;
position: relative;
}
#audioBox span {
font-size: 0.55rem;
}
#currentMusic {
font-size: 0.40rem;
color: #0fb;
text-align: center;
}
</style>
</head>
<body>
<div class="navi">
</div>
<div class="method">
<br>
<br>
<br>
newTitle:<input type="text" name="fname" id="newTitle" value="设置新标题"/>
<br>
<br>
<button onclick="goBack()">返回上一层</button>
<br>
<br>
<button onclick="testA()">更多工具</button>
<br>
<br>
<button onclick="welogin()">登录</button>
<br>
<br>
<button onclick="useMonitorShots()">截图监听</button>
<br>
<br>
<button onclick="useMonitorCaptrue()">录屏监听</button>
<br>
<br>
<button onclick="wemine()">我的页面</button>
<br>
<br>
<button onclick="goScan()">扫一扫</button>
<br>
<br>
<button onclick="setwebTitle()">设置新标题</button>
<br>
<br>
<button onclick="goFirstMain()">去首页</button>
<br>
<br>
<br>
<br>
<button onclick="goTabbarMe()">去我的</button>
<p id="js_content"></p>
</div>
<script type="text/javascript">
function IOSCallJsWithArgs() {
alert("12345");
}
function useMonitorShots() {
window.webkit.messageHandlers.useMonitorShots.postMessage(null);
}
function useMonitorCaptrue() {
window.webkit.messageHandlers.useMonitorCaptrue.postMessage(null);
}
function goBack() {
window.webkit.messageHandlers.goBack.postMessage(null);
}
function testA() {
window.webkit.messageHandlers.moreTools.postMessage("我是js传递过来的数据")
}
function goFirstMain() {
window.webkit.messageHandlers.goTabbarMain.postMessage("我是js传递过来的数据")
}
function goTabbarCourse() {
window.webkit.messageHandlers.goTabbarCourse.postMessage("我是js传递过来的数据")
}
function goTabbarDisCover() {
window.webkit.messageHandlers.goTabbarDisCover.postMessage("我是js传递过来的数据")
}
function goTabbarMe() {
window.webkit.messageHandlers.goTabbarMe.postMessage("我是js传递过来的数据")
}
function welogin() {
window.webkit.messageHandlers.goLogin.postMessage(null);
}
function wemine() {
window.webkit.messageHandlers.wemine.postMessage(null);
}
function refreshPage() {
window.webkit.messageHandlers.refreshPage.postMessage(null);
}
function gocart() {
window.webkit.messageHandlers.gocart.postMessage(null);
}
function goScan() {
window.webkit.messageHandlers.goScan.postMessage(null);
}
function setwebTitle(){
var value = document.getElementById("newTitle").value;
window.webkit.messageHandlers.webTitle.postMessage(value);
}
function ocToJs(params) {
alert(params);
}
</script>
</body>
</html>
class BYDWebTestViewController:BYDH5WebBaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.bundlePath
let basePath = "\(path)/HTML"
let indexPath = "\(basePath)/index.html"
let url = URL(fileURLWithPath: indexPath)
let urlstr = Bundle.main.path(forResource: "index.html", ofType: nil)
let fileUrl = URL(fileURLWithPath: urlstr!, relativeTo: Bundle.main.bundleURL)
webview.loadFileURL(fileUrl, allowingReadAccessTo: Bundle.main.bundleURL)
// addLineBtn()
}
}
import Foundation
import UIKit
import WebKit
import ProgressHUD
class BYDH5WebBaseViewController: UIViewController {
let webview = BYDWKwebView()
var url:URL?
// 进度条
lazy var progressView: UIProgressView = {
var progressView = UIProgressView()
progressView.progressTintColor = UIColor.red
progressView.trackTintColor = UIColor.clear
return progressView
}()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
override func viewDidDisappear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(false, animated: false)
super.viewDidDisappear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
let top = UIDevice.xp_safeDistanceTop()
webview.frame = CGRect(x: 2, y: Int(top), width: Int(mainScreenSize.width - 4), height: Int(mainScreenSize.height) - Int(top))
self.view.addSubview(webview)
// guard let url = url else {
// #if DEBUG
// ProgressHUD.showError("链接为空!!")
// #endif
// return
// }
// addLineBtn()
// let request = URLRequest(url: url)
// self.webview.load(request)
#warning("Test start")
self.webview.configuration.userContentController.add(self, name: "refreshPage") //刷新页面
self.webview.configuration.userContentController.add(self, name: "moreTools") //更多工具
self.webview.configuration.userContentController.add(self, name: "wemine")
#warning("Test end")
self.webview.configuration.userContentController.add(self, name:"gologin")//登录
self.webview.configuration.userContentController.add(self, name: "goBack")//返回
self.webview.uiDelegate = self
self.webview.navigationDelegate = self
self.view .addSubview(progressView);
self.progressView.mas_makeConstraints { make in
make?.top.mas_equalTo()(top)
make?.left.equalTo()(0)
make?.right.equalTo()(0)
make?.height.equalTo()(3)
}
//进度条的进度监听
self.webview.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "estimatedProgress" {
self.progressView.alpha = 1.0
self.progressView.setProgress(Float(webview.estimatedProgress), animated: true)
//进度条的值最大为1.0
if(self.webview.estimatedProgress >= 1.0) {
UIView.animate(withDuration: 0.3, delay: 0.1, options: .curveEaseInOut, animations: { () -> Void in
self.progressView.alpha = 0.0
}, completion: { (finished:Bool) -> Void in
self.progressView.progress = 0
})
}
}
}
func handleMessage(name:String,pram:Any) {
}
func insertMessageToWeb() {
self.webview.evaluateJavaScript("")
}
}
extension BYDH5WebBaseViewController:WKUIDelegate {
}
extension BYDH5WebBaseViewController:WKNavigationDelegate {
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("网页开始加载。。。。")
}
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("网页加载完成")
// if let title = webView.title,title.count > 0 {
// self.title = title
// }
// self.webview.executeJs(funcStr: "IOSCallJsWithArgs", pra: "123")
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print("网页加载失败!!")
}
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
var style = UIAlertController.Style.alert
if isIPhone {
style = .actionSheet
}
let alert = UIAlertController(title: "网页弹窗", message: message, preferredStyle: style)
let action = UIAlertAction(title: "确定", style: .cancel)
alert.addAction(action)
self.present(alert, animated: true)
completionHandler()
}
}
extension BYDH5WebBaseViewController:WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
switch message.name {
case BYDWebMessage.goLogin.rawValue:
print("去登录!!")
BYDUserHandler.goToLogin(formVC: self) { res in
if res {
ProgressHUD.showSuccess()
}
}
case BYDWebMessage.goBack.rawValue:
print("返回!!")
self.navigationController?.popViewController(animated: true)
case BYDWebMessage.webTitle.rawValue:
if let body = message.body as? String{
self.title = body
}
#warning("Test start")
case "refreshPage":
print("刷新页面!!")
self.webview.reload()
case "courseList":
print("去班级列表!!")
MoreToolsJumpHandler.jumpToToolPage(vc: self, type: .myClass)
case "wemine":
print("去去我的首页!!")
self.navigationController?.popToRootViewController(animated: false)
if let rootVC = UIApplication.shared.delegate as? AppDelegate {
rootVC.tabbarVC?.selectedIndex = 3
}
case "moreTools":
print("去更多工具!!")
default:
print("未处理的:\(message.name)")
self.handleMessage(name: message.name, pram: message.body)
}
}
}