2016-02-01 2 views
8

मेरी परियोजना में, मुझे एक छोटा वीडियो डाउनलोड करने की आवश्यकता है। पिछले संस्करण में, मैं इसका उपयोग करता हूं:फ़ाइलों को डाउनलोड करने और AFNetworking 3.0 का उपयोग करके स्थानीय में कैसे सहेजते हैं?

- (void)downloadFileURL:(NSString *)aUrl savePath:(NSString *)aSavePath fileName:(NSString *)aFileName tag:(NSInteger)aTag; 

मैं AFNetworking 3.0 में यह कैसे कर सकता हूं?

उत्तर

12

इस कोड:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { 
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; 
    return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]; 
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { 
    NSLog(@"File downloaded to: %@", filePath); 
}]; 
[downloadTask resume]; 

परियोजना के लिए README पर है: https://github.com/AFNetworking/AFNetworking

3

मुझे पता मूल प्रश्न Obj सी में है, लेकिन यह किसी के लिए भी तो एक गूगल खोज में आता है, बाकी इस पर ठोकर और और @Lou फ्रेंको के जवाब की स्विफ्ट संस्करण की आवश्यकता होगी, ये रहा:

let configuration = URLSessionConfiguration.default 
let manager = AFURLSessionManager(sessionConfiguration: configuration) 
let url = URL(string: "http://example.com/download.zip")! // TODO: Don't just force unwrap, handle nil case 
let request = URLRequest(url: url) 

let downloadTask = manager.downloadTask(
    with: request, 
    progress: { (progress: Progress) in 
        print("Downloading... progress: \(String(describing: progress))") 
       }, 

    destination: { (targetPath: URL, response: URLResponse) -> URL in 
        // TODO: Don't just force try, add a `catch` block 
        let documentsDirectoryURL = try! FileManager.default.url(for: .documentDirectory, 
                      in: .userDomainMask, 
                      appropriateFor: nil, 
                      create: false) 

        return documentsDirectoryURL.appendingPathComponent(response.suggestedFilename!) // TODO: Don't just force unwrap, handle nil case 
       }, 

    completionHandler: { (response: URLResponse, filePath: URL?, error: Error?) in 
          print("File downloaded to \(String(describing: filePath))") 
         } 
) 

downloadTask.resume() 

यहाँ नोटों की युगल:

012,
  • यह स्विफ्ट 3/स्विफ्ट 4
  • है मैं भी (सिर्फ एक print बयान) एक progress बंद गयी। लेकिन निश्चित रूप से मूल उदाहरण के रूप में nil पास करना बिल्कुल ठीक है।
  • तीन स्थान हैं (TODO: के साथ चिह्नित) कोई त्रुटि नहीं है जहां चीजें असफल हो सकती हैं। जाहिर है कि आपको केवल दुर्घटनाग्रस्त होने की बजाय इन त्रुटियों को संभालना चाहिए।
+0

धन्यवाद। यह पूरी तरह से काम करता है –

+0

बहुत बहुत धन्यवाद, स्विफ्ट के साथ ठीक काम करता है - 4 भी –

संबंधित मुद्दे

 संबंधित मुद्दे