ios - 如何在导航 View Controller 时获取当前正在运行的 NSURLSession 后台 session ?

标签 ios objective-c session ios7

我正在开发一个 iOS 应用程序,用于从服务器下载文件 (<10MB)。我使用 NSURLSessionNSURLSessionDownloadTask 进行下载。

我的问题是,下载 View Controller 不是 rootViewController。当我返回到 Root View Controller 时,我可以看到下载进度仍然有效(来自 NSLog)。但是当我再次下载 View Controller 时,我看不到我的标签根据进度更新了。在这种情况下,我如何才能获取当前正在运行的 NSURLSession 后台 session 来更新我的状态标签?或者其他解决方案?

//Start downloading
-(void)startDownload: (NSString *)url{
    NSString *sessionId = MY_SESSION_ID;
    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:sessionId];
    sessionConfiguration.HTTPMaximumConnectionsPerHost = 1;
    self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration
                                                    delegate:self
                                            delegateQueue:nil];
    self.downloadTask = [self.session downloadTaskWithURL:[NSURL URLWithString:url]];
    [self.downloadTask resume];
}

//Delegate
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{

    if (totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown) {
        NSLog(@"Unknown transfer size");
    }
    else{
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            NSInteger percentage = (double)totalBytesWritten * 100 / (double)totalBytesExpectedToWrite;
            self.percentageLabel.text = [NSString stringWithFormat:@"Downloading (%ld%%)", (long)percentage];
            NSLog(@"Progress: %ld", (long)percentage);
        }];
    }
}

最佳答案

这就是我在我的代码中所做的并且它起作用了:

[self performSelectorOnMainThread:@selector(setuploadStatus:) withObject:[NSString stringWithFormat:@"Upload %ld%%", (long)percentage] waitUntilDone:NO];

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {

    NSInteger percentage = (double)totalBytesSent * 100 / (double)totalBytesExpectedToSend;

    **[self performSelectorOnMainThread:@selector(setuploadStatus:) withObject:[NSString stringWithFormat:@"Upload %ld%%", (long)percentage] waitUntilDone:NO];**

    NSLog(@"Upload %ld%% ",(long)percentage);

}

-(void) setuploadStatus : (NSString *) setStat  {

    [_notifyTextLabel setText:setStat];

}

关于ios - 如何在导航 View Controller 时获取当前正在运行的 NSURLSession 后台 session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23750720/

相关文章:

ios - 如何关闭带有淡出动画的模态 VC?

ios - iOS const CGFloat错误:链接器命令失败

php - 检查 PHP session 是否已经开始

node.js - 在 Express.js 中使用 session

ios - UITableView 部分中的背景图像隐藏部分标题

ios - 在 iPhone5 上有 3 个输入 channel (不是 1 个)

iOS 如何保存 map 注释并将它们加载到 mapkit 上?

objective-c - 使用 64 位 arm64 构建时 objc_msgSend 函数指针崩溃

ios - 我可以在 Delphi XE5 中使用 Objective C 代码吗

javascript - 在 Session 中存储随机字符串,当我通过另一个 PHP 文件调用它时,该字符串会被覆盖