Advertisement
Guest User

Untitled

a guest
Jun 20th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. var web2 : UIWebView?
  2. var connection : NSURLConnection?
  3. var webRequest : NSURLRequest?
  4. var isDone = Bool ()
  5.  
  6. func loadWeb2 (){
  7.  
  8. let webURL = NSURL(string: "private Site")
  9. self.webRequest = NSURLRequest(URL: webURL!)
  10.  
  11. web2 = UIWebView(frame: CGRectMake(0,0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height-10))
  12. loadmyWebview = UIView(frame: CGRectMake(0,0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height-10))
  13. loadmyWebview.backgroundColor = UIColor.whiteColor()
  14. self.view.addSubview(web2!)
  15. self.view.addSubview(loadmyWebview!)
  16. web2!.delegate = self
  17. web2?.scalesPageToFit = true
  18. web2!.loadRequest(webRequest!)
  19. web2!.scrollView.delegate = self;
  20.  
  21. }
  22.  
  23. func webView(webView: UIWebView, didFailLoadWithError error: NSError?)
  24. {
  25. print("=========== ERROR HAPPENED================")
  26. print(error)
  27. }
  28.  
  29. func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool
  30. {
  31.  
  32. if(!isDone){
  33. isDone = false
  34.  
  35. connection = NSURLConnection(request: webRequest!, delegate: self)
  36. connection?.start()
  37. print("request for webview got authintication challenge")
  38. return false
  39. }
  40.  
  41. print("NOOOOO authintication challenge")
  42.  
  43. return true
  44. }
  45.  
  46. func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
  47. if challenge.previousFailureCount == 0 {
  48.  
  49. isDone = true
  50. let credential = NSURLCredential(user:"********", password:"*********", persistence: .Permanent)
  51.  
  52.  
  53. challenge.sender?.useCredential(credential, forAuthenticationChallenge: challenge)
  54. print("IS DONNNNNNE IS TRUE ")
  55.  
  56. }
  57. else {
  58. challenge.sender?.cancelAuthenticationChallenge(challenge)
  59. print("ELSSSSSSE IS DONNNNNNE ")
  60. }
  61.  
  62. }
  63.  
  64. func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse){
  65. print("Authintication is DONe Load Webvieew Again")
  66. isDone = true
  67. web2!.loadRequest(webRequest!)
  68. connection.cancel()
  69. }
  70.  
  71. func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool{
  72.  
  73.  
  74. print("accept untrustd site ")
  75. return true
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement