Alo*_*nha 7 nsurlconnection ios ios7 nsurlsession
我已经使用NSURLSession下载单个文件,它工作正常,现在我必须在后台下载三个文件,还必须管理他们在UIProgress中的进度.我的下载单个代码的代码如下.
- (IBAction)startBackground:(id)sender
{
// Image CreativeCommons courtesy of flickr.com/charliematters
NSString *url = @"http://farm3.staticflickr.com/2831/9823890176_82b4165653_b_d.jpg";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
self.backgroundTask = [self.backgroundSession downloadTaskWithRequest:request];
[self setDownloadButtonsAsEnabled:NO];
self.imageView.hidden = YES;
// Start the download
[self.backgroundTask resume];
}
- (NSURLSession *)backgroundSession
{
static NSURLSession *backgroundSession = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.shinobicontrols.BackgroundDownload.BackgroundSession"];
backgroundSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
});
return backgroundSession;
}
#pragma mark - NSURLSessionDownloadDelegate methods
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
double currentProgress = totalBytesWritten / (double)totalBytesExpectedToWrite;
dispatch_async(dispatch_get_main_queue(), ^{
self.progressIndicator.hidden = NO;
self.progressIndicator.progress = currentProgress;
});
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
// Leave this for now
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
// We've successfully finished the download. Let's save the file
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *documentsDirectory = URLs[0];
NSURL *destinationPath = [documentsDirectory URLByAppendingPathComponent:[location lastPathComponent]];
NSError *error;
// Make sure we overwrite anything that's already there
[fileManager removeItemAtURL:destinationPath error:NULL];
BOOL success = [fileManager copyItemAtURL:location toURL:destinationPath error:&error];
if (success)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithContentsOfFile:[destinationPath path]];
self.imageView.image = image;
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.hidden = NO;
});
}
else
{
NSLog(@"Couldn't copy the downloaded file");
}
if(downloadTask == cancellableTask) {
cancellableTask = nil;
} else if (downloadTask == self.resumableTask) {
self.resumableTask = nil;
partialDownload = nil;
} else if (session == self.backgroundSession) {
self.backgroundTask = nil;
// Get hold of the app delegate
SCAppDelegate *appDelegate = (SCAppDelegate *)[[UIApplication sharedApplication] delegate];
if(appDelegate.backgroundURLSessionCompletionHandler) {
// Need to copy the completion handler
void (^handler)() = appDelegate.backgroundURLSessionCompletionHandler;
appDelegate.backgroundURLSessionCompletionHandler = nil;
handler();
}
}
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
dispatch_async(dispatch_get_main_queue(), ^{
self.progressIndicator.hidden = YES;
[self setDownloadButtonsAsEnabled:YES];
});
}
Run Code Online (Sandbox Code Playgroud)
您可以让多个NSURLSessionDownloadTask使用相同的NSSession,并且每个NSSession在同一个mainQueue上一个接一个地执行.
成功下载后,他们致电:
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
Run Code Online (Sandbox Code Playgroud)
如果您下载100 mB的MP4和10KB的图片,那么他们将以不同的顺序返回此方法.
因此,要跟踪哪个会话以及在此返回了什么downloadTask
您需要在调用Web服务之前设置字符串标识符
区分/跟踪您设置的NSURLSessions
session.configuration.identifier
Run Code Online (Sandbox Code Playgroud)
区分NSURLSessionDownloadTask使用
downloadTask_.taskDescription
downloadTask_.taskDescription = [NSString stringWithFormat:@"%@",urlSessionConfigurationBACKGROUND_.identifier];
Run Code Online (Sandbox Code Playgroud)
例如,在我的项目中,我下载了许多用户喜欢的视频及其缩略图.
每个项目都有一个id,例如1234567所以我需要为每个收藏夹打两个电话
所以我创建了两个标识符
"1234567_VIDEO"
"1234567_IMAGE"
Run Code Online (Sandbox Code Playgroud)
然后调用两个ws调用并传入session.configuration.identifier中的标识符
http://my.site/getvideo/1234567
"1234567_VIDEO"
http://my.site1/getimage/1234567
"1234567_IMAGE"
Run Code Online (Sandbox Code Playgroud)
iOS7将在后台下载项目,app可以回去睡觉完成后调用
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
Run Code Online (Sandbox Code Playgroud)
然后我得到了
session.configuration.identifier
"1234567_IMAGE"
Run Code Online (Sandbox Code Playgroud)
将其拆分并检查值
1234567_IMAGE
"1234567"
"_IMAGE" > item at location is a MP4 so save as /Documents/1234567.mp4
"_VIDEO" > item at location is a jpg so save as /Documents/1234567.jpg
Run Code Online (Sandbox Code Playgroud)
如果您有3个网址可以调用,则每个NSSession可以有一个NSURLSessionDownloadTask
file 1 - NSSession1 > NSURLSessionDownloadTask1
file 2 - NSSession2 > NSURLSessionDownloadTask2
file 3 - NSSession3 > NSURLSessionDownloadTask3
Run Code Online (Sandbox Code Playgroud)
当应用程序位于前台时,这似乎工作正常.但是当使用BACKGROUND TRANSFER和BACKGROUND FETCH时我遇到了问题.第一个NSSession> NSURLSessionDownloadTask1将返回,然后不会调用其他任何一个.
在一个NSSession1中有多个NSURLSessionDownloadTask更安全
file 1 - NSSession1 > NSURLSessionDownloadTask1
file 2 - > NSURLSessionDownloadTask2
file 3 - > NSURLSessionDownloadTask3
Run Code Online (Sandbox Code Playgroud)
执行此调用时要小心NSSession finishTasksAndInvalidate not invalidateAndCancel
//[session invalidateAndCancel];
[session finishTasksAndInvalidate];
Run Code Online (Sandbox Code Playgroud)
invalidateAndCancel将停止会话而不完成其他下载任务
| 归档时间: |
|
| 查看次数: |
6428 次 |
| 最近记录: |