iOS 共享到Dropbox
2019-11-29 本文已影响0人
valiant_xin
搞完印象笔记,心情总体来说虽然不爽,但还算OK,但是当看到Dropbox的时候,整个人直接炸了,wtf?还要收费?想要放弃,然后去看了看Box,结果也是,注册收费。
emmmm……心情瞬间低落谷底,这TM还怎么玩?
不过想了想,我只是接你的共享,应该不至于那么丧心病狂的要收费吧?又在官网上晃悠了很久,突然在某一瞬间,突然发现了新大陆,免费注册App!!
终于找到你,还好我没有放弃😭
废话不多说,直接开始吧。
![](https://img.haomeiwen.com/i2847194/597f972b77a2ed92.png)
![](https://img.haomeiwen.com/i2847194/614ac1f519e25f17.png)
pod导入
pod 'ObjectiveDropboxOfficial', '~> 3.4.0'
注册
// dropbox
[DBClientsManager setupWithAppKey:@"jkf1tf5wzyw46tv"];
在- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options方法中,官网给出的说法是souceApplication是com.apple.SafariViewService。我觉得这里直接使用scheme进行判断应该也是合理的。
在delegate文件的openURL方法中:
DBOAuthResult *authResult = [DBClientsManager handleRedirectURL:url];
if (authResult != nil) {
if ([authResult isSuccess]) {
NSLog(@"Success! User is logged into Dropbox.");
return YES;
} else if ([authResult isCancel]) {
NSLog(@"Authorization flow was manually canceled by user!");
return NO;
} else if ([authResult isError]) {
NSLog(@"Error: %@", authResult);
return NO;
}
}
开始
- (void)dropboxAction {
[self dropboxAuthor];
}
授权
// 授权
- (void)dropboxAuthor {
if ([DBClientsManager authorizedClient].isAuthorized) {
[self dropboxUploadFile:[[NSBundle mainBundle] pathForResource:@"4" ofType:@"pdf"]];
}else {
[DBClientsManager authorizeFromController:[UIApplication sharedApplication] controller:self openURL:^(NSURL *url) {
// 这里发送消息给delegate
[[UIApplication sharedApplication] openURL:url options:nil completionHandler:^(BOOL success) {
}];
// 这里做处理
DBOAuthResult *authResult = [DBClientsManager handleRedirectURL:url];
if (authResult != nil) {
if ([authResult isSuccess]) {
NSLog(@"Success! User is logged into Dropbox.");
[self dropboxUploadFile:[[NSBundle mainBundle] pathForResource:@"4" ofType:@"pdf"]];
} else if ([authResult isCancel]) {
NSLog(@"Authorization flow was manually canceled by user!");
} else if ([authResult isError]) {
NSLog(@"Error: %@", authResult);
}
}
}];
}
}
共享文件
- (void)dropboxUploadFile:(NSString *)filePath {
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
// For overriding on upload
DBFILESWriteMode *mode = [[DBFILESWriteMode alloc] initWithOverwrite];
DBUserClient *client = [DBClientsManager authorizedClient];
[[[client.filesRoutes uploadData:@"/4.pdf" mode:mode autorename:@(YES) clientModified:nil mute:@(NO) propertyGroups:nil inputData:fileData] setResponseBlock:^(DBFILESFileMetadata * _Nullable result, DBFILESUploadError * _Nullable routeError, DBRequestError * _Nullable networkError) {
if (result) {
NSLog(@"%@\n", result);
} else {
NSLog(@"%@\n%@\n", routeError, networkError);
}
}] setProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
NSLog(@"\n%lld\n%lld\n%lld\n%.2f%%\n", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite,totalBytesWritten*100.0/totalBytesExpectedToWrite);
}];
}
又搞定一个。
奉上参考文档:
Dropbox API v2集成