thieumao

Open WebView need Authentication

Mar 9th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.m
  3. //  OpenWebViewNeedAuthentication
  4. //
  5. //  Created by Thieu Mao on 3/10/17.
  6. //  Copyright © 2017 thieumao. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. @interface ViewController ()
  12. @property (weak, nonatomic) IBOutlet UIWebView *webView;
  13. @property (strong, nonatomic) NSMutableURLRequest *request;
  14. @end
  15.  
  16. @implementation ViewController
  17.  
  18. - (void)viewDidLoad {
  19.     [super viewDidLoad];
  20.     NSURL *url = [NSURL URLWithString:@"http://42.119.128.237/"];
  21.     self.request = [NSMutableURLRequest requestWithURL:url
  22.         cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:12];
  23.     [self.webView loadRequest:self.request];
  24.     [NSURLConnection connectionWithRequest:self.request delegate:self];
  25. }
  26.  
  27. - (void)connection:(NSURLConnection *)connection
  28.     didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  29.     NSURLCredential *credential = [NSURLCredential credentialWithUser:@"admin" password:@"test"
  30.         persistence:NSURLCredentialPersistenceForSession];
  31.     [[NSURLCredentialStorage sharedCredentialStorage]setCredential:credential
  32.         forProtectionSpace:[challenge protectionSpace]];
  33.     [self.webView loadRequest:self.request];
  34. }
  35.  
  36. @end
Add Comment
Please, Sign In to add comment