0

I've the following problem:

My app has an UIWebView and within this web view I want to visit a website which is protected with SSL. The ssl certificate is self-signed

I've implemented the following methods:

shouldStartLoadWithRequest:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
    if(![self authenticated])
    {
        [self setAuthenticated:NO];
        [self setUrlConnection:[[NSURLConnection alloc] initWithRequest:[self requestObj] delegate:self]];
        [[self urlConnection] start];
        return NO;
    }
    return YES;
}

didReceiveAuthenticationChallenge:

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if([challenge previousFailureCount] == 0)
    {
        [self setAuthenticated:YES];
        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
    }
    else [[challenge sender] cancelAuthenticationChallenge:challenge];
}

didReceiveResponse:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
    [self setAuthenticated:YES];
    [[self webView] loadRequest:[self requestObj]];
    [[self urlConnection] cancel];
}

canAuthenticateAgainstProtectionSpace:

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

Did I forget some function or a line of code? If I visit the website I got the error

2016-05-03 13:46:35.819 App[2121:806751] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
2016-05-03 13:46:35.965 App[2121:806745] CFNetwork SSLHandshake failed (-9824 -> -9829)
2016-05-03 13:46:35.967 App[2121:806745] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9829)

The website should display the error I get from the server for some seconds and load the website after all.

EDIT:

I also added following lines in my Info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

And the server is configured correct (https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html?hl=fi) at the point "Requirements for Connecting Using ATS"

Community
  • 1
  • 1
Premox
  • 323
  • 10
  • 25

0 Answers0