-
-
Save saiday/9689ae6c15e15663a0255bea09fa7335 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SAWKWebView.swift | |
// | |
// Created by Costantino Pistagna on 24/01/2020. | |
// Copyright © 2020 Sofapps. All rights reserved. | |
// | |
import UIKit | |
import WebKit | |
class SAWKWebView: WKWebView, URLSessionDelegate, URLSessionTaskDelegate { | |
func load(_ request: URLRequest, handleRedirect: Bool = false, with cookies: [HTTPCookie]?) -> WKNavigation? { | |
HTTPCookieStorage.shared.cookieAcceptPolicy = .always | |
if handleRedirect == false { | |
var request = request | |
if let cookies = cookies { | |
let headers = HTTPCookie.requestHeaderFields(with: cookies) | |
for (name, value) in headers { | |
request.addValue(value, forHTTPHeaderField: name) | |
} | |
} | |
return load(request) | |
} else { | |
let sessionConfig: URLSessionConfiguration = URLSessionConfiguration.default | |
let session = URLSession(configuration: sessionConfig, delegate: self, delegateQueue: nil) | |
sessionConfig.httpAdditionalHeaders = ["User-Agent" : "Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1"] | |
session.dataTask(with: request) { (data, response, error) in | |
if let _ = error { | |
} else if let data = data, let tmpHtml = String(data: data, encoding: .utf8) { | |
DispatchQueue.main.async { | |
self.loadHTMLString(tmpHtml, baseURL: response?.url) | |
} | |
} | |
}.resume() | |
} | |
return nil | |
} | |
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Swift.Void) { | |
// The original request was redirected but we still need to grab cookies | |
if let headers = response.allHeaderFields as? [String:String], let url = response.url { | |
let cookies = HTTPCookie.cookies(withResponseHeaderFields: headers, for: url) | |
for aCookie in cookies { | |
HTTPCookieStorage.shared.setCookie(aCookie) | |
} | |
} | |
let sessionConfig: URLSessionConfiguration = URLSessionConfiguration.default | |
let session = URLSession(configuration: sessionConfig, delegate: self, delegateQueue: nil) | |
sessionConfig.httpAdditionalHeaders = ["User-Agent" : "Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1"] | |
session.dataTask(with: request) { (data, response, error) in | |
if let _ = error { | |
} else if let data = data, let tmpHtml = String(data: data, encoding: .utf8) { | |
DispatchQueue.main.async { | |
self.loadHTMLString(tmpHtml, baseURL: response?.url) | |
} | |
} | |
}.resume() | |
} | |
func load(_ request: URLRequest, with cookies: [HTTPCookie]) { | |
var request = request | |
let headers = HTTPCookie.requestHeaderFields(with: cookies) | |
for (name, value) in headers { | |
request.addValue(value, forHTTPHeaderField: name) | |
} | |
load(request) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment