2013-02-02 179 views
1

我正在嘗試使用UIWebview進行NTLM身份驗證。我研究過,而且我是這樣寫的。但在我的NSLog中,我只看到「有auth challange」,但我沒有看到「通過nsurlconnection接收到響應」。請幫幫我。我堅持了5天以上。我正在使用ios 6.1進行測試。我感謝所有幫助:)NTLM身份驗證Xcode

-(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; 
} 

回答

0

它爲我用這個代替您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]; 
} 

}