我已从Apple网站实施了以下方法,可在此页面上找到:
//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 :(得分:0)
经过无数次的尝试,由于之前的一切看起来都很好,所以添加了这两条简单的线条:
NSString* userAgent = @"user";
[theRequest addValue:userAgent forHTTPHeaderField:@"User-Agent"];
他们以极快的速度加快了下载速度。也许还需要添加更多东西,但现在它确实令人满意。