Search code examples
iossslhttpscredentialsmknetworkkit

iOS: Encountered an error kCFURLErrorUserCancelledAuthentication -1012?


In iOS application is required to sign a certificate request, which had previously been obtained. When I try to run a query catch this error:

kCFURLErrorUserCancelledAuthentication -1012.

The documentation says:

kCFURLErrorUserCancelledAuthentication The connection failed because the user cancelled required authentication.

Implemented as follows:

- (void)startConnection {
    NSString *serverURL = @"host.ru/method";
    MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:serverURL customHeaderFields:nil];
    MKNetworkOperation *op = [engine operationWithPath:nil params:nil httpMethod:@"GET" ssl:YES];
    NSString *thePath = [[NSBundle mainBundle] pathForResource:@"client" ofType:@"p12"];
    [op setShouldContinueWithInvalidCertificate:YES];
    op.clientCertificate = thePath;
    op.clientCertificatePassword = @"1234qwerty";

    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        NSLog(@"[operation responseData]-->>%@", [operation responseString]);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
    }];

    [engine enqueueOperation:op];   
}

What am I doing wrong?

P.S.

Certificate, which try to sign the request has been received in advance. It tested separately in the browser, it's okay.

An application for android to the same server requests are normally the same scheme.


Solution

  • This can happen when your connection sends a request for an authentication challenge.

    A possible cause is that the site's certificate is invalid/untrusted and you have opted not to accept invalid certificates.