0
votes

I am using NSURLSession in my application to send the data to server. Server URL may be arbitary , It may change dynamically

So I have added following in my plist file

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

because user can change the server URL manually from settings page inside my application. So when I try to send data , NSURLRequest will be configured with latest server URL.

My code is

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

//some of my codes

 }] resume];

I will create NSMutableURLRequest from dynamic URL which is entered by user in settings page(eg. https://100.100.25.25/sample).

NSMutableURLRequest *dataRequest = [NSMutableURLRequest requestWithURL:serverAddress 
                                                               cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                           timeoutInterval:60.0];   

when I try to access the server using NSURLSession with latest server URL then Its always return error "NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843)".

Note : If my URL only contain characters(https://sample.com/sample) then its working fine. If it contains some number(https://100.100.25.25/sample) then its not working. Given URL is not live. Its just to say Please help me to find the issue . Thanks in advance

1

1 Answers

1
votes

This works for me. Source : How do I accept a self-signed SSL certificate using iOS 7's NSURLSession and its family of delegate methods for development purposes?

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:Nil];
...
...
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{
  if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
    if([challenge.protectionSpace.host isEqualToString:@"mydomain.com"]){
      NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
      completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
    }
  }
}