Skip to content

Commit 694b03f

Browse files
committed
fix demo code
1 parent 285ddb1 commit 694b03f

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

WKWebViewJavascriptBridge.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
BC4A38B320138F7500E96E5D /* WKWebViewJavascriptBridgeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC4A38B220138F7500E96E5D /* WKWebViewJavascriptBridgeTests.swift */; };
1515
BC4A38B520138F7500E96E5D /* WKWebViewJavascriptBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = BC4A38A720138F7500E96E5D /* WKWebViewJavascriptBridge.h */; settings = {ATTRIBUTES = (Public, ); }; };
1616
BC942673201750BA00A5A6F9 /* WKWebViewJavascriptBridge.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC4A38A420138F7500E96E5D /* WKWebViewJavascriptBridge.framework */; };
17+
BC94267420182E8300A5A6F9 /* Demo.html in Resources */ = {isa = PBXBuildFile; fileRef = BC995A582016F6A60012F728 /* Demo.html */; };
1718
BC995A492016F5690012F728 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC995A482016F5690012F728 /* AppDelegate.swift */; };
1819
BC995A4B2016F5690012F728 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC995A4A2016F5690012F728 /* ViewController.swift */; };
1920
BC995A4E2016F5690012F728 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BC995A4C2016F5690012F728 /* Main.storyboard */; };
@@ -272,6 +273,7 @@
272273
isa = PBXResourcesBuildPhase;
273274
buildActionMask = 2147483647;
274275
files = (
276+
BC94267420182E8300A5A6F9 /* Demo.html in Resources */,
275277
BC995A532016F5690012F728 /* LaunchScreen.storyboard in Resources */,
276278
BC995A502016F5690012F728 /* Assets.xcassets in Resources */,
277279
BC995A4E2016F5690012F728 /* Main.storyboard in Resources */,

WKWebViewJavascriptBridgeDemo/Demo.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
.logLine { border-bottom:1px solid #ccc; padding:4px 2px; font-family:courier; font-size:11px; }
99
</style>
1010
</head><body>
11-
<h1>WebViewJavascriptBridge Demo</h1>
11+
<h1>Demo</h1>
1212
<script>
1313
window.onerror = function(err) {
1414
log('window.onerror: ' + err)
@@ -23,7 +23,7 @@ <h1>WebViewJavascriptBridge Demo</h1>
2323
<!-- WVJBIframe.style.display = 'none';-->
2424
<!-- WVJBIframe.src = 'https://__bridge_loaded__';-->
2525
<!-- document.documentElement.appendChild(WVJBIframe);-->
26-
window.webkit.messageHandlers.setupWKWebViewJavascriptBridge.postMessage()
26+
window.webkit.messageHandlers.iOS_Native_InjectJavascript.postMessage(null)
2727

2828
<!-- setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)-->
2929
}

WKWebViewJavascriptBridgeDemo/ViewController.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,27 @@ class ViewController: UIViewController {
2020
webView.navigationDelegate = self
2121
bridge = WKWebViewJavascriptBridge(webView: webView)
2222
view.addSubview(webView)
23+
loadDemoPage()
2324
}
2425

2526
func loadDemoPage() {
26-
let pagePath = Bundle.main.path(forResource: "Demo", ofType: "html")
27+
enum LoadDemoPageError: Error {
28+
case nilPath
29+
}
30+
31+
do {
32+
let pagePath = Bundle.main.path(forResource: "Demo", ofType: "html")
33+
guard pagePath != nil else {
34+
throw LoadDemoPageError.nilPath
35+
}
36+
let pageHtml = try String(contentsOfFile: pagePath!, encoding: .utf8)
37+
let baseURL = URL(fileURLWithPath: pagePath!)
38+
webView.loadHTMLString(pageHtml, baseURL: baseURL)
39+
} catch LoadDemoPageError.nilPath {
40+
print(print("webView loadDemoPage error: pagePath is nil"))
41+
} catch let error {
42+
print("webView loadDemoPage error: \(error)")
43+
}
2744
}
2845
}
2946

0 commit comments

Comments
 (0)