This repository was archived by the owner on Feb 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFLWKWebView.swift
More file actions
54 lines (43 loc) · 1.78 KB
/
FLWKWebView.swift
File metadata and controls
54 lines (43 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import UIKit
import WebKit
@available(iOS 8.0, *)
extension WKWebView: FLWebViewProvider {
// Use associated objects to set and get the request ivar
func associatedObjectKey() -> String {
return "kAssociatedObjectKey"
}
var request: NSURLRequest? {
get {
return objc_getAssociatedObject(self, associatedObjectKey()) as? NSURLRequest
}
set(newValue) {
objc_setAssociatedObject(self, associatedObjectKey(), newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
// A simple convenience initializer, this allows for WKWebView(delegateView:) initialization
convenience init(delegateView: ViewController) {
self.init(frame: delegateView.view.frame, configuration: WKWebViewConfiguration())
setDelegateViews(delegateView)
}
// We will need to set both the UIDelegate AND navigationDelegate in the case of WebKit
func setDelegateViews(viewController: ViewController) {
self.UIDelegate = viewController as WKUIDelegate
self.navigationDelegate = viewController as WKNavigationDelegate
}
func currentURL() -> NSURL? {
return self.URL
}
func canNavigateBack() -> Bool {
return self.canGoBack
}
func canNavigateForward() -> Bool {
return self.canGoForward
}
// A quick method for loading requests based on strings in a URL format
func loadRequestFromString(urlNameAsString: String!) {
self.loadRequest(NSURLRequest(URL: NSURL(string: urlNameAsString)!))
}
func evaluateJavaScriptString(javascriptString: String!, completionHandler: (AnyObject?, NSError?) -> ()) {
evaluateJavaScript(javascriptString, completionHandler: completionHandler)
}
}