|
@@ -14,7 +14,6 @@ public protocol SwiftWebVCDelegate: class {
|
|
|
func didFinishLoading(success: Bool)
|
|
|
func didFinishLoading(success: Bool, url: URL)
|
|
|
func webDismiss()
|
|
|
- func decidePolicyForNavigationAction(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
|
|
|
}
|
|
|
|
|
|
public class SwiftWebVC: UIViewController {
|
|
@@ -335,28 +334,25 @@ extension SwiftWebVC: WKNavigationDelegate {
|
|
|
}
|
|
|
|
|
|
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
|
|
|
-
|
|
|
- self.delegate?.decidePolicyForNavigationAction(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler)
|
|
|
-
|
|
|
- /*
|
|
|
- let url = navigationAction.request.url
|
|
|
-
|
|
|
- if #available(iOS 9.0, *) {
|
|
|
- decisionHandler(.allow)
|
|
|
+ if #available(iOS 11.0, *) {
|
|
|
+ let url = request.url!.host!
|
|
|
+ webView.loadDiskCookies(for: url){
|
|
|
+ decisionHandler(.allow)
|
|
|
+ }
|
|
|
} else {
|
|
|
-
|
|
|
- let userAgent : String = CCUtility.getUserAgent()
|
|
|
-
|
|
|
- if (navigationAction.request.value(forHTTPHeaderField: "User-Agent") == userAgent) {
|
|
|
+ decisionHandler(.allow)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
|
|
|
+ if #available(iOS 11.0, *) {
|
|
|
+ let url = request.url!.host!
|
|
|
+ webView.writeDiskCookies(for: url){
|
|
|
decisionHandler(.allow)
|
|
|
- } else {
|
|
|
- let newRequest : NSMutableURLRequest = navigationAction.request as! NSMutableURLRequest
|
|
|
- newRequest.setValue(userAgent, forHTTPHeaderField: "User-Agent")
|
|
|
- decisionHandler(.cancel)
|
|
|
- webView.load(newRequest as URLRequest)
|
|
|
}
|
|
|
+ } else {
|
|
|
+ decisionHandler(.allow)
|
|
|
}
|
|
|
- */
|
|
|
}
|
|
|
|
|
|
public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
|
|
@@ -367,7 +363,6 @@ extension SwiftWebVC: WKNavigationDelegate {
|
|
|
}
|
|
|
|
|
|
public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
|
|
|
-
|
|
|
if let serverTrust = challenge.protectionSpace.serverTrust {
|
|
|
completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
|
|
|
} else {
|
|
@@ -376,3 +371,70 @@ extension SwiftWebVC: WKNavigationDelegate {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+@available(iOS 11, *)
|
|
|
+
|
|
|
+extension WKWebView {
|
|
|
+
|
|
|
+ enum PrefKey {
|
|
|
+ static let cookie = "cookies"
|
|
|
+ }
|
|
|
+
|
|
|
+ func writeDiskCookies(for domain: String, completion: @escaping () -> ()) {
|
|
|
+ fetchInMemoryCookies(for: domain) { data in
|
|
|
+ print("write cookies data", data)
|
|
|
+ UserDefaults.standard.setValue(data, forKey: PrefKey.cookie + domain)
|
|
|
+ completion();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func loadDiskCookies(for domain: String, completion: @escaping () -> ()) {
|
|
|
+ if let diskCookie = UserDefaults.standard.dictionary(forKey: (PrefKey.cookie + domain)){
|
|
|
+ fetchInMemoryCookies(for: domain) { freshCookie in
|
|
|
+
|
|
|
+ print("read cookies data", freshCookie)
|
|
|
+
|
|
|
+ let mergedCookie = diskCookie.merging(freshCookie) { (_, new) in new }
|
|
|
+
|
|
|
+ for (_, cookieConfig) in mergedCookie {
|
|
|
+ let cookie = cookieConfig as! Dictionary<String, Any>
|
|
|
+
|
|
|
+ var expire : Any? = nil
|
|
|
+
|
|
|
+ if let expireTime = cookie["Expires"] as? Double{
|
|
|
+ expire = Date(timeIntervalSinceNow: expireTime)
|
|
|
+ }
|
|
|
+
|
|
|
+ let newCookie = HTTPCookie(properties: [
|
|
|
+ .domain: cookie["Domain"] as Any,
|
|
|
+ .path: cookie["Path"] as Any,
|
|
|
+ .name: cookie["Name"] as Any,
|
|
|
+ .value: cookie["Value"] as Any,
|
|
|
+ .secure: cookie["Secure"] as Any,
|
|
|
+ .expires: expire as Any
|
|
|
+ ])
|
|
|
+
|
|
|
+ self.configuration.websiteDataStore.httpCookieStore.setCookie(newCookie!)
|
|
|
+ }
|
|
|
+
|
|
|
+ completion()
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ completion()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func fetchInMemoryCookies(for domain: String, completion: @escaping ([String: Any]) -> ()) {
|
|
|
+ var cookieDict = [String: AnyObject]()
|
|
|
+ WKWebsiteDataStore.default().httpCookieStore.getAllCookies { (cookies) in
|
|
|
+ for cookie in cookies {
|
|
|
+ if cookie.domain.contains(domain) {
|
|
|
+ cookieDict[cookie.name] = cookie.properties as AnyObject?
|
|
|
+ }
|
|
|
+ }
|
|
|
+ completion(cookieDict)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|