iOS NSURLSession下载
我得到这段代码是为了实现一些东西,帮助我从给定的URL下载文件iOS NSURLSession下载,ios,nsurl,Ios,Nsurl,我得到这段代码是为了实现一些东西,帮助我从给定的URL下载文件 -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { NSLog(@"Temporary File :%@\n", location); NSError *err = nil; NS
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
NSLog(@"Temporary File :%@\n", location);
NSError *err = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *docsDirURL = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:@"out1.zip"]];
if ([fileManager moveItemAtURL:location
toURL:docsDirURL
error: &err])
{
NSLog(@"File is saved to =%@",docsDir);
}
else
{
NSLog(@"failed to move: %@",[err userInfo]);
}
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
//You can get progress here
NSLog(@"Received: %lld bytes (Downloaded: %lld bytes) Expected: %lld bytes.\n",
bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
}
第二部分:
-(void) downloadFileWithProgress
{
NSURL * url = [NSURL URLWithString:@"https://s3.amazonaws.com/hayageek/downloads/SimpleBackgroundFetch.zip"];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDownloadTask * downloadTask =[ defaultSession downloadTaskWithURL:url];
[downloadTask resume];
}
所有这些代码都在我的下载中
我的下载.h是:
@interface Download : NSObject
-(void) downloadFileWithProgress
@end
我真的不知道如何开始下载。在另一个类中,我创建了一个按钮,可以开始下载:
-(IBAction)buttonStartDownload:(id)sender {
[Download downloadFileWithProgress];
}
错误在最后一行:
No known class method for selector 'downloadFileWithProgress'
但是为什么?方法“-(void)downloadFileWithProgress”是一个实例方法,因此不能使用类名“Download”调用此方法
要调用此方法,您需要创建“Download”类的实例并在该实例上调用该方法。我们需要更多信息,您是否导入了Download helper类?为什么您需要三个不同的类?好的,是的,我在“DownloadViewController.m”中导入了“Download.h”将下载代码外包到一个类中并在DownloadViewController类中引用它是否有用?我认为现在将所有内容都放在DownloadViewController类中比较容易,因为你只需要外包一个功能。好的,谢谢。我会试试看,然后告诉你我是否成功;)
Method -(void)downloadFilwWithProgress in instance method...So to call that method
-(IBAction)buttonStartDownload:(id)sender {
Download *downldObj=[[Download alloc]init];
[downldObj downloadFileWithProgress];
}
If you write method +(void)downloadFilwWithProgress then you can call like this.[Download downloadFileWithProgress]