Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.m
- // TestWebView
- //
- // Created by Thieu Mao on 3/9/17.
- // Copyright © 2017 thieumao. All rights reserved.
- //
- #import "ViewController.h"
- @interface ViewController ()
- @property (weak, nonatomic) IBOutlet UIWebView *webView;
- @end
- @implementation ViewController
- //BOOL isauth;
- //- (void)viewDidLoad
- //{
- //
- // // Load the web view with your url
- // [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://42.119.128.237/"]]];
- // isauth = NO;
- //}
- //
- //- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
- //{
- //
- //// if (!isauth) {
- //// isauth = NO;
- //// [[NSURLConnection alloc] initWithRequest:request delegate:self];
- //// return NO;
- //// }
- // [[NSURLConnection alloc] initWithRequest:request delegate:self];
- // return NO;
- //}
- //
- //
- ////in nsurlconnections delegate. This one deals with the authentication challenge. this time set isauth to YES
- //
- //- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
- //{
- // [[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"admin" password:@"test" persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];
- //
- //// if ([challenge previousFailureCount] == 0) {
- //// isauth = YES;
- //// [[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"admin" password:@"test" persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];
- //// }
- //// else
- //// [[challenge sender] cancelAuthenticationChallenge:challenge];
- //}
- //
- //// if the authentication is successfully handled than you will get data in this method in which you can reload web view with same request again.
- //
- //- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- //{
- // NSLog(@"received response via nsurlconnection");
- //
- // NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://42.119.128.237/"]];
- //
- // [_webView loadRequest:urlRequest];
- //}
- //
- //- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
- //{
- // return NO;
- //}
- - (void)viewDidLoad {
- [super viewDidLoad];
- // _webView.delegate = self;
- //basic HTTP authentication
- NSURL *url = [NSURL URLWithString:@"http://vietnamnet.vn/"];
- //@"http://42.119.128.237/"];
- //@"http://vietnamnet.vn/
- NSMutableURLRequest *request;
- request = [NSMutableURLRequest requestWithURL:url
- cachePolicy:NSURLRequestReloadIgnoringCacheData
- timeoutInterval:12];
- [_webView loadRequest:request];
- }
- - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
- {
- // if (!isauth) {
- // isauth = NO;
- // [[NSURLConnection alloc] initWithRequest:request delegate:self];
- // return NO;
- // }
- [[NSURLConnection alloc] initWithRequest:request delegate:self];
- return NO;
- }
- //Then
- - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
- {
- NSURLCredential * cred = [NSURLCredential credentialWithUser:@"admin"
- password:@"test"
- persistence:NSURLCredentialPersistenceForSession];
- [[NSURLCredentialStorage sharedCredentialStorage]setCredential:cred forProtectionSpace:[challenge protectionSpace]];
- }
- - (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
- {
- return YES;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement