Advertisement
arkader

Save file to Documents directory

Sep 4th, 2014
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void)download:(NSDictionary *)json
  2. {
  3.     //NSLog(@"download %@", json);
  4.     // {action: "download", url: "http://demo.tbwa-paris.com/test/file.json", file: "/json/file.json"}
  5.    
  6.    
  7.     //*
  8.     // 1
  9.     NSString *urlToDownload = @"http://purprod.fr/assets/js/slider.json";
  10.    
  11.     NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  12.     AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
  13.    
  14.     NSURL *URL = [NSURL URLWithString:urlToDownload];
  15.     NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  16.    
  17.     NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
  18.        
  19.         NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
  20.        
  21.  
  22.        
  23.         //NSURL *documentsDirectoryURL  = [NSURL URLWithString:@"/var/mobile/Applications/271DB222-E103-4C8A-904A-BE0C4C682BBD/NinaCrm.app/dist"];
  24.        
  25.         return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
  26.     } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
  27.  
  28.         NSLog(@"File downloaded to: %@", [filePath absoluteString]);
  29.        
  30.  
  31.        
  32.         [self callback:json data:@{@"url": [filePath absoluteString]}];
  33.        
  34.        
  35.         NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  36.         NSString  *documentsDirectory = [paths objectAtIndex:0];
  37.         [self listFileAtPath:documentsDirectory];
  38.  
  39.     }];
  40.     [downloadTask resume];
  41.    
  42.      //*/
  43.    
  44.     //    /var/mobile/Applications/271DB222-E103-4C8A-904A-BE0C4C682BBD/Documents/meteo.json
  45.     //    /var/mobile/Applications/271DB222-E103-4C8A-904A-BE0C4C682BBD/NinaCrm.app//dist/
  46.    
  47.     //*
  48.     //download the file in a seperate thread.
  49.     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  50.         NSLog(@"Downloading Started");
  51.         NSString *urlToDownload = @"http://api.openweathermap.org/data/2.5/weather?q=London,uk";
  52.         NSURL  *url = [NSURL URLWithString:urlToDownload];
  53.         NSData *urlData = [NSData dataWithContentsOfURL:url];
  54.        
  55.         if ( urlData )
  56.         {
  57.             NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  58.             NSString  *documentsDirectory = [paths objectAtIndex:0];
  59.             NSLog(@"documentsDirectory : %@", documentsDirectory);
  60.      
  61.             NSString  *filePath = [NSString stringWithFormat:@"/var/mobile/Applications/271DB222-E103-4C8A-904A-BE0C4C682BBD/Documents/new.json"];
  62.      
  63.             //saving is done on main thread
  64.             dispatch_async(dispatch_get_main_queue(), ^{
  65.                 [urlData writeToFile:filePath atomically:YES];
  66.                 NSLog(@"File Saved to filePath : %@", filePath);
  67.  
  68.                
  69.  
  70.                
  71.                
  72.                 [self callback:json data:@{@"url": filePath}];
  73.             });
  74.         }
  75.        
  76.     });
  77.     // */
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement