1

This happened only in iOS 9.3.5 (not sure about other 9 or below), not occur in iOS 10.x.

When I use AFNetworking to connect to an API URL, it just run into failure callback with error like below:

LOG: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={NSErrorFailingURLStringKey=APIURL, _kCFStreamErrorCodeKey=-2200, NSErrorFailingURLKey=APIURL, NSLocalizedDescription=Could not connect to the server., _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x16146990 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorDomainKey=4, NSErrorPeerAddressKey={length = 16, capacity = 16, bytes = 0x100201bb253ce7160000000000000000}, _kCFStreamErrorCodeKey=-2200}}}

When I paste the API to safari browser (same device), it ask me to trust a certificate or something like that, so I'm afraid there's something wrong in the configuration in the app.

My call for URL:

self.sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseUrl sessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
self.sessionManager.securityPolicy.allowInvalidCertificates = YES;
self.sessionManager.securityPolicy.validatesDomainName = NO;
self.sessionManager.requestSerializer.timeoutInterval = timeout;
self.sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];

// Call API:
[self.sessionManager POST:path parameters:dictParams progress:nil success:^(NSURLSessionTask *task, id responseObject) { 
    // success block
} failure:^(NSURLSessionTask *task, NSError *error) {
    // failure block
}

I've even set NSAllowsArbitraryLoads=true in App Transport Security Settings inside info.plist.

How to fix this? I found this question have the similar error, but I'm not so sure. I don't have access to the API host and in ios 10.x it works just fine. When using postman it works too.

Community
  • 1
  • 1
Eddie
  • 1,903
  • 2
  • 21
  • 46

0 Answers0