测试测试 | ✗ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2014年12月 |
由 Dalton Claybrook 维护。
一组有用的类,用于获取和持久化文件数据。它基于 NSURLSession。
初始化 & 配置
self.fileManager = [[SFSFileManager alloc] init];
self.fileManager.usesEncryptionByDefault = YES;
self.fileManager.diskSizeLimit = 512 * 1024 * 1024; // 512 MB
最简单的获取
NSURL *url = [NSURL URLWithString:@"http://placekitten/600/500"];
[self.fileManager fetchFileDataAtURL:url withCompletion:^(NSURL *fileURL, NSError *error) {
// data fetched using '[url absoluteString]' as the identifier
// and 'SFSFileManagerDefaultFileGroup' as the file group
}];
复杂的获取
NSURL *url = [NSURL URLWithString:@"http://myapi.com/users"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[request setValue:<#auth string#> forHTTPHeaderField:@"Authorization"];
SFSFileFetchRequest *request = [SFSFileFetchRequest request];
request.urlRequest = urlRequest;
request.identifier = @"12345";
request.fileGroup = @"userDataGroup";
request.encryptionPolicy = SFSFileFetchRequestEncryptionPolicyUseEncryption;
request.taskPriority = SFSFileFetchRequestTaskPriorityHigh;
__typeof__(self) __weak weakSelf = self;
id<SFSTask> task = [self.fileManager fetchFileDataUsingFetchRequest:request withCompletion:^(NSURL *fileURL, NSError *error) {
[weakSelf useFileAtURL:fileURL error:error];
}];
// Some time later...
if ([task isRunning])
{
[task ignoreResults]; //will cause the above completion block to not be called, but will not cancel the request.
// or
[task cancelRequest];
}
图像管理器
// using 'nil' causes a file manager to be created for you.
self.imageManager = [[SFSImageManager alloc] initWithFileManager:nil];
NSURL *url = [NSURL URLWithString:@"http://placekitten/600/500"];
__typeof__(self) __weak weakSelf = self;
[self.imageManager fetchImageAtURL:url withCompletion:^(UIImage *image, NSError *error) {
// completion is executed on the main thread.
weakSelf.imageView.image = image;
}];
集成 SpaceFactoryNetworking 最简单的方法是使用 CocoaPods
# Example Podfile
pod 'SpaceFactoryNetworking'
否则,您可以克隆此仓库,并从 'SpaceFactoryNetworking/Core' 文件夹导入文件。
欢迎使用为该项目提交拉取请求。如果您考虑这样做,请通过[email protected]与我联系。我想了解一下。