Search code examples
xmlparsingdownloaddocuments

iPhone - How to download xml file in Documents directory


i've to parse an xml file, but at first i have to download it and store in my /Documents directory. How can i do this?


Solution

  • -(NSString *)downloadXML:(NSString *)path newFileName:(NSString *)newFileName {
    
        //NSLog(@"\nENTRATO NEL METODO DI DOWNLOAD");
        NSURL *URL = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
        NSData *xmlData = [NSData dataWithContentsOfURL:URL];
        if(xmlData == nil) {
            // Error - handle appropriately
        }
        NSString *applicationDocumentsDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *storePath=[applicationDocumentsDir stringByAppendingPathComponent:newFileName];
        [xmlData writeToFile:storePath atomically:TRUE];
    
        //NSLog(@"\nFINITO IL METODO DI DOWNLOAD");
        return storePath;
    
    }