sách gpt4 ăn đã đi

iOS NSURLSession 下载

In lại Tác giả: Walker 123 更新时间:2023-11-29 10:42:02 28 4
mua khóa gpt4 giày 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);
}
khác
{
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:@"../../../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];
}

sai lầm在最后一行:

No known class method for selector 'downloadFileWithProgress'

但为什么呢?

câu trả lời hay nhất

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

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

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

28 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress