用AFNetworking(2.0)进行网络请求教程

2017-02-08 16:27
248

主bundle栏

用AFNetworking(2.0)进行网络请求教程-JEESNS
Paste_Image.png
ViewController.m文件
#import "ViewController.h"
#import "MD_Networking.h"

@interface ViewController () 

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [MD_Networking shareNetworking].HTTPHeaderDictionary = @{@"填入后台的 apiKey 值" : @"填入后台的 apiValue 值"} ;
    [MD_Networking getRequestWithURL:@"填入URL地址字段" finished:^(BOOL isSuccess, NSData *data) {
        if (isSuccess) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil] ;
            NSLog(@"%@" , dict) ;
        }
    }] ;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

@end
MD_Networking.h文件
#import <Foundation/Foundation.h>

@interface MD_Networking : NSObject <NSCopying>

//请求头参数:
@property (nonatomic, strong) NSDictionary *HTTPHeaderDictionary ;
//创建单例类用于获取单例对象:
+ (MD_Networking *)shareNetworking ;
//GET方式请求:
+ (void)getRequestWithURL:(NSString *)urlStr finished:(void (^) (BOOL isSuccess , NSData *data))finish ;
//POST方式请求:
+ (void)postRequestWithURL:(NSString *)urlStr parameterDict:(NSDictionary *)parameterDict finished:(void (^) (BOOL isSuccess , NSData *data))finish ;

@end
MD_Networking.m文件:
#import "MD_Networking.h"
#import "AFNetworking.h"

static MD_Networking *networking = nil ;

@implementation MD_Networking 

+ (MD_Networking *)shareNetworking {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        networking = [[MD_Networking alloc] init] ;
    });
    return networking ;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        networking = [super allocWithZone:zone] ;
    });
    return networking ;
}

- (id)copyWithZone:(NSZone *)zone {
    return networking ;
}

+ (void)getRequestWithURL:(NSString *)urlStr finished:(void (^)(BOOL, NSData *))finish {

    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager] ;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer] ;
    if ([MD_Networking shareNetworking].HTTPHeaderDictionary) {
        for (NSString *key in [MD_Networking shareNetworking].HTTPHeaderDictionary) {
            [manager.requestSerializer setValue:[MD_Networking shareNetworking].HTTPHeaderDictionary[key] forHTTPHeaderField:key] ;
        }
    }

    [manager GET:urlStr parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        finish(YES , responseObject) ;

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        finish(NO , nil) ;

    }] ;
}

+ (void)postRequestWithURL:(NSString *)urlStr parameterDict:(NSDictionary *)parameterDict finished:(void (^)(BOOL, NSData *))finish {

    //    NSString *characterToEscape = @"?!@#$%^&*()" ;
    //    NSCharacterSet *allowedCharacter = [[NSCharacterSet characterSetWithCharactersInString:characterToEscape] invertedSet] ;
    //    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacter] ;
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager] ;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer] ;
    if ([MD_Networking shareNetworking].HTTPHeaderDictionary) {
        for (NSString *key in [MD_Networking shareNetworking].HTTPHeaderDictionary) {
            [manager.requestSerializer setValue:[MD_Networking shareNetworking].HTTPHeaderDictionary[key] forHTTPHeaderField:key] ;
        }
    }

    [manager POST:urlStr parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        finish(YES , responseObject) ;

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        finish(NO , nil) ;

    }] ;
}

@end

愿编程让这个世界更美好

帖子评论

关注 / 粉丝

我无话可说