SWNetworking
SkyWite 是一个开源的多用途框架,功能强大且灵活。干净的代码和精巧的特性使 SkyWite 成为理想之选。内置 Cocoa 的高层网络抽象,具有设计合理的、特性丰富的 API,使用起来非常愉快。
使用 SkyWite 赢得时间,可以节省数百小时。
开始使用 Skywite 进行开发。您一定会感到高兴...是的..
在实现之前,您需要将 "SystemConfiguration" 框架添加到您的项目中。
如何应用至 Xcode 项目
#架构
SWRequest
SWRequest
SWGETRequest
SWPOSTRequest
SWMultiPartRequest
SWPUTRequest
SWPATCHRequest
SWDELETERequest
SWHEADRequest
SWOfflineRequestManger
ResponseType
SWResponseDataType
SWResponseJSONDataType
SWResponseXMLDataType
SWResponseStringDataType
SWResponseUIImageType
SWRequestDataType
SWRequestFormData
SWRequestMulitFormData
SWRequestJSONData
可到达性
SWReachability
文件
SWMedia
UIKit+SWNetworking
UIImageView+SWNetworking
UIProgressView+SWNetworking
NSURLSession
所有请求都将使用 NSURLSession。会话将添加到 NSOperationQueue
中。请求创建、响应序列化、网络可达性处理以及离线请求也将进行。
GET
請求 SWGETRequest *getRequest = [[SWGETRequest alloc]init];
[getRequest startDataTaskWithURL:@"http://127.0.0.1:3000" parameters:nil success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
}];
如果您想發送參數,您有兩種選擇
SWGETRequest *getRequest = [[SWGETRequest alloc]init];
getRequest.responseDataType = [SWResponseJSONDataType type];
[getRequest startDataTaskWithURL:@"http://127.0.0.1:3000" parameters:@"name=this is name&address=your address" parentView:nil success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
}];
如果您想鎖定參數和值,您需要傳遞帶有鍵/值的NSDictionary
對象。
SWGETRequest *getRequest = [[SWGETRequest alloc]init];
getRequest.responseDataType = [SWResponseJSONDataType type];
[getRequest startDataTaskWithURL:@"http://127.0.0.1:3000" parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:nil success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
}];
我們推薦使用第二種選擇,因為如果參數或值中帶有&
符號,則會破壞值的發送。
GET
帶有响应类型可用作响应类型的:SWResponseJSONDataType
,SWResponseJSONDataType
,SWResponseXMLDataType
,SWResponseStringDataType
,SWResponseUIImageType
您需要設置responseDataType
。
// this response will be JSON
SWGETRequest *getRequest = [[SWGETRequest alloc]init];
getRequest.responseDataType = [SWResponseJSONDataType type];
[getRequest startDataTaskWithURL:@"http://127.0.0.1:3000" parameters:nil parentView:nil success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
}];
GET
帶有加载指示器如果您設置了父視圖方法,則將顯示加载指示器。
SWGETRequest *getRequest = [[SWGETRequest alloc]init];
getRequest.responseDataType = [SWResponseJSONDataType type];
[getRequest startDataTaskWithURL:@"http://127.0.0.1:3000" parameters:nil parentView:self.view success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
}];
如果您想自定義加载視圖,您需要將新的nib
文件添加到您的項目中,並將其命名為'sw_loadingView'。它將在屏幕上顯示。
如果您想要訪問在响应中的緩存數據,您需要使用包括緩存塊的相關方法。
SWGETRequest *getRequest = [[SWGETRequest alloc]init];
getRequest.responseDataType = [SWResponseJSONDataType type];
[getRequest startDataTaskWithURL:@"http://127.0.0.1:3000" parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:nil success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
}];
POST
請求(簡單)緩存和加载視圖可用於相關方法。請檢查可用方法
SWPOSTRequest *postRequest = [[SWPOSTRequest alloc]init];
postRequest.responseDataType = [SWResponseJSONDataType type];
[postRequest startDataTaskWithURL:@"http://127.0.0.1:3000/drivers" parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:nil cachedData:^(NSCachedURLResponse *response, id responseObject) {
NSLog(@"%@", responseObject);
} success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
NSLog(@"%@", error);
}];
POST
多部分請求非常簡單。
SWPOSTRequest *postRequest = [[SWPOSTRequest alloc]init];
postRequest.responseDataType = [SWResponseJSONDataType type];
//need to crate files array to upload
UIImage *image = [UIImage imageNamed:@"skywite"];
NSData *imageData = UIImagePNGRepresentation(image);
SWMedia *file1 = [[SWMedia alloc]initWithFileName:@"imagefile.png" key:@"image" data:imageData];
//create with custom mine type one
SWMedia *file2 = [[SWMedia alloc]initWithFileName:@"image.jpg" key:@"image2" mineType:@"image/jpeg" data:imageData];
//create an array with files
NSArray *fileArray = @[file1, file2];
[postRequest startUploadTaskWithURL:@"http://127.0.0.1:3000/drivers" files:fileArray parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:nil
cachedData:^(NSCachedURLResponse *response, id responseObject) {
NSLog(@"%@", responseObject);
} success:^(NSURLSessionUploadTask *uploadTask, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
NSLog(@"%@", error);
}];
PUT
簡單請求 SWPUTRequest *putRequest = [[SWPUTRequest alloc]init];
putRequest.responseDataType = [SWResponseXMLDataType type];
[putRequest startDataTaskWithURL:@"http://127.0.0.1:3000/drivers" parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:nil success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
NSLog(@"%@", error);
}];}
PATCH
簡單請求 SWPATCHRequest *patchRequest = [[SWPATCHRequest alloc]init];
patchRequest.responseDataType = [SWResponseXMLDataType type];
[patchRequest startDataTaskWithURL:@"http://127.0.0.1:3000/drivers" parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:nil success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
NSLog(@"%@", error);
}];
DELETE
簡單請求 SWDELETERequest *deleteRequest = [[SWDELETERequest alloc]init];
deleteRequest.responseDataType = [SWResponseXMLDataType type];
[deleteRequest startDataTaskWithURL:@"http://127.0.0.1:3000/drivers" parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:nil cachedData:^(NSCachedURLResponse *response, id responseObject) {
NSLog(@"%@", responseObject);
} success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
NSLog(@"%@", error);
}];
HEAD
簡單請求 SWHEADRequest *headRequest = [[SWHEADRequest alloc]init];
headRequest.responseDataType = [SWResponseXMLDataType type];
[headRequest startDataTaskWithURL:@"http://127.0.0.1:3000/drivers" parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:nil cachedData:^(NSCachedURLResponse *response, id responseObject) {
NSLog(@"%@", responseObject);
} success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
NSLog(@"%@", error);
}];}
以下所有功能都適用於所有請求類型。例如:如果您想訪問,需要調用包括緩存塊的相關方法。
如果您想添加自定義header,您可以設置訪問請求對象。
SWPOSTRequest *postRequest = [[SWPOSTRequest alloc]init];
[postRequest.request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
postRequest.responseDataType = [SWResponseJSONDataType type];
[postRequest startDataTaskWithURL:@"your URL String" parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:self.view cachedData:^(NSCachedURLResponse *response, id responseObject) {
NSLog(@"%@", responseObject);
} success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
}];
如果您想更改請求超時時間,您必須更改叫做timeOut
的屬性。
SWPOSTRequest *postRequest = [[SWPOSTRequest alloc]init];
[postRequest setTimeOut:120];
postRequest.responseDataType = [SWResponseJSONDataType type];
[postRequest startDataTaskWithURL:@"your URL String" parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:self.view cachedData:^(NSCachedURLResponse *response, id responseObject) {
NSLog(@"%@", responseObject);
} success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
}];
您需要在所有請求中為responseDataType
發送對象類型。
//JSON
SWPOSTRequest *postRequest = [[SWPOSTRequest alloc]init];
postRequest.responseDataType = [SWResponseJSONDataType type];
[postRequest startDataTaskWithURL:@"your URL String" parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:self.view cachedData:^(NSCachedURLResponse *response, id responseObject) {
NSLog(@"%@", responseObject);
} success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
}];
//XML
SWPOSTRequest *postRequestXML = [[SWPOSTRequest alloc]init];
postRequestXML.responseDataType = [SWResponseJSONDataType type];
[postRequestXML startDataTaskWithURL:@"your URL String" parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:self.view cachedData:^(NSCachedURLResponse *response, id responseObject) {
NSLog(@"%@", responseObject);
} success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
}];
//String
SWPOSTRequest *postRequestString = [[SWPOSTRequest alloc]init];
postRequestString.responseDataType = [SWResponseStringDataType type];
[postRequestString startDataTaskWithURL:@"your URL String" parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:self.view cachedData:^(NSCachedURLResponse *response, id responseObject) {
NSLog(@"%@", responseObject);
} success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
}];
//String
SWGETRequest *getRequestImage = [[SWGETRequest alloc]init];
getRequestImage.responseDataType = [SWResponseUIImageType type];
[getRequestImage startDataTaskWithURL:@"your URL String" parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:self.view cachedData:^(NSCachedURLResponse *response, id responseObject) {
NSLog(@"%@", responseObject);
} success:^(NSURLSessionDataTask *uploadTask, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
}];
UIImageView
與SWNetworking
不再需要下載圖像並將其設置為UIImageView
。您可以將URL設置為UIImageView
。
// Please use only one method . you can see 4 methods :)
// from url
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
[imageView loadWithURLString:@"image url"];
//if you want to load image cache and then load download you can use following method
[imageView loadWithURLString:@"image url" loadFromCacheFirst:YES];
//If you want to get complete event
[imageView loadWithURLString:@"image url" complete:^(UIImage *image) {
//you can assing your image to your object here.
}];
//if you want cache and image handle
[imageView loadWithURLString:@"image url" loadFromCacheFirst:YES complete:^(UIImage *image) {
}];
新的可達性類以支持在更改網絡狀態時ブロック,可用狀態:
SWNetworkReachabilityStatusNotReachable
SWNetworkReachabilityStatusReachableViaWWAN
SWNetworkReachabilityStatusReachableViaWiFi
if ([SWReachability getCurrentNetworkStatus] == SWNetworkingReachabilityStatusNotReachable) {
//connection not available.
}
//if you want to get status change notification
[SWReachability checkCurrentStatus:^(SWNetworkingReachabilityStatus currentStatus) {
//current status when call method
} statusChange:^(SWNetworkingReachabilityStatus changedStatus) {
//every time when change status
}];
SWPOSTRequest *postRequest = [[SWPOSTRequest alloc]init];
postRequest.responseDataType = [SWResponseJSONDataType type];
//need to crate files array to upload
UIImage *image = [UIImage imageNamed:@"skywite"];
NSData *imageData = UIImagePNGRepresentation(image);
SWMedia *file1 = [[SWMedia alloc]initWithFileName:@"imagefile.png" key:@"image" data:imageData];
//create with custom mine type one
SWMedia *file2 = [[SWMedia alloc]initWithFileName:@"image.jpg" key:@"image2" mineType:@"image/jpeg" data:imageData];
//create an array with files
NSArray *fileArray = @[file1, file2];
[postRequest startUploadTaskWithURL:@"http://127.0.0.1:3000/drivers" files:fileArray parameters:@{@"name": @"this is name", @"address": @"your address"} parentView:nil success:^(NSURLSessionUploadTask *uploadTask, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
NSLog(@"%@", error);
}];
[postRequest setUploadProgressBlock:^(long long bytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"bytesWritten => %lld and totalBytesExpectedToWrite = %lld", bytesWritten, totalBytesExpectedToWrite);
}];
SWGETRequest *getR = [[SWGETRequest alloc]init];
[getR startDownloadTaskWithURL:@"http://samples.mplayerhq.hu/A-codecs/ACELP.net/2001-04-11.asf" parameters:nil parentView:nil cachedData:^(NSCachedURLResponse *response, NSURL *location) {
} success:^(NSURLSessionDownloadTask *uploadTask, NSURL *location) {
NSLog(@"location %@", location);
} failure:^(NSURLSessionTask *uploadTask, NSError *error) {
NSLog(@"error %@", error);
}];
[getR setDownloadProgressBlock:^(long long bytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"bytesWritten => %lld and totalBytesExpectedToWrite = %lld", bytesWritten, totalBytesExpectedToWrite);
}];
您可以将自定义对象作为 userObject
设置到您的请求对象中。这将允许任何类型的对象。
SWGETRequest *getRequest = [[SWGETRequest alloc]init];
getRequest.userObject = //any type object.
您可以为请求设置 tag
。
SWGETRequest *getRequest = [[SWGETRequest alloc]init];
getRequest.tag = 12;
SWNetworking
发起的离线请求这非常简单。首先,您需要发送离线请求过期时间,单位为秒。
[SWOfflineRequestManger requestExpireTime:1300 ];
您有带参数传递的方法 sendLaterIfOllfine
。只需传递 YES
即可。就这样。
SWGETRequest *getR = [[SWGETRequest alloc]init];
getR.tag = 400;
[getR startDataTaskWithURL:@"http://www.google.com" parameters:nil parentView:nil sendLaterIfOffline:YES cachedData:^(NSCachedURLResponse *response, id responseObject) {
} success:^(NSURLSessionDataTask *operation, id responseObject) {
} failure:^(NSURLSessionTask *operation, NSError *error) {
}];
如果您想捕获离线请求,需要使用以下方法。最好将以下行添加到您的 AppDelegate
didFinishLaunchingWithOptions 方法中。
[[SWOfflineRequestManger sharedInstance] requestSuccessBlock:^(SWRequest *operation, id responseObject) {
NSLog(@"%d", operation.tag);
} requestFailBlock:^(SWRequest *operation, NSError *error) {
}];
请注意,您需要设置 tag
或 userObject
以识别请求。 userObject
应为 ‘NSCording’ 支持对象。
请使用以下方法为 UIProgressView 设置任务。
-(void)setDownloadTask:(NSURLSessionDownloadTask *)downloadTask;
-(void)setUploadTask:(NSURLSessionUploadTask *)downloadTask;
SWNetworking
由 SkyWite 拥有和维护。SWNetworking
由 saman kumara 原创创建。如果您想联系 [email protected]。
如果您认为您发现了 SWNetworking
的安全漏洞,您应尽快通过电子邮件向 [email protected] 报告。请勿将其发布到公共问题跟踪器。
SWNetworking
在 MIT 许可证下发布。有关详细信息,请参阅 LICENSE。