TBDropboxKit 1.2.3

TBDropboxKit 1.2.3

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最新发布2019年1月

Kanstantsin Bucha维护。



 
依赖
ObjectiveDropboxOfficial= 3.9.4
CDBKit>= 0
CDBDelegateCollection>= 0
TBLogger>= 0
 

TBDropboxKit

优雅地应用于 App Store

CI Status Version License Platform

简介

稳定版本,核心更新到 ObjectiveDropboxOfficial 3.1.2。此框架为 Dropbox 和双向更改通知提供了基本功能。它底层使用官方 Dropbox API 版本 2。它平滑了官方每发布时做的破坏性更改。它使用 SOLID 原则和 DRY 模式编写。

版本

1.1.5 
    Updated to to ObjectiveDropboxOfficial 3.1.2 and checked
    Improved iOS example logging

示例

  1. 在应用程序启动后启动 Dropbox
self.dropbox = [TBDropboxClient sharedInstance];
// passing YES to ConnectionDesired makes it connect instantly
// use NO if you want connect after some onthe services will start or 
// based on application settings like I do in this example
[self.dropbox initiateWithConnectionDesired: self.settings.syncType == AppSyncDropbox
                                usingAppKey: self.dropboxAppKey];
[self.dropbox addDelegate: self];

// you could use some specific settings to make framework log more events
self.dropbox.tasksQueue.batchSize = QM_Dropbox_Queue_Batch_Size;
self.dropbox.watchdog.logger.logLevel = TBLogLevelInfo;
self.dropbox.connection.logger.logLevel = TBLogLevelVerbose;
self.dropbox.logger.logLevel = TBLogLevelInfo;
  1. 实现此代理的这些方法
#pragma mark - TBDropboxClientDelegate -

#pragma mark TBDropboxClient

- (void)client:(TBDropboxClient *)client
    didReceiveIncomingChanges:(NSArray<TBDropboxChange *> *)changes {    
     // get your sinchronized documents base url
    NSArray * URLs = [[NSFileManager defaultManager] URLsForDirectory: NSDocumentDirectory
                                                            inDomains: NSUserDomainMask];
    NSURL * baseURL = URLs.lastObject;

    for (TBDropboxChange * change in changes) {
        NSURL * itemURL = [change localURLUsingBaseURL: baseURL];
        NSLog(@"%@", change);
        switch (change.action) {
            case TBDropboxChangeActionUpdateFile: {
                [self dropboxSyncChangedDocumentAtURL: itemURL
                                          dropboxPath: change.dropboxPath
                                           completion: ^(NSError * _Nullable error) {
                      if (error != nil) {
                          NSLog(@"Dropbox local state Synchronization Failed %@", error);
                          return;
                      }
                      // update ui
                      //[self handleDidChangeDocumentAtURL: itemURL];  
                  }];
            }   break;
            case TBDropboxChangeActionDelete: {
                // delete file and update ui
                //[self handleDidRemoveDocumentAtURL: itemURL];
               
            }   break;
            case TBDropboxChangeActionUpdateFolder: {
                // Do nothing for created folders,
                // will create them when files appears inside
            }   break;
            default: {
                NSLog(@" acquire unsupported action in Dropbox change = %@",
                change);
            }   break;
        }
    }
}

#pragma mark TBDropboxConnectionDelegate

- (void)dropboxConnection:(TBDropboxConnection *)connection
         didChangeStateTo:(TBDropboxConnectionState)state {
    switch (connection.state) {
        case TBDropboxConnectionStateReconnected: {
            [self handleDropboxDidReconnect];
            // available notifications about server changes
            self.dropbox.watchdogEnabled = YES;
        }   break;
        case TBDropboxConnectionStateConnected: {
            [self handleDropboxDidConnect];
            // available notifications about server changes
            self.dropbox.watchdogEnabled = YES;
        }   break;
        case TBDropboxConnectionStatePaused:
        case TBDropboxConnectionStateDisconnected: {
            [self handleDropboxDidDisconnect];
            self.dropbox.watchdogEnabled = NO;
        }   break;
        case TBDropboxConnectionStateAuthorization: {
            
        }   break;
        case TBDropboxConnectionStateUndefined: {
            
        }   break;
        
        default:
            break;
    }
}

- (void)dropboxConnection:(TBDropboxConnection *)connection
     didChangeAuthStateTo:(TBDropboxAuthState)state
                withError:(NSError *)error {
    
}

3). 创建任务然后将其添加到队列中,以上传/删除/移动

using [self.dropbox.tasksQueue addTask: task];

- (TBDropboxUploadFileTask *)dropboxUploadTaskUsingDocumentURL:(NSURL *)documentURL
                                                       baseURL:(NSURL *)baseURL
                                                withCompletion:(CDBErrorCompletion)completion {
    TBDropboxFileEntry * entry =
        [TBDropboxEntryFactory fileEntryByMirroringLocalURL: documentURL
                                               usingBaseURL: baseURL];
    TBDropboxUploadFileTask * result =
        [TBDropboxUploadFileTask taskUsingEntry: entry
                                        fileURL: documentURL
                                     completion: ^(TBDropboxTask * _Nonnull task,
                                                   NSError * _Nullable error) {
        if (completion != nil) {
            completion(error);
        }
    }];
    
    return result;
}

- (TBDropboxDeleteEntryTask *)dropboxDeleteTaskUsingDocumentURL:(NSURL *)documentURL
                                                        baseURL:(NSURL *)baseURL
                                                 withCompletion:(CDBErrorCompletion)completion {
    TBDropboxFileEntry * entry =
    [TBDropboxEntryFactory fileEntryByMirroringLocalURL: documentURL
                                           usingBaseURL: baseURL];
    TBDropboxDeleteEntryTask * result =
        [TBDropboxDeleteEntryTask taskUsingEntry: entry
                                      completion: ^(TBDropboxTask * _Nonnull task,
                                                    NSError * _Nullable error) {
        completion(error);
    }];
    
    return result;
}

- (TBDropboxMoveEntryTask *)dropboxMoveTaskUsingDocumentURL:(NSURL *)documentURL
                                             destinationURL:(NSURL *)destinationURL
                                                    baseURL:(NSURL *)baseURL
                                             withCompletion:(CDBErrorCompletion)completion {
    TBDropboxFileEntry * entry =
        [TBDropboxEntryFactory fileEntryByMirroringLocalURL: documentURL
                                               usingBaseURL: baseURL];
    TBDropboxFileEntry * destinationEntry =
        [TBDropboxEntryFactory fileEntryByMirroringLocalURL: destinationURL
                                               usingBaseURL: baseURL];
    TBDropboxMoveEntryTask * result =
        [TBDropboxMoveEntryTask taskUsingEntry: entry
                              destinationEntry: destinationEntry
                                    completion: ^(TBDropboxTask * _Nonnull task,
                                                   NSError * _Nullable error) {
        completion(error);
    }];
    
    return result;
}

4). 同步传入的更改 & 下载

- (void)dropboxSyncChangedDocumentAtURL:(NSURL *)URL
                            dropboxPath:(NSString *)path
                             completion:(CDBErrorCompletion)completion {
    if (self.dropbox.connection.connected == NO) {
        if (completion != nil){
       	 	NSString * description =
        		[NSString stringWithFormat: @"Failed to proceed with disconnected dropbox\
              		                        \r document URL: %@", URL];
   			    NSError * error = [NSError errorWithDomain: NSStringFromClass([self class])
                                        		      code: 4
                                    		      userInfo: @{ NSLocalizedDescriptionKey: description }];
            completion(error);
        }
        return;
    }
    
    TBDropboxFileEntry * entry =
        [TBDropboxEntryFactory fileEntryUsingDropboxPath: path];
    TBDropboxTask * downloadTask =
        [TBDropboxDownloadFileTask taskUsingEntry: entry
                                          fileURL: URL
                                       completion: ^(TBDropboxTask * _Nonnull task, NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"FAILED dropboxSyncChangedDocumentAtURL: %@\
                       /r Dropbox path %@", URL, path);
        }
        if (completion != nil) {
            completion(error);
        }
    }];
    
    if (downloadTask == nil) {
        NSLog(@"FAILED create task for dropboxSyncChangedDocumentAtURL: %@\
                   \r Dropbox path: %@", URL, path);
    }
    
    [self.dropbox.tasksQueue addTask: downloadTask];
}

5). 清理

- (void)cleanupDropboxAppContainerWithCompletion:(CDBErrorCompletion _Nonnull)completion {
    TBDropboxFolderEntry * rootEntry = [TBDropboxEntryFactory folderEntryUsingDropboxPath:nil];
    TBDropboxListFolderTask * cleanupTask =
        [TBDropboxListFolderTask taskUsingEntry: rootEntry
                                     completion: ^(TBDropboxTask * _Nonnull task,
                                                   NSError * _Nullable error) {
         NSArray * entries = [(TBDropboxListFolderTask *)task folderEntries];
         
         if (entries.count == 0) {
             if (completion != nil) {
                 completion(nil);
             }
             return;
         }
         
         __block NSUInteger counter = 0;
         __block NSUInteger count = entries.count;
         __block NSError * deleteError = nil;
         
         for (id<TBDropboxEntry> deleteEntry in entries) {
             TBDropboxDeleteEntryTask * deleteTask =
                [TBDropboxDeleteEntryTask taskUsingEntry: deleteEntry
                                              completion: ^(TBDropboxTask * _Nonnull task, NSError * _Nullable error) {
                    counter++;
                    if (deleteError != nil) {
                        deleteError = error;
                    }

                    if (counter == count
                        && completion != nil) {
                        completion(error);
                    }
                }];
             
             [self.dropbox.tasksQueue addTask: deleteTask];
         }
    }];
    
    if (cleanupTask == nil
        && completion != nil) {
        NSDictionary * info = @{NSLocalizedDescriptionKey : @"Clean up root folder task failed"};
        NSError * error = 
        	[NSError errorWithDomain: NSStringFromClass([self class])
                                code: 1
                            userInfo: info];
        completion(error);
    }
    
    [self.dropbox.tasksQueue addTask: cleanupTask];
}

要运行示例项目,请克隆仓库,然后首先从示例目录运行pod install

需求

安装

TBDropboxKit可通过CocoaPods获取。要安装它,只需在Podfile中添加以下行:

pod "TBDropboxKit"

作者

truebucha, Kanstantsin Bucha [email protected]

许可

TBDropboxKit遵循MIT许可。详见LICENSE文件中的更多信息。