gpt4 book ai didi

iOS NSURLSession 下载

转载 作者:行者123 更新时间:2023-11-29 10:42:02 28 4
gpt4 key购买 nike

我得到这段代码来实现一些东西,它可以帮助我从给定的 URL 下载文件。

-(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];

}

所有这些代码都在我的 Download.m 中

我的download.h是:

@interface Download : NSObject
-(void) downloadFileWithProgress
@end

我真的不知道如何开始下载。在另一个类中,我创建了一个应该开始下载的按钮:

-(IBAction)buttonStartDownload:(id)sender {
[Download downloadFileWithProgress];
}

错误在最后一行:

No known class method for selector 'downloadFileWithProgress'

但为什么呢?

最佳答案

方法“-(void) downloadFileWithProgress”是一个实例方法,因此您不能使用类名“Download”调用此方法。

为了调用此方法,您需要创建“下载”类的实例并在该实例上调用该方法。

关于iOS NSURLSession 下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24080543/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com