Getting an XML file from a URL into an NSData object?

I need to load an XML file from a URL into an NSData object for further analysis with some libraries I already have (but they ask me a .xml file as an NSData), how can I do this?

The format of the URL will be something like this:

http://127.0.0.1/config.xml

+3
source share
2 answers

Assuming this is UTF-8 data. If it is local (i.e. inside the package) something like:

NSError *error;
NSString* contents = [NSString stringWithContentsOfFile:PATHTOLOCALFILE 
                               encoding:NSUTF8StringEncoding
                               error:&error];
NSData* xmlData = [contents dataUsingEncoding:NSUTF8StringEncoding];

, - . , . , - ASIHTTPConnection, .

NSError *error;
NSString* contents = [NSString stringWithContentsOfUrl:[NSURL URLWithString:URLOFXMLFILE] 
                               encoding:NSUTF8StringEncoding
                               error:&error];
NSData* xmlData = [contents dataUsingEncoding:NSUTF8StringEncoding];
+7

Source: https://habr.com/ru/post/1715750/


All Articles