任何登录失败后都不会加载UIWebView

时间:2014-03-18 14:00:57

标签: ios uiwebview

我正在加载一个具有身份验证的网址所以我使用了以下代码

BOOL _authed = NO;
NSURLAuthenticationChallenge *mChallenge;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request      navigationType:(UIWebViewNavigationType)navigationType;

{

NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authed);

  if (!_authed) {
        _authed = NO;

         [NSURLConnection alloc]initWithRequest:request delegate:self];

         return NO;
    }

    return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
    NSLog(@"got auth challange");


   _authed = YES;        
   mChallenge = challenge;

    UIAlertView *prompt = [[UIAlertView alloc]initWithTitle:nil message:@"Authentication Required" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Login", nil];
    prompt.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    [prompt show];


}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex == 1){
        UITextField *usernameField = [alertView textFieldAtIndex:0];
        UITextField *passwordField = [alertView textFieldAtIndex:1];
        NSString *userName = usernameField.text;
        NSString *password = passwordField.text;
        NSLog(@"%@, %@",userName,password);

        [[mChallenge sender] useCredential:[NSURLCredential credentialWithUser:userName password:password persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:mChallenge];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{


    NSLog(@"response %@",response);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL       URLWithString:@"http://mysharepointurl.aspx"]];
[self.webView loadRequest:request];


}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
{
    return NO;
}

如果我第一次尝试正确的密码,这可以正常工作。 但是,当我第一次尝试使用错误的凭据时,提示再次出现,然后我尝试使用正确的凭据,然后不会加载Web视图

关于此的任何建议

0 个答案:

没有答案