Category Archives: afnetworking

AFHTTPSessionManager UserNameandPassword

I'm having trouble adding login and password to my server and still have AFHTTPSessionManager download audio files.

Currently, I have a .htaccess and .htpasswd file in a folder with the login and password details and this is working fine when I'm using AFHTTPRequestOperationManager to download a JSON file using this:

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];    
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:kJSONLogin password:kJSONpw];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

But when I try to use AFHTTPSessionManager to download audio files using the same credentials (files are in the same folder). I get a (401) unauthorized access error.

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:kJSONLogin password:kJSONpw];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

What am I doing wrong here?

iOS: HTTPS communication with Apache Server with SSLVerifyClient require directive

In my iOS app I have to communicate with a Apache Server with SSLVerifyClient require directive so, as far as I know, the client must to present a valid certificate to the server. I have the .cer certificate loaded in my app's bundle, I'm using the AFNetworking library and I would like to know how the steps to take in order to communicate with the server successfully. Thanks in advance.

use AFNetworking upload file to my apache server

I use afnetworking upload my file to apache . before that I config the apache server like this and my upload code is:

NSString *remoteUrl = @"http://192.168.8.101/example";
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"uploadPic" ofType:@"JPG"];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:remoteUrl parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileURL:[NSURL fileURLWithPath:filePath] name:@"file" fileName:filePath.lastPathComponent mimeType:@"image/jpeg" error:nil];
} error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSProgress *progress = nil;

NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"succ:%@ %@", response, responseObject);
    }
}];
[uploadTask resume];

and I received the status code 200

2014-06-08 16:13:18.760 ReasearchDemo[10374:60b] succ:<NSHTTPURLResponse: 0x17803b240> { URL: http://192.168.8.101/example/ } { status code: 200, headers {
Connection = "Keep-Alive";
"Content-Length" = 1322;
"Content-Type" = "text/html;charset=ISO-8859-1";
Date = "Sun, 08 Jun 2014 08:13:17 GMT";
"Keep-Alive" = "timeout=5, max=100";
Server = "Apache/2.4.7 (Unix) PHP/5.5.6 OpenSSL/1.0.1e mod_perl/2.0.8-dev Perl/v5.16.3";

the problem is I can not find the file I upload this means I upload fail but I don't know the problem
anyone can help thanks a lot