AFNetworking AFHTTPClient Class

坚强是说给别人听的谎言 提交于 2019-12-05 11:32:45

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

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

Bruno Tereso

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);
        }];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!