与 Dropbox API v2 集成的最完整的第三方 Objective-C SDK。
除了 Dropbox Business API 之外,所有 Dropbox HTTP API v2 可以做到的事情。
ObjectiveDropbox 通过 CocoaPods 提供。要安装
它,只需将以下行添加到您的 Podfile 中
pod "ObjectiveDropbox"
或者您可以通过将源文件复制到您的项目中手动安装它。在这种情况下,您应将 WebKit.framework 添加到您的目标中,状态为 'Optional'。
要运行示例项目,克隆仓库,然后从 Example 目录中运行 'ObjectiveDropbox.xcworkspace'。
使用以下导入
#import "DropboxClient.h"
然后创建 DropboxClient
DropboxClient *dropboxClient = [[DropboxClient alloc] initWithAppKey:<DropboxApp key>
redirectURL:<redirectURL> restartAllTasksAfterRelaunch:YES];
我建议使用 'https://' 作为 redirectURL(别忘了在您的 Dropbox App 页面上添加它)。
如果您想同时使用多个 Dropbox 账户,请使用此初始化器创建每个 DropboxClient
DropboxClient *dropboxClient = [[DropboxClient alloc] initWithAppKey:<DropboxApp key>
redirectURL:<redirectURL>
restartAllTasksAfterRelaunch:YES
keychainAccount:<account string>];
在第一次请求之前获取访问令牌
(用户将在此步骤看到 Dropbox 身份验证 UI)
[dropboxClient getNewTokenWithSuccess:^ { ... } fail:^(NSString * _Nonnull errorSummary) { ... }];
访问令牌将被保存在用户的密钥链中并在以后恢复。如果您的 dropboxClient.accessToken 不为 nil,您无需调用该方法。
如果您想更改 Dropbox 用户,请使用此代码
dropboxClient.accessToken = nil;
然后再次调用 [dropboxClient getNewTokenWithSuccess:fail:]。
像这样使用所有 Dropbox 功能
DropboxCommitInfo *commitInfo = [[DropboxCommitInfo alloc] initWithPath:@"/myfile.txt"
mode:[[DropboxWriteMode alloc] initWithOverwrite]];
[dropboxClient.files upload:commitInfo sourceFileUrl:sourceFileURL
progress:^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
NSLog(@"Upload progress %i%%", (int)(100.0 * totalBytesSent/totalBytesExpectedToSend));
} success:^(DropboxFileMetadata * _Nonnull uploadResult) {
NSLog(@"File uploaded: %@", uploadResult);
} fail:^(DropboxError * _Nonnull error) {
NSLog(@"Upload failed with error: %@", error);
}];
查看 DropboxClient.h 了解所有可用方法。
每个下载/上传方法都返回一个任务对象。您可以取消、暂停和恢复它。您的下载/上传任务将在后台保持活跃(您的目标特权必须启用后台获取)。
如果您想成为优秀的 iOS 市民,请将以下行添加到您的 AppDelegate 实现
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler
{
NSDictionary *userInfo = @{@"sessionIdentifier": identifier, @"completionHandler": completionHandler};
[[NSNotificationCenter defaultCenter] postNotificationName:@"ObjectiveDropboxBackgroundSessionUpdated" object:nil userInfo:userInfo];
}
Mikhail Motyzhenkov, [email protected]
ObjectiveDropbox 提供 MIT 许可证。请参阅 LICENSE 文件以获取更多信息。