I am trying to do NTLM authentication with UIWebview. I have researched and I wrote like this. But in my NSLog, I only see but I don't see . 但没有看到 。 Please help me. I am stuck with this for more than 5 days. I am testing with . 进行测试。 I appreciate any help :)
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if([request.URL.host isEqualToString:@"42.61.46.2"])
{
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
NSLog(@"%@",request);
return NO;
}
self.lbl_error.hidden = YES; //hide error message label
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"got auth challange");
if ([challenge previousFailureCount] == 0)
{
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost: @"42.61.46.2"
port: 80
protocol: @"http"
realm: nil authenticationMethod : NSURLAuthenticationMethodNTLM];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential: [NSURLCredential credentialWithUser:@"example" password:@"example" persistence:NSURLCredentialPersistenceForSession] forProtectionSpace:protectionSpace];
}
else
{
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"received response via nsurlconnection");
NSURL* url = [NSURL URLWithString:@"http://42.61.46.2"]; //create a NSURL object
if(activeWebView == nil)
{
//If there is no activeWebview i.e. no webview in the collection, create a new one and show it
[self addWebViewWithURL:url];
}
else
{
NSString *url = @"http://42.61.46.2";
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[activeWebView loadRequest:urlRequest];
}
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
return NO;
}
It worked for me by using this instead for your didReceiveAuthenticationChallange:
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{
NSLog(@"got auth challange");
if ([challenge previousFailureCount] ==0)
{
[[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession] forAuthenticationChallenge:challenge];
}
else
{
NSLog(@"Did cancelAuthenticationChallenge");
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.