【问题标题】:Slow download with Apple sample code + progress使用 Apple 示例代码 + 进度缓慢下载
【发布时间】:2023-03-09 07:50:01
【问题描述】:

我已经从 Apple 网站实现了以下方法,可在此页面上找到:

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLDownload.html

//on my .h file:

@interface AppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate, NSURLDownloadDelegate>
{
    BOOL allJobDone;
@private
    NSURLResponse*downloadResponse;
    long long bytesReceived;
}


//on my .m file:

@implementation AppDelegate

@synthesize downloadResponse    = _downloadResponse;
@synthesize bytesReceived       = _bytesReceived;

//.... the rest..

- (IBAction)startProcess:(id)sender {

    // some code here..
    [self startDownloadingURL];
}

// start below with the Apple code available here: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLDownload.html

- (void)startDownloadingURL /*:sender*/
{
    // Create the request.
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://freefr.dl.sourceforge.net/project/hpc/hpc/g95/gfortran-4.9-bin.tar.gz"]
                                                cachePolicy:NSURLRequestUseProtocolCachePolicy
                                            timeoutInterval:30.0];

    // Create the download with the request and start loading the data.
    NSURLDownload  *theDownload = [[NSURLDownload alloc] initWithRequest:theRequest delegate:self];
    if (!theDownload) {
        // Inform the user that the download failed.
        NSLog(@"Download NOT started");
    } else {
        NSLog(@"Download started");
    }
}

- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename
{
    NSString *destinationFilename;

    destinationFilename = [[[_homeDirectory stringByAppendingPathComponent:@"Desktop"] stringByAppendingPathComponent:@"DOWN"] stringByAppendingPathComponent:filename];
    [download setDestination:destinationFilename allowOverwrite:YES];
}


- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
{
    // Dispose of any references to the download object
    // that your app might keep.


    // Inform the user.
    NSLog(@"Download failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)downloadDidFinish:(NSURLDownload *)download
{
    // Dispose of any references to the download object
    // that your app might keep.


    // Do something with the data.
    NSLog(@"%@",@"downloadDidFinish");
}

- (void)setDownloadResponse:(NSURLResponse *)aDownloadResponse
{
    NSLog(@"aDownloadResponse - %@",aDownloadResponse);
    downloadResponse = aDownloadResponse;
    NSLog(@"downloadResponse - %@",downloadResponse);
}

- (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response
{
    // Reset the progress, this might be called multiple times.
    // bytesReceived is an instance variable defined elsewhere.
    bytesReceived = 0;

    // Store the response to use later.
    [self setDownloadResponse:response];
}

- (void)download:(NSURLDownload *)download didReceiveDataOfLength:(unsigned long)length
{
    long long expectedLength = [downloadResponse expectedContentLength];

    bytesReceived = bytesReceived + length;

    if (expectedLength != NSURLResponseUnknownLength) {
        // If the expected content length is
        // available, display percent complete.
        float percentComplete = (bytesReceived/(float)expectedLength)*100.0;
        NSLog(@"Percent complete - %f",percentComplete);
    } else {
        // If the expected content length is
        // unknown, just log the progress.
        NSLog(@"Bytes received - %lld",bytesReceived);
    }
}

似乎一切正常,但下载速度真的很慢。在 Safari 中尝试链接,一切都非常快。 我得到的印象是计算进度的部分代码(我需要进度指示器)与减速有关。 有谁知道如何解决速度问题?

【问题讨论】:

  • 糟糕:令人惊讶的是,上面的代码没有问题,因为有些链接是自重定向的。使用正确的链接(重定向后)一切都非常快。现在的问题是如何处理重定向、备忘录和不幸的......也许现在我的问题应该更新

标签: objective-c macos cocoa nsurldownload


【解决方案1】:

经过无数次尝试,由于之前一切正常,所以添加了这两条简单的行:

NSString* userAgent = @"user";
[theRequest addValue:userAgent forHTTPHeaderField:@"User-Agent"];

他们以惊人的速度加快了下载速度。也许还需要添加更多的东西,但现在真的很令人满意。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多