0
votes

I have an app with data fed from an AFNetworking request with basic auth. I have a refresh button to perform the same function used to initially feed the data. This all works, but after a couple minutes it doesn't.

My code:

- (void) refreshData
{
    // Show loading
    NSLog(@"Refreshing...");
    [LoadingView initInView:self.view];

    // Get session data for auth key
    SessionHelper *session = [SessionHelper sharedInstance];

    // Setup request
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
    [manager.requestSerializer clearAuthorizationHeader];
    [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:session.authKey password:@""];
    NSString *url = @"https://myurl.com/getdata";
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.data = [[NSMutableArray alloc] init];
        [self.data addObjectsFromArray:responseObject];
        [self.tableView reloadData];
        [LoadingView remove];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", [error description]);
        [operation cancel];
    }];
}

Works repeatedly fine at first, but after a few minutes it hangs with the loading screen (from LoadingView) and the error:

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

I thought maybe I needed to do clearAuthorizationHeader or something, but no go. Any ideas? Thanks!

Update: Logging operation.response.statusCode in the error message gives me 0.

Update 2: Using AFNetworkActivityLogger gives me this after it starts failing:

2014-02-26 13:23:02.206 MyApp iPad[4087:60b] GET 'https://myurl.com/getdata' 2014-02-26 13:23:02.646 MyApp iPad[4087:60b] [Error] GET '(null)' (0) [0.4332 s]: Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x17d9a990 {NSErrorFailingURLKey=https://myurl.com/getdata, NSErrorFailingURLStringKey=https://myurl.com/getdata} 2014-02-26 13:23:02.648 MyApp iPad[4087:60b] Error: Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x17d9a990 {NSErrorFailingURLKey=https://myurl.com/getdata, NSErrorFailingURLStringKey=https://myurl.com/getdata}, Status Code: 0

Seems the URL is being sent as (null)...

1

1 Answers

3
votes

Turns out, even though the GoDaddy certificate I'm using looks to be valid and supported by iOS 7 according to http://support.apple.com/kb/HT5012, adding this bit solves the problem. Seems to kind of defeat the purpose, but it works. I won't accept this answer yet in case someone can give some insight / something more helpful.

AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
securityPolicy.allowInvalidCertificates = YES;
manager.securityPolicy = securityPolicy;