Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import Security
- class backends: UIViewController, NSURLConnectionDelegate, UIWebViewDelegate {
- @IBOutlet weak var webview: UIWebView!
- var loadingUnvalidatedHTTPSPage:Bool!
- var connection:NSURLConnection!
- let path = "https://dev.app.masjidia.com/admin/login"
- override func viewDidLoad() {
- super.viewDidLoad()
- webview.delegate = self
- webview.scalesPageToFit = true
- webview.contentMode = .scaleAspectFit
- let requestObj = NSURLRequest.init(url: URL(string: path)!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10.0)
- let conn:NSURLConnection = NSURLConnection(request: requestObj as URLRequest, delegate: self)!
- conn.start()
- self.loadingUnvalidatedHTTPSPage = true
- webview.loadRequest(requestObj as URLRequest)
- }
- private func webView(webView: UIWebView!, didFailLoadWithError error: NSError!) {
- print("Webview fail with error \(error)");
- if(error.domain == NSURLErrorDomain){
- if (error.code == NSURLErrorServerCertificateHasBadDate || error.code == NSURLErrorServerCertificateUntrusted ||
- error.code == NSURLErrorServerCertificateHasUnknownRoot || error.code == NSURLErrorServerCertificateNotYetValid) {
- print("\n ---- :C ....")
- }
- }
- }
- private func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) ->Bool{
- print("Webview iniciando");
- if (self.loadingUnvalidatedHTTPSPage!) {
- self.connection = NSURLConnection(request: request as URLRequest, delegate: self)
- self.connection.start();
- return false;
- }
- return true;
- }
- private func webViewDidStartLoad(webView: UIWebView!) {
- print("Webview started Loading")
- }
- private func webViewDidFinishLoad(webView: UIWebView!) {
- print("Webview did finish load")
- }
- // MARK - NSURLConnectionDelegate methods
- func connection(_ connection: NSURLConnection, willSendRequestFor challenge: URLAuthenticationChallenge) {
- let trust:SecTrust = challenge.protectionSpace.serverTrust!;
- let cred:URLCredential = URLCredential(trust: trust)
- challenge.sender?.use(cred, for: challenge)
- }
- func connection(_ connection: NSURLConnection, NSURLConnection response:URLResponse){
- let requestObj:NSURLRequest = NSURLRequest(url: URL(string: path)!, cachePolicy: NSURLRequest.CachePolicy.returnCacheDataElseLoad, timeoutInterval: 20.0)
- self.loadingUnvalidatedHTTPSPage = false
- self.webview.loadRequest(requestObj as URLRequest)
- self.connection.cancel()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement