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); }];

How to get download progress in AFNetworking 2.0?

You should observe the fractionCompleted property of your NSProgress object using KVO: NSURL *url = [NSURL URLWithString:@”http://www.hfrmovies.com/TheHobbitDesolationOfSmaug48fps.mp4″]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFHTTPSessionManager *session = [AFHTTPSessionManager manager]; NSProgress *progress; NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { // … } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { [progress removeObserver:self forKeyPath:@”fractionCompleted” context:NULL]; … Read more

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

Uploading image with AFNetworking 2.0

I’m not sure which part (I think that some details were missing) was responsible, but I did it finally 🙂 here you go: -(void)uploadPhoto{ AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@”http://server.url”]]; NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5); NSDictionary *parameters = @{@”username”: self.username, @”password” : self.password}; AFHTTPRequestOperation *op = [manager POST:@”rest.of.url” parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { //do not … Read more

‘Project Name’ was compiled with optimization – stepping may behave oddly; variables may not be available

If your project is using Swift, there are two separate “Optimization Level” settings in the project/target configuration. Make sure you set them both correctly: Select your project in the Project Navigator pane Select your project’s settings under the “PROJECT” tree Click “Build Settings” tab Search for “Optimization Level” and you’ll see two settings, one for … 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

Request failed: unacceptable content-type: text/html using AFNetworking 2.0

This means that your server is sending “text/html” instead of the already supported types. My solution was to add “text/html” to acceptableContentTypes set in AFURLResponseSerialization class. Just search for “acceptableContentTypes” and add @”text/html” to the set manually. Of course, the ideal solution is to change the type sent from the server, but for that you … Read more