1

I am trying to call a web service using NSURLSession and NSURLSessionDataTask, the site is an HTTPS and using a self-signed certificate, I tried to set the delegate for NSURLSession and call its method to let the request proceed, but it doesn't work too.

I set the App transport security also in Info.plist and doesn't work too, and its only for testing purposes, i know its dangerous to use a self-signed certificate, please any help.

here is a sample code for calling the webservice:

- (void) operationRequest{
    NSMutableArray *extraDataRequest = [[NSMutableArray alloc] init];
    [extraDataRequest addObject:[mPayRequest prepareExtraDataWithKey:systemConstantsObj.section andValue:@"Dinarak"]];

    NSNumber *defaultLanguage = [NSNumber numberWithInt:1];
    NSDictionary *newDatasetInfo = [mPayRequest mPayRequestJsonObject:deviceId extraData:extraDataRequest language:defaultLanguage operation:systemConstantsObj.systemConfiguration pinCode:[NSNull null] sender:deviceId senderType:systemConstantsObj.mobileSenderType];

    NSString *stringValues = [mPayRequest ToStringValues:deviceId extraData:extraDataRequest language:defaultLanguage operation:systemConstantsObj.systemConfiguration pinCode:[NSNull null] sender:deviceId senderType:systemConstantsObj.mobileSenderType];

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    NSMutableURLRequest *request = [WebService POST:stringValues initWithJsonString:newDatasetInfo];

    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
        if (data != nil && error == nil) {
            [self parseResponse:data];
        }
        if (data == nil || error != nil) {
            NSLog(@"error %@", error);
        }
    }];

    [dataTask resume];
}

-(void)parseResponse:(NSData *)data{
    NSMutableArray *extraData = [mPayResponseResult parseResponseForOperation:systemConstantsObj.systemConfiguration withData:data];
    if (extraData != nil && [extraData count] > 0)
        [systemConstantsObj saveSystemConfigurations:extraData];
    else
        systemConstantsObj.systemConfigsList = nil;
}

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
        if([challenge.protectionSpace.host isEqualToString:@"dinarakapp"]){
            NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
            completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
        }
    }
}

- (BOOL)connection:(NSURLSession *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return YES;
}

- (void)connection:(NSURLSession *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
        if([challenge.protectionSpace.host isEqualToString:@"dinarakapp"])
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

and the settings for Info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>https://80.90.171.183:8445/ps-mpay</key>
        <dict>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSAllowsArbitraryLoads</key>
            <true/>
        </dict>
    </dict>
</dict>
BK Elizabeth
  • 479
  • 5
  • 15
AShi
  • 33
  • 4

1 Answers1

0

Add below code in your Info.plist in both main project folder and project Tests folder. We had the same issue before. By adding the AppTransportSecurity in Main project Info.plist used to give us the same error. When we made changes in both Info.plist files it worked.

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
harsha yarabarla
  • 506
  • 4
  • 11
  • harsha yarabarla unfortunately it doesn't work either. – AShi Feb 17 '16 at 13:10
  • NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) – AShi Feb 17 '16 at 14:12
  • Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9802, NSErrorPeerCertificateChainKey={type = immutable, count = 1, values = ( 0 : – AShi Feb 17 '16 at 14:13
  • )}, NSUnderlyingError=0x15ed2ac40 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=1, kCFStreamPropertySSLPeerTrust=, _kCFNetworkCFStreamSSLErrorOriginalValue=-9802, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9802, kCFStreamPropertySSLPeerCertificates={type = immutable, count = 1, values = ( 0 : )}}}, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made: – AShi Feb 17 '16 at 14:14