iosobjective-chttpsafnetworking-2

AFNetworking + HTTPS (Identity not verified)


I need to fetch the records from my Web-Server through https protcol. My API calls has Basic Authentication. Now, am trying this with AFNetworking version 2.0x

After work around and find out simplest way to add Basic authentication here Now, i am getting problem with https call. When i execute my code,

NSDictionary *params = ....;

AFHTTPRequestOperationManager *manager = [SBAPIManager sharedManager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

@try {
    [manager POST:@"Create" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success - %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
} @catch (NSException *exception) {
    NSLog(@"Exception - %@", exception.description);
} 

Am getting below response only,

Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x7fd268560900 {NSErrorFailingURLKey=https://myapi.com/Create, NSErrorFailingURLStringKey=https://myapi.com/Create}

I found the github fixes issue But, AFNetworking 2.x has (instancetype)policyWithPinningMode:(AFSSLPinningMode)pinningMode; by default. How could i integrate this into my constraint.

Anyone has idea on this?


Solution

  • In the end, I found the answer by myself. I have fixed my problem with the code below,

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:API_URL]];
    manager.securityPolicy.allowInvalidCertificates = YES; // not recommended for production
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    

    If we want to pin AFNetworking to AFHTTPRequestOperationManager, we must add the following one line, which will provide the result.

    manager.securityPolicy.allowInvalidCertificates = YES; // not recommended for production

    Reference: AFNetworking