How to download a file and save it to the documents directory with AFNetworking?

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@”…”]]; AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@”filename”]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@”Successfully downloaded file to %@”, path); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@”Error: %@”, error); }]; [operation start];

iOS Image upload via AFNetworking 2.0

I ended up using the multi-part request UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; NSData *imageData = UIImageJPEGRepresentation(image, 0.5); AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *parameters = @{@”foo”: @”bar”}; [manager POST:@”http://example.com/resources.json” parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFormData:imageData name:@”image”]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@”Success: %@”, responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@”Error: %@”, error); }];

Does AFNetworking have backgrounding support?

EDIT: As of AFNetworking 1.0RC1, this is an explicit feature. AFURLConnectionOperation now has the method setShouldExecuteAsBackgroundTaskWithExpirationHandler:, which transparently manages all of this for you. It’s an implicit feature, so I didn’t really think about advertising it. All you’d need to do is: – (void)applicationWillResignActive:(UIApplication *)application { __block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) { [application endBackgroundTask:backgroundTaskIdentifier]; … Read more

kCFStreamErrorDomainSSL, -9802 when connecting to a server by IP address through HTTPS in iOS 9

iOS9 requires the server to only support TLSv1.2 and support perfect forward security. Also required is for the app to support IPV6 including not using hard-coded IP addresses. Suggested is to use NSURLSession. Otherwise exception additions must be made in the app plist. See the WWDC-15 session “Security and your Apps”. Also see Steven Peterson’s … Read more

afnetworking 3.0 Migration: how to POST with headers and HTTP Body

Another way to call a POST method with AFNetworking 3.0 is: AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; [manager.requestSerializer setValue:@”application/json” forHTTPHeaderField:@”Content-Type”]; [manager POST:url parameters:parametersDictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@”success!”); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@”error: %@”, error); }]; Hope it helps!

Cannot install Alamofire in new Xcode Project. “No Such module Alamofire”

Make sure you haven’t added any files from Alamofire to your project except for the Alamofire.xcodeproj Here is step by step instruction: Download and unarchive Alamofire Copy the root folder of Alamofire to any subfolder of your project. Libs, for example. Drag and drop Alamofire.xcodeproj to your Xcode project Open project settings of your project, … Read more

AFNetworking and background transfers

A couple of thoughts: You have to make sure you do the necessary coding outlined in the Handling iOS Background Activity section of the URL Loading System Programming Guide says: If you are using NSURLSession in iOS, your app is automatically relaunched when a download completes. Your app’s application:handleEventsForBackgroundURLSession:completionHandler: app delegate method is responsible for … Read more

Can AFNetworking return data synchronously (inside a block)?

To block the execution of the main thread until the operation completes, you could do [operation waitUntilFinished] after it’s added to the operation queue. In this case, you wouldn’t need the return in the block; setting the __block variable would be enough. That said, I’d strongly discourage forcing asynchronous operations to synchronous methods. It’s tricky … Read more