Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.85 KB | None | 0 0
  1. import UIKit
  2. import Security
  3.  
  4. class backends: UIViewController, NSURLConnectionDelegate, UIWebViewDelegate {
  5.  
  6.     @IBOutlet weak var webview: UIWebView!
  7.     var loadingUnvalidatedHTTPSPage:Bool!
  8.     var connection:NSURLConnection!
  9.     let path = "https://dev.app.masjidia.com/admin/login"
  10.    
  11.     override func viewDidLoad() {
  12.         super.viewDidLoad()
  13.        
  14.         webview.delegate = self
  15.         webview.scalesPageToFit = true
  16.         webview.contentMode = .scaleAspectFit
  17.        
  18.         let requestObj = NSURLRequest.init(url: URL(string: path)!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10.0)
  19.         let conn:NSURLConnection = NSURLConnection(request: requestObj as URLRequest, delegate: self)!
  20.         conn.start()
  21.        
  22.         self.loadingUnvalidatedHTTPSPage = true
  23.         webview.loadRequest(requestObj as URLRequest)
  24.        
  25.     }
  26.    
  27.     private func webView(webView: UIWebView!, didFailLoadWithError error: NSError!) {
  28.         print("Webview fail with error \(error)");
  29.        
  30.         if(error.domain == NSURLErrorDomain){
  31.             if (error.code == NSURLErrorServerCertificateHasBadDate || error.code == NSURLErrorServerCertificateUntrusted   ||
  32.                 error.code == NSURLErrorServerCertificateHasUnknownRoot || error.code == NSURLErrorServerCertificateNotYetValid) {
  33.                 print("\n ---- :C ....")
  34.             }
  35.         }
  36.     }
  37.    
  38.     private func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) ->Bool{
  39.         print("Webview iniciando");
  40.         if (self.loadingUnvalidatedHTTPSPage!) {
  41.             self.connection = NSURLConnection(request: request as URLRequest, delegate: self)
  42.             self.connection.start();
  43.             return false;
  44.         }
  45.         return true;
  46.     }
  47.    
  48.     private func webViewDidStartLoad(webView: UIWebView!) {
  49.         print("Webview started Loading")
  50.     }
  51.    
  52.     private func webViewDidFinishLoad(webView: UIWebView!) {
  53.         print("Webview did finish load")
  54.     }
  55.    
  56.    
  57.     // MARK - NSURLConnectionDelegate methods
  58.     func connection(_ connection: NSURLConnection, willSendRequestFor challenge: URLAuthenticationChallenge) {
  59.         let trust:SecTrust = challenge.protectionSpace.serverTrust!;
  60.         let cred:URLCredential = URLCredential(trust: trust)
  61.         challenge.sender?.use(cred, for: challenge)
  62.     }
  63.    
  64.     func connection(_ connection: NSURLConnection, NSURLConnection response:URLResponse){
  65.         let requestObj:NSURLRequest = NSURLRequest(url: URL(string: path)!, cachePolicy: NSURLRequest.CachePolicy.returnCacheDataElseLoad, timeoutInterval: 20.0)
  66.         self.loadingUnvalidatedHTTPSPage = false
  67.         self.webview.loadRequest(requestObj as URLRequest)
  68.         self.connection.cancel()
  69.     }
  70.    
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement