6

iPad アプリで大きな .zip ファイル (最大 800 MB) をダウンロードする必要があります。ダウンロードがキャンセルされた場合やアプリがバックグラウンドになっている場合は、もう一度ダウンロードを再開したい。

operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:YES];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){

// unzip the .zip


}failure:^(AFHTTPRequestOperation *operation, NSError *error){

//Handle the error

}];


[operation setDownloadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten,long long totalBytesExpectedToWrite) {

//update the progressView

}];

[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{

// What I have to do here?

}];

[operation start];
[operation waitUntilFinished];


-(void)applicationWillResignActive:(UIApplication *)application{

// What I have to do here?

}

ご協力いただきありがとうございます。

4

1 に答える 1

23

これを行うには、AFDownloadRequestOperationを使用できます。

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"....zip"]];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"....zip"];
    AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully downloaded file to %@", path);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    [operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
        NSLog(@"Operation%i: bytesRead: %d", 1, bytesRead);
        NSLog(@"Operation%i: totalBytesRead: %lld", 1, totalBytesRead);
        NSLog(@"Operation%i: totalBytesExpected: %lld", 1, totalBytesExpected);
        NSLog(@"Operation%i: totalBytesReadForFile: %lld", 1, totalBytesReadForFile);
        NSLog(@"Operation%i: totalBytesExpectedToReadForFile: %lld", 1, totalBytesExpectedToReadForFile);
    }];
    [operations addObject:operation];

アプリを再起動し、同じURLのリクエストを生成すると、ダウンロードが再開されます。「shouldResume:YES」は機能します

于 2012-08-31T05:53:21.793 に答える