2014-04-21 26 views
0

我已經實現了從蘋果網站下面的方法,此頁面上提供:緩慢的下載與蘋果的示例代碼+進步

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中的鏈接,一切都非常快。 我得到的印象是計算進度(我需要進度指示器)的部分代碼與減速有關。 有誰知道如何解決速度問題?

+0

糟糕:令人驚訝的是,上面的代碼是可以的,因爲有些鏈接是自重定向的。使用正確的鏈接(重定向後),一切都非常快。現在的問題是如何處理重定向,備忘錄和不幸..也許現在我的問題應該更新 – Mike97

回答

0

無數次嘗試後,因爲一切都確定之前看了一下,這兩個簡單的線條補充說:

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

他們加快下載在一個真正令人驚訝。也許還需要添加更多的東西,但現在它確實令人滿意。