2012-08-29 20 views
6

मुझे अपने आईपैड ऐप के साथ बड़ी .zip फ़ाइलें (800 एमबी तक) डाउनलोड करने की आवश्यकता है। यदि यह रद्द किया गया है या यदि ऐप पृष्ठभूमि में है तो मैं फिर से डाउनलोड फिर से शुरू करना चाहता हूं।AFNetworking + रोकें/बड़ी फ़ाइलों को डाउनलोड करना फिर से शुरू करें

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? 

} 

आपकी सहायता के लिए धन्यवाद।

उत्तर

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]; 

अपने ऐप को पुनरारंभ करने के बाद, और उसी यूआरएल के अनुरोध को उत्पन्न करने के बाद, यह डाउनलोड फिर से शुरू हो जाएगा। "कंधे: हाँ" काम करता है

+1

क्या कोई गैर एआरसी विकल्प है? – Tudorizer

+0

क्या आप जानते हैं कि यह यूआरएल रीडायरेक्शन के साथ काम करता है? मैंने यूआरएल को निर्धारित किया है जो फिर डाउनलोड करने के लिए वास्तविक फ़ाइल पर रीडायरेक्ट करता है। इन यूआरएल का उपयोग करके डाउनलोड फिर से शुरू नहीं हो रहे हैं। –

+0

बस यह सुनिश्चित करें कि आप हर बार एक ही लक्ष्यपैथ निर्दिष्ट करते हैं। इसके अलावा यह निर्देशिका नहीं हो सकता है; यदि आप एक निर्देशिका निर्दिष्ट करते हैं, तो यह फ़ाइल का नाम देने के लिए यूआरएल का उपयोग करेगा। – mrgrieves