Taj rajan
Taj rajan

Reputation: 631

Webview not working after didReceiveAuthenticationChallenge fail

i pressed button call to bellow button action function

- (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) Press button call to buttonAction:
2) will appear authenticaion login view
3) give wrong username and password press 'login' than re-showed authentication login view
4) give current username and password than call to
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

webview load is started. But not display

Upvotes: 1

Views: 695

Answers (1)

hirenhcm
hirenhcm

Reputation: 93

I also had the same issue. I fixed mine by taking reference of this answer here

and adding one more delegate of NSURLConnection

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

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

Hope this fixes your issue.

Upvotes: 1

Related Questions