|
@@ -1,26 +1,6 @@
|
|
|
//
|
|
|
// WKCookieWebView.swift
|
|
|
//
|
|
|
-// Created by Jens Reynders on 30/03/2018.
|
|
|
-// Copyright (c) 2018 November Five
|
|
|
-//
|
|
|
-// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
-// of this software and associated documentation files (the "Software"), to deal
|
|
|
-// in the Software without restriction, including without limitation the rights
|
|
|
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
-// copies of the Software, and to permit persons to whom the Software is
|
|
|
-// furnished to do so, subject to the following conditions:
|
|
|
-//
|
|
|
-// The above copyright notice and this permission notice shall be included in all
|
|
|
-// copies or substantial portions of the Software.
|
|
|
-//
|
|
|
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
-// SOFTWARE.
|
|
|
|
|
|
|
|
|
import Foundation
|
|
@@ -58,18 +38,12 @@ class WKCookieWebView : WKWebView {
|
|
|
self.syncCookiesInJS()
|
|
|
if let data = data, let response = response {
|
|
|
let _ = self.webViewLoad(data: data, response: response)
|
|
|
- } else {
|
|
|
- self.syncCookies(newRequest, nil, { (cookieRequest) in
|
|
|
- let _ = super.load(cookieRequest)
|
|
|
- })
|
|
|
}
|
|
|
}
|
|
|
}, failure: {
|
|
|
// let WKWebView handle the network error
|
|
|
DispatchQueue.main.async {
|
|
|
- self.syncCookies(request, nil, { (newRequest) in
|
|
|
- let _ = super.load(newRequest)
|
|
|
- })
|
|
|
+ let _ = super.load(request)
|
|
|
}
|
|
|
})
|
|
|
|
|
@@ -84,26 +58,7 @@ class WKCookieWebView : WKWebView {
|
|
|
failure()
|
|
|
} else {
|
|
|
if let response = response as? HTTPURLResponse {
|
|
|
-
|
|
|
- let code = response.statusCode
|
|
|
- if code == 200 {
|
|
|
- // for code 200 return data to load data directly
|
|
|
- success(request, response, data)
|
|
|
-
|
|
|
- /*
|
|
|
- } else if code >= 300 && code < 400 {
|
|
|
- // for redirect get location in header,and make a new URLRequest
|
|
|
- guard let location = response.allHeaderFields["Location"] as? String, let redirectURL = URL(string: location) else {
|
|
|
- failure()
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- let request = URLRequest(url: redirectURL, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5)
|
|
|
- success(request, nil, nil)
|
|
|
- */
|
|
|
- } else {
|
|
|
- success(request, response, data)
|
|
|
- }
|
|
|
+ success(request, response, data)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -123,36 +78,7 @@ class WKCookieWebView : WKWebView {
|
|
|
}
|
|
|
|
|
|
extension WKCookieWebView {
|
|
|
- // sync HTTPCookieStorage cookies to URLRequest
|
|
|
- private func syncCookies(_ request: URLRequest, _ task: URLSessionTask? = nil, _ completion: @escaping (URLRequest) -> Void) {
|
|
|
- var request = request
|
|
|
- var cookiesArray = [HTTPCookie]()
|
|
|
-
|
|
|
- if let task = task {
|
|
|
- HTTPCookieStorage.shared.getCookiesFor(task, completionHandler: { (cookies) in
|
|
|
- if let cookies = cookies {
|
|
|
- cookiesArray.append(contentsOf: cookies)
|
|
|
-
|
|
|
- let cookieDict = HTTPCookie.requestHeaderFields(with: cookiesArray)
|
|
|
- if let cookieStr = cookieDict["Cookie"] {
|
|
|
- request.addValue(cookieStr, forHTTPHeaderField: "Cookie")
|
|
|
- }
|
|
|
- }
|
|
|
- completion(request)
|
|
|
- })
|
|
|
- } else if let url = request.url {
|
|
|
- if let cookies = HTTPCookieStorage.shared.cookies(for: url) {
|
|
|
- cookiesArray.append(contentsOf: cookies)
|
|
|
- }
|
|
|
- let cookieDict = HTTPCookie.requestHeaderFields(with: cookiesArray)
|
|
|
- if let cookieStr = cookieDict["Cookie"] {
|
|
|
- request.addValue(cookieStr, forHTTPHeaderField: "Cookie")
|
|
|
- }
|
|
|
- completion(request)
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
// MARK: - JS Cookie handling
|
|
|
private func syncCookiesInJS(for request: URLRequest? = nil) {
|
|
|
if let url = request?.url,
|
|
@@ -191,9 +117,6 @@ extension WKCookieWebView {
|
|
|
extension WKCookieWebView : URLSessionTaskDelegate {
|
|
|
|
|
|
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
|
|
|
-
|
|
|
- syncCookies(request) { (newRequest) in
|
|
|
- completionHandler(newRequest)
|
|
|
- }
|
|
|
+ completionHandler(request)
|
|
|
}
|
|
|
}
|