Pregunta

I have implemented the following methods from the Apple site, available on this page:

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);
    }
}

Everything seems to work, but the download is really slow. Trying the link in Safari, everything is very fast. I get the impression that part of the code which calculates the progress (I will need for the progress indicator), has to do with the slowdown. Does anyone know how to fix speed problems?

¿Fue útil?

Solución

After countless attempts, since everything looked ok before, these two simple lines added:

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

they have speed up the download in a truly surprising. Perhaps even more things needs to be added, but now it's really satisfying.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top