ios - 离开 uiviewcontroller 时 Dropbox 上传停止

标签 ios iphone dropbox

我在保管箱上传时遇到问题。

当我停留在我编写了 Dropbox 上传代码的 View 上时,它工作正常。但是,如果在上传过程中我离开 View 并打开另一个屏幕,则上传将停止或取消。

我正在使用下面的代码

- (void)didPressLink {
    self.restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
    self.restClient.delegate = self;
    if(![[DBSession sharedSession] isLinked]) {
        [[DBSession sharedSession] linkFromController:self];
    }
}

-(void)saveFileOnDropBox{
    [self didPressLink];
    NSString *filename = [soundOneNew lastPathComponent];
    NSString *destDir = @"/";
    [[self restClient] uploadFile:filename toPath:destDir    withParentRev:nil fromPath:soundOneNew];
}

-(void)restClient:(DBRestClient*)client uploadedFile:(NSString*)destPath
         from:(NSString*)srcPath metadata:(DBMetadata*)metadata {

     NSLog(@"File uploaded successfully to path: %@ %@", metadata.path, destPath);

     [[self restClient] loadSharableLinkForFile: [NSString stringWithFormat:@"/%@",metadata.path]];
}

- (void)restClient:(DBRestClient*)client uploadFileFailedWithError:(NSError*)error {
    NSLog(@"File upload failed with error - %@", error);
}

 - (void)restClient:(DBRestClient*)restClient loadedSharableLink:(NSString*)link
       forFile:(NSString*)path
{
   NSLog(@"Sharable link %@",link);
   NSLog(@"File Path %@ ",path);

}


-(void) restClient:(DBRestClient *)client uploadProgress:(CGFloat)progress forFile:(NSString *)destPath from:(NSString *)srcPath {
   static NSDate* date = nil;
   static double oldUploadedFileSize = 0;
   if (!date) {
       date = [NSDate date] ;
   } else {
        NSTimeInterval sec = -[date timeIntervalSinceNow];
        date = [NSDate date] ;
       NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:srcPath error:nil];
       double uploadedFileSize = (double)[fileAttributes fileSize] * progress;
       if (sec) {
           NSLog(@"speed approx. %.2f KB/s", (uploadedFileSize - oldUploadedFileSize )/1024.0 / sec );
       }
       oldUploadedFileSize = uploadedFileSize;
       self.uploadAudioProgress.progress = progress;
    }
}

即使我离开 View ,如何保持上传过程继续

最佳答案

你打电话

self.restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];

然后创建一个新的 REST 客户端

[[self restClient] uploadFile:filename toPath:destDir withParentRev:nil fromPath:soundOneNew];

上传。因此,此客户端由 View Controller 拥有,并在 View Controller 被销毁时被销毁 - 即当您离开它时。

你想要一些其他类和它的一个实例(可能是一个单例或一个由应用程序委托(delegate)拥有并传递给需要它的实例),它拥有所有创建的 REST 客户端并保留它们直到它们完成。

关于ios - 离开 uiviewcontroller 时 Dropbox 上传停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30274736/

相关文章:

ios - 如何在 iOS 10.2 上从受密码保护的 PDF 获取超链接(URL)

ios - BSD 套接字在 iPhone 3G 环境中不起作用

java - Dropbox 数据存储 API : Reliably determining if the user is authenticated

ios - 当文本长度 <=4 时,如何禁用文本字段的 UIKeyboardType 默认值上的默认返回键

ios - 为什么我的自定义 UITableViewCell 没有显示?

ios - 从 UIWebView 在 Safari 中打开 URL 文件 (.pdf)

iphone - 适用于残疾人的iPad/iPhone应用程序

iphone - iOS 中当前为您提供的 TTF 获取 UIFont 的方法是什么?

从 Dropbox 加载数据时,iOS UITableView 不会显示任何内容

c# - Dropbox API "USER TOKEN", "USER SECRET"