@@ -18,6 +18,8 @@ public class LoginWebView: WKWebView, WKNavigationDelegate {
18
18
/// Called once the flow is completed.
19
19
var completionHandler : ( ( Result < [ HTTPCookie ] , Error > ) -> Void ) !
20
20
21
+ private var cookieTimer : Timer ?
22
+
21
23
// MARK: Init
22
24
@available ( * , unavailable, message: " using a custom `userAgent` is no longer supported " )
23
25
public init ( frame: CGRect , userAgent: String ? , didReachEndOfLoginFlow: ( ( ) -> Void ) ? = nil ) {
@@ -34,6 +36,7 @@ public class LoginWebView: WKWebView, WKNavigationDelegate {
34
36
self . didReachEndOfLoginFlow = didReachEndOfLoginFlow
35
37
super. init ( frame: frame, configuration: configuration)
36
38
self . navigationDelegate = self
39
+ setCookieTimer ( )
37
40
}
38
41
39
42
@available ( * , unavailable, message: " use `init(frame:didReachEndOfLoginFlow:)` instead. " )
@@ -79,9 +82,10 @@ public class LoginWebView: WKWebView, WKNavigationDelegate {
79
82
}
80
83
81
84
// MARK: Clean cookies
82
- private func fetchCookies( ) {
83
- configuration. websiteDataStore. httpCookieStore. getAllCookies { [ weak self] in
84
- self ? . completionHandler ? ( . success( $0) )
85
+
86
+ private func setCookieTimer( ) {
87
+ cookieTimer = Timer . scheduledTimer ( withTimeInterval: 1 , repeats: true ) { [ weak self] _ in
88
+ self ? . tryFetchingCookies ( )
85
89
}
86
90
}
87
91
@@ -99,6 +103,9 @@ public class LoginWebView: WKWebView, WKNavigationDelegate {
99
103
self ? . navigationDelegate = nil
100
104
// notify user.
101
105
self ? . completionHandler ? ( . success( $0) )
106
+ // deinit timer
107
+ self ? . cookieTimer? . invalidate ( )
108
+ self ? . cookieTimer = nil
102
109
}
103
110
}
104
111
@@ -109,20 +116,5 @@ public class LoginWebView: WKWebView, WKNavigationDelegate {
109
116
completionHandler: completionHandler)
110
117
}
111
118
112
- // MARK: Navigation delegate
113
- public func webView( _ webView: WKWebView , didFinish navigation: WKNavigation ! ) {
114
- switch webView. url? . absoluteString {
115
- case " https://www.instagram.com/ " ? :
116
- // notify user.
117
- didReachEndOfLoginFlow ? ( )
118
- // fetch cookies.
119
- fetchCookies ( )
120
- // no need to check anymore.
121
- navigationDelegate = nil
122
- default :
123
- // try fetching cookies.
124
- tryFetchingCookies ( )
125
- }
126
- }
127
119
}
128
120
#endif
0 commit comments