NetworkSimple 1.1.3

NetworkSimple 1.1.3

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最后发布2015年7月

Jamie Evans 维护。



 
依赖
NSJSONSerialization-NSNullRemoval~> 1.0
可达性~> 3.2
 

  • Jamie Evans

使用方法

要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install

安装

如何使用

要使用此库,首先导入客户端

#import <NetworkSimple/NSClient.h>

要使用客户端,您必须从实例开始

NSClient *client = [NSClient clientWithScheme:@"http" andHost:@"randomuser.me"];

要执行简化的 URL 请求,可以进行以下操作

[client sendRequestWithEndpoint:@""
                 httpMethodType:NSHTTPMethodTypeGet
                    requestType:NSRequestTypeURL
                     dataObject:nil
           requestMutationBlock:nil
                    andCallback:^(NSUInteger statusCode, id responseObject, NSError *error)
 {
     if(statusCode == 200)
     {
         // Received users!
         // responseObject should contain an NSArray of user objects
     }
     else if(error)
     {
         // Display alert with error message
     }
 }];

对于更复杂的请求,让我们发送带有 JSON 主体数据的 POST 请求。我们还将设置请求的 HTTP 头部 mutationBlock - 这总是会在请求生成中最后触发。

[client sendRequestWithEndpoint:@""
                 httpMethodType:NSHTTPMethodTypePost
                    requestType:NSRequestTypeJSON
                     dataObject:@{@"firstName"   : @"Jamie",
                                  @"lastName"    : @"Evans",
                                  @"phoneNumber" : @"555-555-5555"}
           requestMutationBlock:^(NSMutableURLRequest *request)
 {
     [request setValue:@"cbeiqu829fPamfr12adkjln" forHTTPHeaderField:@"client_token"];
     [request setTimeoutInterval:10.0f];
 }
                    andCallback:^(NSUInteger statusCode, id responseObject, NSError *error)
 {
     if(statusCode == 200)
     {
         // Created a user successfully!
     }
     else if(error)
     {
         // Failed to create a user :(
     }
}];

我们还可以更新请求的默认超时间隔

[client setRequestTimeout:10.0f];

或设置请求的缓存策略

[client setCachingPolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];

如果您使用多部分请求,则需要设置边界字符串

[client setBoundaryString:@"myBoundaryStringForThisApplication"];

所有这些属性都可以在 mutationBlock 中进行覆盖。

作者

Jamie Evans, [email protected]

许可

NetworkSimple 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。