PXLNetworking 1.0.3

PXLNetworking 1.0.3

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布日期上次发布2014年12月

Jason Silberman 维护。



  • 作者:
  • Jason Silberman

一种简单的方法,在 iOS 上进行网络连接,无需繁琐的操作。

PXLNetworking 是一个超级简单的类。如果您需要更复杂的功能,我强烈建议您使用 AFNetworking

文档

您可以继续阅读以获取 PXLNetworking 的简要概述,或者您可以阅读文档

从 v0.1.0 迁移到 v1.0.0

如果您仅使用 PXLHTTPClient,则没有差异。所有更改均针对响应对象的序列化方式进行了调整。

添加到您的项目

开始使用 PXLNetworking 非常简单!您可以使用 Cocoapods,或者您可以手动操作。

手动

要手动添加到您的项目中

  1. PXLNetworking/ 目录中的文件添加到您的项目中。

PXLNetworking 需要ARC。

使用方法

使用 PXLNetworking 非常简单。您只需将 PXLNetworking.h 包含在您希望使用它的文件中即可。

PXLHTTPClient

PXLNetworking 有一个主要的类,即 PXLHTTPClient

PXLHTTPClient 具有以下方法

+ (instancetype)sharedClient; // Use this to get the global PXLHTTPClient instance

- (void)POST:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(PXLHTTPOperation *response, id responseObject))success failure:(void (^)(PXLHTTPOperation *response, NSError *error))failure;
- (void)GET:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(PXLHTTPOperation *response, id responseObject))success failure:(void (^)(PXLHTTPOperation *response, NSError *error))failure;
- (void)PUT:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(PXLHTTPOperation *response, id responseObject))success failure:(void (^)(PXLHTTPOperation *response, NSError *error))failure;
- (void)PATCH:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(PXLHTTPOperation *response, id responseObject))success failure:(void (^)(PXLHTTPOperation *response, NSError *error))failure;
- (void)DELETE:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(PXLHTTPOperation *response, id responseObject))success failure:(void (^)(PXLHTTPOperation *response, NSError *error))failure;
- (void)ANY:(NSString *)URLString parameters:(NSDictionary *)parameters requestType:(PXLHTTPRequestType)requestType headers:(NSDictionary *)headers success:(void (^)(PXLHTTPOperation *response, id responseObject))success failure:(void (^)(PXLHTTPOperation *response, NSError *error))failure;

- (void)sendHTTPRequest:(NSURLRequest *)request success:(void (^)(PXLHTTPOperation *response, id responseObject))success failure:(void (^)(PXLHTTPOperation *response, NSError *error))failure;

使用 PXLHTTPClient 非常简单,尽管某些方法可能看起来复杂。

要获取全局 PXLHTTPClient 实例,请调用 +sharedClient 方法。

GET 请求

要发送简单的 GET HTTP 请求,请执行以下操作

PXLHTTPClient *client = [PXLHTTPClient sharedClient];
[client GET:@"http://example.com/things.json" parameters:nil success:^(PXLHTTPOperation *response, id responseObject) {
    NSLog(@"[Success] %@ %@", response.operationDescription, responseObject);
} failure:^(PXLHTTPOperation *response, NSError *error) {
    NSLog(@"[Error] %@", error);
}];

POST JSON 编码请求

要发送带有参数的 POST HTTP 请求,请执行以下操作

PXLHTTPClient *client = [PXLHTTPClient sharedClient];
NSDictionary *params = @{@"bar": @"foo"};
[client POST:@"http://example.com/things.json" parameters:params success:^(PXLHTTPOperation *response, id responseObject) {
    NSLog(@"[Success] %@ %@", response.operationDescription, responseObject);
} failure:^(PXLHTTPOperation *response, NSError *error) {
    NSLog(@"[Error] %@", error);
}];

带有头和/或参数的 POST 请求

要发送带有头和/或参数的 POST HTTP 请求,请执行以下操作

PXLHTTPClient *client = [PXLHTTPClient sharedClient];
NSDictionary *headers = @{@"Content-Type" : @"application/json"};
[client ANY:@"http://example.com/things.json" parameters:nil requestType:PXLHTTPRequestTypePOST headers:headers success:^(PXLHTTPOperation *response, id responseObject) {
    NSLog(@"[Success] %@ %@", response.operationDescription, responseObject);
} failure:^(PXLHTTPOperation *response, NSError *error) {
    NSLog(@"[Error] %@", error);
}];

《requestType》可以是以下任何一种:PXLHTTPRequestTypePOST 用于发送 POST 请求,PXLHTTPRequestTypeGET 用于发送 GET 请求,PXLHTTPRequestTypePUT 用于发送 PUT 请求,PXLHTTPRequestTypePATCH 用于发送 PATCH 请求,或者 PXLHTTPRequestTypeDELETE 用于发送 DELETE 请求。

响应

在所有上述方法中,无论是成功块中的 PXLHTTPOperation 及其 .serializedResponseObject 属性,还是错误块中的 PXLHTTPOperation 及其 .error 属性。

PXLHTTPOperation

在所有回调块中,都会返回一个 PXLHTTPOperation

PXLHTTPOperation 使用的以下属性

(id) serializedResponseObject
// The serialized response object.

(id) rawResponseData
// The raw response data returned by the NSURLSessionDataTask.

(NSDictionary) responseHeaders
// The headers returned by the NSURLSessionDataTask.

(NSError) error
// The error returned by the NSURLSessionDataTask, nil if successful.

(NSURL) URL
// The URL used for making the request.

(NSString) operationDescription;
// The operation description. Example: GET http://example.com

PXLActivityIndicator

PXLNetworking 还提供了一个网络活动指示器。您应该通过在 AppDelegate application:didFinishLaunchingWithOptions 中启用 PXLActivityIndicator 的共享实例来实现此操作

[PXLActivityIndicator sharedManager].enabled = YES;

致谢

PXLNetworking 部分基于 AFNetworking,由 Mattt ThompsonScott Raymond 开发。

许可

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