1
+ //
2
+ // SMWebView
3
+ //
4
+ // Created by Shai Mishali on 8/19/15.
5
+ // Copyright (c) 2015 Shai Mishali. All rights reserved.
6
+ //
7
+
8
+ import Foundation
9
+ import UIKit
10
+
11
+ public class SMWebView : UIWebView , UIWebViewDelegate {
12
+ //MARK: Typealiasing for Closure Types
13
+ public typealias SMWebViewClosure = ( webView: SMWebView ) -> ( )
14
+ public typealias SMWebViewFailClosure = ( webView: SMWebView , error: NSError ) -> ( )
15
+ public typealias SMWebViewShouldLoadClosure = ( webView: SMWebView , request: NSURLRequest , navigationType: UIWebViewNavigationType ) -> ( Bool )
16
+
17
+ //MARK: Internal storage for Closures
18
+ internal var __didStartLoadingHandler : SMWebViewClosure ? = nil
19
+ internal var __didFinishLoadingHandler : SMWebViewClosure ? = nil
20
+ internal var __didCompleteLoadingHandler : SMWebViewClosure ? = nil
21
+ internal var __didFailLoadingHandler : SMWebViewFailClosure ? = nil
22
+ internal var __shouldStartLoadingHandler : SMWebViewShouldLoadClosure ? = nil
23
+
24
+ //MARK: Initializers
25
+ override public init ( frame: CGRect ) {
26
+ super. init ( frame: frame)
27
+ self . delegate = self
28
+ }
29
+
30
+ required public init ? ( coder aDecoder: NSCoder ) {
31
+ super. init ( coder: aDecoder)
32
+ self . delegate = self
33
+ }
34
+
35
+ // URL/String loaders with Chaining-support
36
+ public class func loadURL( URL: NSURL ) -> SMWebView {
37
+ let wv = SMWebView ( )
38
+ wv. loadRequest ( NSURLRequest ( URL: URL) )
39
+ return wv
40
+ }
41
+
42
+ public class func loadHTML( string: String ! , baseURL: NSURL ! ) -> SMWebView {
43
+ let wv = SMWebView ( )
44
+ wv. loadHTMLString ( string, baseURL: baseURL)
45
+ return wv
46
+ }
47
+
48
+ public func loadURL( URL: NSURL ) -> SMWebView {
49
+ self . loadRequest ( NSURLRequest ( URL: URL) )
50
+ return self
51
+ }
52
+
53
+ public func loadHTML( string: String ! , baseURL: NSURL ! ) -> SMWebView {
54
+ self . loadHTMLString ( string, baseURL: baseURL)
55
+ return self
56
+ }
57
+
58
+ //MARK: Closure methods
59
+ public func didStartLoading( handler: SMWebViewClosure ) -> SMWebView {
60
+ self . __didStartLoadingHandler = handler
61
+
62
+ return self
63
+ }
64
+
65
+ public func didFinishLoading( handler: SMWebViewClosure ) -> SMWebView {
66
+ self . __didFinishLoadingHandler = handler
67
+ return self
68
+ }
69
+
70
+ public func didFailLoading( handler: SMWebViewFailClosure ) -> SMWebView {
71
+ self . __didFailLoadingHandler = handler
72
+ return self
73
+ }
74
+
75
+ public func didCompleteLoading( handler: SMWebViewClosure ) -> SMWebView {
76
+ self . __didCompleteLoadingHandler = handler
77
+ return self
78
+ }
79
+
80
+ public func shouldStartLoading( handler: SMWebViewShouldLoadClosure ) -> SMWebView {
81
+ self . __shouldStartLoadingHandler = handler
82
+ return self
83
+ }
84
+
85
+ //MARK: UIWebView Delegate & Closure Handling
86
+ public func webView( webView: UIWebView , didFailLoadWithError error: NSError ? ) {
87
+ self . __didFailLoadingHandler ? ( webView: self , error: error!)
88
+ }
89
+
90
+ public func webView( webView: UIWebView , shouldStartLoadWithRequest request: NSURLRequest , navigationType: UIWebViewNavigationType ) -> Bool {
91
+ if self . __shouldStartLoadingHandler != nil {
92
+ return self . __shouldStartLoadingHandler!( webView: self , request: request, navigationType: navigationType)
93
+ }
94
+
95
+ return true
96
+ }
97
+
98
+ public func webViewDidStartLoad( webView: UIWebView ) {
99
+ self . __didStartLoadingHandler ? ( webView: self )
100
+ }
101
+
102
+ public func webViewDidFinishLoad( webView: UIWebView ) {
103
+ self . __didFinishLoadingHandler ? ( webView: self )
104
+
105
+ if !webView. loading {
106
+ self . __didCompleteLoadingHandler ? ( webView: self )
107
+ }
108
+ }
109
+ }
0 commit comments