Posting JSON data using AFNetworking 2.0

after searching docs and trying out some codes I got following as an example AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; NSDictionary *params = @ {@”user” :txtUserName, @”pwd” :txtPwd }; [manager POST:URL_SIGNIN parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@”JSON: %@”, responseObject); } failure: ^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@”Error: %@”, error); }]; Also … Read more

AFNetworking 2.0 and HTTP Basic Authentication

AFNetworking 2.0 new architecture use serializers for creating requests and parsing responses. In order to set the authorization header, you should first initialize a request operation manager that replaces the AFHTTPClient, create a serializer and then call the dedicated method to set the header. For example you code would become: AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] … Read more

AFNetworking 2.0 AFHTTPSessionManager: how to get status code and response JSON in failure block?

You can access the “data” object directly from AFNetworking by using the “AFNetworkingOperationFailingURLResponseDataErrorKey” key so there is no need for subclassing the AFJSONResponseSerializer. You can the serialize the data into a readable dictionary. Here is some sample code to get JSON Data : NSData *errorData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey]; NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData: errorData options:kNilOptions error:nil]; … Read more

How to download image with AFNetworking 2.0?

SO you want something like this for 2.0. AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest]; requestOperation.responseSerializer = [AFImageResponseSerializer serializer]; [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@”Response: %@”, responseObject); _imageView.image = responseObject; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@”Image error: %@”, error); }]; [requestOperation start]; As mentioned by Adam you can also do something like the below … 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

How can I log each request/response using Alamofire?

There’s a sweet little pod for this: https://github.com/konkab/AlamofireNetworkActivityLogger Add this to your podfile: pod ‘AlamofireNetworkActivityLogger’, ‘~> 2.0′ In your AppDelegate: import AlamofireNetworkActivityLogger Then in your didFinishLaunchingWithOptions, add this: NetworkActivityLogger.shared.level = .debug NetworkActivityLogger.shared.startLogging() EDIT: I’ve actually encountered crashes with this in production. To be on the safe side, use “build flags” to only use this in … Read more

tech