NetworkBuilder是iOS URL加载框架的轻量级包装器。
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