Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)download:(NSDictionary *)json
- {
- //NSLog(@"download %@", json);
- // {action: "download", url: "http://demo.tbwa-paris.com/test/file.json", file: "/json/file.json"}
- //*
- // 1
- NSString *urlToDownload = @"http://purprod.fr/assets/js/slider.json";
- NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
- AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
- NSURL *URL = [NSURL URLWithString:urlToDownload];
- 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];
- //NSURL *documentsDirectoryURL = [NSURL URLWithString:@"/var/mobile/Applications/271DB222-E103-4C8A-904A-BE0C4C682BBD/NinaCrm.app/dist"];
- return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
- } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
- NSLog(@"File downloaded to: %@", [filePath absoluteString]);
- [self callback:json data:@{@"url": [filePath absoluteString]}];
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- [self listFileAtPath:documentsDirectory];
- }];
- [downloadTask resume];
- //*/
- // /var/mobile/Applications/271DB222-E103-4C8A-904A-BE0C4C682BBD/Documents/meteo.json
- // /var/mobile/Applications/271DB222-E103-4C8A-904A-BE0C4C682BBD/NinaCrm.app//dist/
- //*
- //download the file in a seperate thread.
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- NSLog(@"Downloading Started");
- NSString *urlToDownload = @"http://api.openweathermap.org/data/2.5/weather?q=London,uk";
- NSURL *url = [NSURL URLWithString:urlToDownload];
- NSData *urlData = [NSData dataWithContentsOfURL:url];
- if ( urlData )
- {
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSLog(@"documentsDirectory : %@", documentsDirectory);
- NSString *filePath = [NSString stringWithFormat:@"/var/mobile/Applications/271DB222-E103-4C8A-904A-BE0C4C682BBD/Documents/new.json"];
- //saving is done on main thread
- dispatch_async(dispatch_get_main_queue(), ^{
- [urlData writeToFile:filePath atomically:YES];
- NSLog(@"File Saved to filePath : %@", filePath);
- [self callback:json data:@{@"url": filePath}];
- });
- }
- });
- // */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement