DropBlocks是对Dropbox iOS SDK的封装,允许您使用块而不是委托回调。
您不再这样做
[restClient loadFile:path intoPath:destinationPath];
然后实现这些回调
- (void)restClient:(DBRestClient*)client loadedFile:(NSString*)destPath contentType:(NSString*)contentType metadata:(DBMetadata*)metadata {
NSLog(@"Yay, my file loaded. Let me go find that progress view so I can dismiss it ...");
}
- (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath {
NSLog(@"Got some file load progress, let me go find that progress view so I can update it ...");
}
- (void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)error {
NSLog(@"Uh oh, something went wrong with the file load. Let me go figure out which one that was ...");
}
在DropBlocks中,您可以一键完成所有操作,如下所示
UIProgressView* progressView = ... //make me a progressView and present it
[DropBlocks loadFile:path intoPath:intoPath:destinationPath completionBlock:^(NSString* contentType, DBMetadata* metadata, NSError* error) {
[progressView removeFromSuperview];
if (error) {
NSLog(@"Uh oh, something went wrong with this file load. I'd better do something about that.");
} else {
NSLog(@"Yay, my file loaded. My work here is done.");
}
} progressBlock:^(CGFloat progress) {
progressView.progress = progress;
}];
我强烈建议您使用CocoaPods将DropBlocks集成到您的应用程序中。如果您不熟悉它,CocoaPods是针对Objective-C项目的一个非常简单的依赖管理器。您可以在这里了解更多信息。
如果您使用CocoaPods,您只需要在Podfile中添加以下行
pod 'DropBlocks', '0.0.3'
然后,就完成了。这也将自动将标准Dropbox SDK拉入您的项目。
如果您不使用CocoaPods(为什么不呢?您有什么问题?),那么您只需将DropBlocks源代码抓取并放入您的项目中。您将需要DropBlocks.m和DropBlocks.h。您还需要手动将标准Dropbox SDK集成到您的项目中。有关这方面的更多信息,请参阅Dropbox SDK文档。
您需要在代码中完成所有正常的Dropbox设置(链接帐户、创建会话等)。您可以在Dropbox SDK文档中找到关于该信息。
DropBlocks旨在替换您通常对DBRestClient类进行的所有调用。
尽管DropBlocks 0.0.3版本已经开始在更多生产级应用程序中看到使用,但它仍然是一个测试版发布。一些方法尚未经过彻底测试。请自行承担风险,并在遇到任何问题时请提交错误。
John Blanco (ZaBlanc) - 修复了文件上传并改进了回调类型定义以提高自动完成。 (#1)
Ryan Tsao (rtsao)
如果您在项目中使用DropBlocks,我很乐意了解它的使用情况。