Skip to content

Commit

Permalink
Update to 2.5.0
Browse files Browse the repository at this point in the history
* Make login cookies check more robust

* Bump pod version number
  • Loading branch information
pxrne authored May 16, 2021
1 parent 756bf2e commit 53bc4ac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
2 changes: 1 addition & 1 deletion SwiftyInsta.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "SwiftyInsta"
s.version = "2.4.3"
s.version = "2.5.0"
s.summary = "Private and Tokenless Instagram RESTful API."

s.homepage = "https://github.com/TheM4hd1/SwiftyInsta"
Expand Down
28 changes: 10 additions & 18 deletions SwiftyInsta/UI/LoginWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class LoginWebView: WKWebView, WKNavigationDelegate {
/// Called once the flow is completed.
var completionHandler: ((Result<[HTTPCookie], Error>) -> Void)!

private var cookieTimer: Timer?

// MARK: Init
@available(*, unavailable, message: "using a custom `userAgent` is no longer supported")
public init(frame: CGRect, userAgent: String?, didReachEndOfLoginFlow: (() -> Void)? = nil) {
Expand All @@ -34,6 +36,7 @@ public class LoginWebView: WKWebView, WKNavigationDelegate {
self.didReachEndOfLoginFlow = didReachEndOfLoginFlow
super.init(frame: frame, configuration: configuration)
self.navigationDelegate = self
setCookieTimer()
}

@available(*, unavailable, message: "use `init(frame:didReachEndOfLoginFlow:)` instead.")
Expand Down Expand Up @@ -79,9 +82,10 @@ public class LoginWebView: WKWebView, WKNavigationDelegate {
}

// MARK: Clean cookies
private func fetchCookies() {
configuration.websiteDataStore.httpCookieStore.getAllCookies { [weak self] in
self?.completionHandler?(.success($0))

private func setCookieTimer() {
cookieTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
self?.tryFetchingCookies()
}
}

Expand All @@ -99,6 +103,9 @@ public class LoginWebView: WKWebView, WKNavigationDelegate {
self?.navigationDelegate = nil
// notify user.
self?.completionHandler?(.success($0))
// deinit timer
self?.cookieTimer?.invalidate()
self?.cookieTimer = nil
}
}

Expand All @@ -109,20 +116,5 @@ public class LoginWebView: WKWebView, WKNavigationDelegate {
completionHandler: completionHandler)
}

// MARK: Navigation delegate
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
switch webView.url?.absoluteString {
case "https://www.instagram.com/"?:
// notify user.
didReachEndOfLoginFlow?()
// fetch cookies.
fetchCookies()
// no need to check anymore.
navigationDelegate = nil
default:
// try fetching cookies.
tryFetchingCookies()
}
}
}
#endif

0 comments on commit 53bc4ac

Please sign in to comment.