NetworkBuilder 2.0.1

NetworkBuilder 2.0.1

测试已测试
Lang语言 Obj-CObjective C
许可证 Apache 2
发布最后发布2015年11月

Patrick Hogan维护。



  • Patrick Hogan

什么是NetworkBuilder?

NetworkBuilder是iOS URL加载框架的轻量级包装器。

要求

  • iOS 9.0+ / Mac OS X 10.10+
  • Xcode 7.0
  • Cocoapods 0.37.1

用例

NBHttpSession作为简单的包装器来容纳NSURLSession及其属性。会话对象调解了一个http请求。以下是一个简单的用例。

#import "HttpSessionManager.h"

#import <NetworkBuilder/NetworkBuilder.h>

@import AFNetworking;

@interface HttpSessionManager ()

@property (nonatomic) NBNetworkHost *networkHost;
@property (nonatomic) AFJSONRequestSerializer *requestSerializer;
@property (nonatomic) NBHttpSession *session;

@end

@implementation HttpSessionManager

- (instancetype)init {

    self = [super init];

    if (self) {
        self.networkHost = ({
            NSURL *baseUrl = [NSURL URLWithString:@"https://www.google.com"];
            NBNetworkHost *networkHost = [[NBNetworkHost alloc] initWithBaseUrl:baseUrl
                                                        redirectBaseUrlProvider:nil];
            networkHost;
        });

        self.requestSerializer = [AFJSONRequestSerializer new];
        self.session = [[NBHttpSession alloc] initWithBuilder:^(id <NBHttpSessionBuilder> builder) {
            builder.delegate = self;
        }];

        [self loadHttpTask];
    }

    return self;

}

- (void)loadHttpTask {

    NBHttpTask *task =
    [self.session createTask:self.networkHost
                buildRequest:^ (id <NBHttpRequestBuilder> builder) {
                    builder.requestSerializer = ^(NBHttpMethod method, NSString *urlString, NSDictionary<NSString *, id> *parameters) {
                        return [self.requestSerializer requestWithMethod:method
                                                               URLString:urlString
                                                              parameters:parameters
                                                                   error:nil];
                    };
                }
                   buildTask:^(NBHttpRequest *request, id <NBHttpTaskBuilder> builder) {
                       builder.onDidComplete = ^(NBHttpTask *task, NSError *error) {
                           // Do something with the result
                       };
                   }];

    [self.session addTask:task];

}

@end