user3544731
user3544731

Reputation: 21

UIWebView Authentication not working anymore in iOS 7.1

I have some serious problems with my application since upgrading to iOS 7.1. The webview and the authentication challenge worked fine. Since I have upgraded, the webview doesn't display the content anymore. The authentication seems to work fine as I am getting the right values through debugging + NSLog, so invalid credentials can't be the problem (checked them about 10 times). After the webview reloads the page with the valid credentials, still no content is displayed. Here is my code:

- (void)viewDidLoad
{
    _authed = NO;
    [self.webView loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString:@"xxx"]]];
}

- (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 willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{

    if ([challenge previousFailureCount] > 0) {

    }
    else
    {

        NSURLCredential *credential = [NSURLCredential credentialWithUser:@"xxx"
                                                                 password:@"xxx"
                                                              persistence:NSURLCredentialPersistencePermanent];
        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
    }

}

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

    if ([challenge previousFailureCount] == 0) {
        _authed = YES;
        [[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"xxx" password:@"xxx"  persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];
    }
    else
        [[challenge sender] cancelAuthenticationChallenge:challenge];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
    NSLog(@"received response via nsurlconnection");

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString: @"xxx"]];

    [self.webView loadRequest:urlRequest];
    _authed = YES;
}


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

NSLog output:

2014-04-17 12:23:20.418 Test[510:60b] Did start loading: xxx auth:0

2014-04-17 12:23:20.704 Test[510:60b] received response via nsurlconnection

2014-04-17 12:23:20.710 Test[510:60b] Did start loading: xxx auth:1

Thanks in advance.

Upvotes: 2

Views: 630

Answers (1)

AbBarjoud
AbBarjoud

Reputation: 126

You can pass authentication in url instead of request header please try instead of BASE_URL: NSString* urlString = [NSString stringWithFormat:@"%@//%@:%@@%@",@"http:",userName,password,myUrl]

Upvotes: 1

Related Questions