2
我想通過使用NSURLSession而不是NSURLConnection從服務器下載文件。但是當我使用它時,一些委託方法不會調用,我不知道爲什麼。NSURLSession方法不會調用
#import "ViewController.h"
@interface ViewController()<NSURLSessionDelegate,NSURLSessionDataDelegate,NSURLSessionDownloadDelegate>
@property (strong,nonatomic) NSURLSessionDownloadTask *downloadTask;
@property (strong,nonatomic) NSURLSession *session;
@end
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSURLSessionConfiguration *backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.webcash.TestingNSURLSession"];
self.session = [NSURLSession sessionWithConfiguration:backgroundConfiguration delegate:self delegateQueue:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)startDownload:(id)sender {
NSURL *downloadRUL = [NSURL URLWithString:@"http://laguboard.com/music/down/33867378/2794942/NTU0NktpZE56eERWZGxVeC9zTS9LVUd1TEhkZTE0cFZuaW1QS1pFMHVhOUNkM2ZoZEE=/(http://mp3xload.wapka.me).mp3"];
self.downloadTask = [self.session downloadTaskWithURL:downloadRUL];
[self.downloadTask resume];
}
- (IBAction)stopDownload:(id)sender {
}
// ====== THIS METHOD IS NOT CALLING
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
if (totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown) {
return;
} else {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
double progress = (double)totalBytesWritten/(double)totalBytesExpectedToWrite;
NSLog(@"Downloading progress : %f",progress);
}];
}
}
// ==============
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
if (error != nil) {
NSLog(@"Error : %@",[error localizedDescription]);
} else {
NSLog(@"Download finished");
}
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *urls = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *documentsDirectory = [urls objectAtIndex:0];
NSURL *originalURL = [[downloadTask originalRequest] URL];
NSURL *destinationURL = [documentsDirectory URLByAppendingPathComponent:[originalURL lastPathComponent]];
[fileManager removeItemAtURL:destinationURL error:NULL];
BOOL success = [fileManager copyItemAtURL:location toURL:destinationURL error:&error];
if (success) {
NSLog(@"Download finished");
} else {
NSLog(@"Error : %@",error.description);
}
}
@end
我跟進了很多教程,但它仍然無法正常工作。請幫我解決這個問題。謝謝。!
,我應該怎麼做,如果我想知道下載進度的百分比是多少? – vichhai
我編輯了響應來計算下載進度的百分比。我希望它能幫助你 –