嘗試對AFNetworking https://github.com/AFNetworking/AFNetworking
可以在AppDelegate中定義該部分,但一定要使其可見一看(屬性)
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
然後在任意視圖讓他們和啓動下載任務
NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
如果您需要一些額外的信息,我忘了在這裏提到,請提及 –
問題em是,你可能正在使用主線程執行下載任務,爲什麼不使用異步線程在後臺下載文件,並仍然將進度更新發送到主線程上的UI? – Satyam
但問題是每個下載任務,我做了一個對象,有幾個標誌和屬性,如下載的數據,進度viewView等.....如何可能 –