5

I’m fairly new to iOS programming, especially when it comes to webservices. I’m developing a App for academic purposes, and I need to communicate with my server, currently using AFNetworking2 and Restler/php, everything work when it comes to GET methods. But I can’t upload anything.

Been reading for hours, in github support site, stackoverflow, pretty much all examples/questions to upload images (and there are a LOT) use this line:

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://server"]]; 

I do have a Client class, subclass of AFHTTPSessionManager, with my sharedClient. But all examples use this AFHTTPClient with initWithURL and other classes like AFJSONRequestOperation that I can’t no longer find.

Mostly it says I should create a singleton, subclass of AFHTTPClient, but I can´t find it anywhere. Some links even send me to official github repository but it’s not available anymore. So my question is, where can I get more info about AFHTTPClient, should I use it, can anyone point me a tutorial on how to create one or at least understand its functionality.

Cheers

Bruno Tereso
  • 227
  • 1
  • 5
  • 15

3 Answers3

23

In AFNetworking 2.0 the AFHTTPClient has been replaced by AFHTTPRequestOperationManager / AFHTTPSessionManager. I would suggest you to refer to the example in git by them. Git clone and open in XCode. It should help you. That has the most updated example.

If you want to use AFHTTPClient i.e 1.x code. Here is the git link to the branch. The pod spec to that would be

pod 'AFNetworking', '~> 1.3.3'

In 2.0 AFNetworking, you can create a singleton client like this.

interface

@interface AFAppDotNetAPIClient : AFHTTPSessionManager

+ (instancetype)sharedClient;

@end

Implementation

#import "AFAppDotNetAPIClient.h"

static NSString * const AFAppDotNetAPIBaseURLString = @"https://alpha-api.app.net/";

@implementation AFAppDotNetAPIClient

+ (instancetype)sharedClient {
    static AFAppDotNetAPIClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[AFAppDotNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:AFAppDotNetAPIBaseURLString]];
        [_sharedClient setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey]];
    });

    return _sharedClient;
}

@end
Pradeep Mahdevu
  • 7,613
  • 2
  • 31
  • 29
  • I did check their example, it's very helpful, but it doesn't have any upload methods, only GET. Which is what you can access using your example, that part I managed to do and it's working perfectly... I'm starting to think that downgrading is the only option. – Bruno Tereso Oct 21 '13 at 21:00
  • @Bruno Check this [example](http://stackoverflow.com/questions/19261481/ios-image-upload-via-afnetworking-2-0?answertab=oldest#tab-top) – Pradeep Mahdevu Oct 21 '13 at 21:05
  • Still having some problems grabbing the picture on server side, but at least I managed to upload something, with a modified version of that code, thanks! – Bruno Tereso Oct 21 '13 at 22:15
  • It's not working for image files, it always gets an error saying that the entity is too large. I'm trying to find a workaround, I'll mark as answered if there's one using that code. – Bruno Tereso Oct 21 '13 at 22:36
  • So you are getting a HTTP 413. I guess the request was larger than the server can handle. See if you can upload a very small image. If you can, then you need to change server code. – Pradeep Mahdevu Oct 21 '13 at 22:43
2

AFHTTPClient is class from AFNetworking 1.x -- https://github.com/AFNetworking/AFNetworking/tree/1.x

AFNetworking 2.0 is pretty new library, so there's not too much tutorials about it, for now you can still you first version till you will feel that there's time to learn 2.x))

Hope helps

in.disee
  • 1,102
  • 1
  • 7
  • 15
2

Here's the solution, modified for latest version of AFNetworking.

//sample PNG
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"700k_image.png"]);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:WEBSERVICE_IMAGEM_UPLOAD parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            [formData appendPartWithFileData:imageData name:@"image" fileName:@"image_name" mimeType:@"image/png"];
             } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"Success: %@", responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Bruno Tereso
  • 227
  • 1
  • 5
  • 15