Advertisement
Guest User

Swift SSL

a guest
Mar 8th, 2016
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1.  
  2.  
  3. import UIKit
  4.  
  5. class ViewController: UIViewController {
  6.  
  7. @IBOutlet var webView: UIWebView!
  8. func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool{
  9. print("canAuthenticateAgainstProtectionSpace method Returning True")
  10. return true
  11. }
  12.  
  13.  
  14. func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
  15.  
  16. print("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)")
  17.  
  18. if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
  19. print("send credential Server Trust")
  20. let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
  21. challenge.sender!.useCredential(credential, forAuthenticationChallenge: challenge)
  22.  
  23. }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{
  24. print("send credential HTTP Basic")
  25. let defaultCredentials: NSURLCredential = NSURLCredential(user: "username", password: "password", persistence:NSURLCredentialPersistence.ForSession)
  26. challenge.sender!.useCredential(defaultCredentials, forAuthenticationChallenge: challenge)
  27.  
  28. }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{
  29. print("send credential NTLM")
  30.  
  31. } else{
  32. challenge.sender!.performDefaultHandlingForAuthenticationChallenge!(challenge)
  33. }
  34. }
  35.  
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. webView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
  39.  
  40.  
  41. let urlHttps = NSURL(string: "https://mysslweb.com");
  42. let urlHttp = NSURL(string: "http://google.com");
  43.  
  44. let urlRequest = NSURLRequest(URL: urlHttps!);
  45. let urlConnection:NSURLConnection = NSURLConnection(request: urlRequest, delegate: self)!
  46.  
  47. webView.loadRequest(urlRequest)
  48.  
  49.  
  50. // Do any additional setup after loading the view, typically from a nib.
  51. }
  52.  
  53. override func didReceiveMemoryWarning() {
  54. super.didReceiveMemoryWarning()
  55. // Dispose of any resources that can be recreated.
  56. }
  57.  
  58.  
  59.  
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement