Advertisement
thieumao

Test

Mar 9th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. //
  2. // ViewController.m
  3. // TestWebView
  4. //
  5. // Created by Thieu Mao on 3/9/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. @end
  14.  
  15. @implementation ViewController
  16.  
  17. //BOOL isauth;
  18. //- (void)viewDidLoad
  19. //{
  20. //
  21. // // Load the web view with your url
  22. // [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://42.119.128.237/"]]];
  23. // isauth = NO;
  24. //}
  25. //
  26. //- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
  27. //{
  28. //
  29. //// if (!isauth) {
  30. //// isauth = NO;
  31. //// [[NSURLConnection alloc] initWithRequest:request delegate:self];
  32. //// return NO;
  33. //// }
  34. // [[NSURLConnection alloc] initWithRequest:request delegate:self];
  35. // return NO;
  36. //}
  37. //
  38. //
  39. ////in nsurlconnections delegate. This one deals with the authentication challenge. this time set isauth to YES
  40. //
  41. //- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
  42. //{
  43. // [[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"admin" password:@"test" persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];
  44. //
  45. //// if ([challenge previousFailureCount] == 0) {
  46. //// isauth = YES;
  47. //// [[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"admin" password:@"test" persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];
  48. //// }
  49. //// else
  50. //// [[challenge sender] cancelAuthenticationChallenge:challenge];
  51. //}
  52. //
  53. //// 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.
  54. //
  55. //- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
  56. //{
  57. // NSLog(@"received response via nsurlconnection");
  58. //
  59. // NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://42.119.128.237/"]];
  60. //
  61. // [_webView loadRequest:urlRequest];
  62. //}
  63. //
  64. //- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
  65. //{
  66. // return NO;
  67. //}
  68.  
  69. - (void)viewDidLoad {
  70. [super viewDidLoad];
  71. // _webView.delegate = self;
  72. //basic HTTP authentication
  73. NSURL *url = [NSURL URLWithString:@"http://vietnamnet.vn/"];
  74. //@"http://42.119.128.237/"];
  75. //@"http://vietnamnet.vn/
  76. NSMutableURLRequest *request;
  77. request = [NSMutableURLRequest requestWithURL:url
  78. cachePolicy:NSURLRequestReloadIgnoringCacheData
  79. timeoutInterval:12];
  80. [_webView loadRequest:request];
  81. }
  82.  
  83. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
  84. {
  85.  
  86. // if (!isauth) {
  87. // isauth = NO;
  88. // [[NSURLConnection alloc] initWithRequest:request delegate:self];
  89. // return NO;
  90. // }
  91. [[NSURLConnection alloc] initWithRequest:request delegate:self];
  92. return NO;
  93. }
  94.  
  95.  
  96. //Then
  97. - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
  98. {
  99. NSURLCredential * cred = [NSURLCredential credentialWithUser:@"admin"
  100. password:@"test"
  101. persistence:NSURLCredentialPersistenceForSession];
  102. [[NSURLCredentialStorage sharedCredentialStorage]setCredential:cred forProtectionSpace:[challenge protectionSpace]];
  103.  
  104. }
  105.  
  106. - (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
  107. {
  108. return YES;
  109. }
  110.  
  111. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement