在didReceiveAuthenticationChallenge失败后,Webview无法正常工作

时间:2014-03-21 12:17:23

标签: ios uiwebview

按下按钮调用按钮按钮动作功能

- (IBAction)buttonAction:(UIButton *)sender {
    NSURL *URL = [NSURL URLWithString:@"<url>”];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL
                                             cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                         timeoutInterval:30.0];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge previousFailureCount] == 0) {
        _alert = [[EYAlertView alloc] init];
        [_alert initWithTitle:@"Authentication Required" message:@“<url>” block:^(Authentication *login) {
            if(login!=nil){
                NSURLCredential *newCredential = [NSURLCredential credentialWithUser:login.username
                                                                            password:login.password
                                                                         persistence:NSURLCredentialPersistenceSynchronizable];
                [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];

            }
        }];
    }
    else {
        NSLog(@“Login failed”);
        NSURL *URL = [NSURL URLWithString:@“<url>”];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL
                                                 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                             timeoutInterval:30.0];

        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [connection start];

    }
}

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

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSString *fullURL = @“<url>”;
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webview loadRequest:requestObj];
}

1)按下按钮调用按钮操作:
2)将出现认证登录视图
3)输入错误的用户名和密码按“登录”#39;比重新显示认证登录视图
4)提供当前用户名和密码,而不是打电话给
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

启动webview加载。但不显示

1 个答案:

答案 0 :(得分:1)

我也有同样的问题。我通过参考这个答案here

来修复我的

再添加一个NSURLConnection委托

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace{
NSLog(@"protectionSpace send yes");
return YES;
}

- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;{
//Modified this to YES.
return YES;
}

希望这可以解决您的问题。