CodeMoto
CodeMoto

Reputation: 353

Getting XML File from Site to Phone

Ok. So I have my first ever iPhone app prototyped and it will parse a given XML file. This file is in with all of the other files that make up the app for testing purposes but eventually I want the app to pull the XML data from a web service.

So I've written a PHP script that reads data and creates an XML file for me. But how do I go about getting the XML file from the web server to the phone?

This is what I have:

  1. An XML file accessible via URL - i.e. //localhost/v/p.xml
  2. An iOS 6 app that can parse an XML file which is currently loaded in the app (put under the Supporting Files folder in my XCode project).

So what's the best way to get the XML file to the phone? Is LazyTableImages something I should be looking at?

Should I do this:

NSURL *url = [[NSURL alloc] initFileURLWithPath:@"http://localhost/v/p.xml"];
NSData *xmlData = [[NSData alloc] initWithContentsOfURL:url];

//NSString *path = [[NSBundle mainBundle] pathForResource:@"profileXML" ofType:@"xml"];
//NSData *xmlData = [[NSData alloc] initWithContentsOfFile:path];

Where to top 2 lines replace the commented out line? Is it that simple?

Upvotes: 3

Views: 5721

Answers (2)

Tricertops
Tricertops

Reputation: 8512

Your example should work as expected (1), but will cause the app to freeze for a while on second line. That method downloads the file synchronously and this is usually not desired (unless you run it on background queue).

I would use +[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]. This is the easiest asynchronous way to download something.

Example:

NSURL *url = [NSURL URLWithString:@"http://localhost/v/p.xml"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                                              // Do something with the `data` unless you have `error`.
                                          }];

(1) I'm just not sure about the way you construct NSURL. It says initFileURLWithPath, but you are definitely not passing path there.

Upvotes: 0

woz
woz

Reputation: 11004

You can use stringWithContentsOfURL:encoding:error:, documented here:

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

For example:

[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://localhost/v/p.xml"] encoding:NSUTF8StringEncoding error:nil];

Update:

As Jim says, NSURLConnection is non-blocking, and it allows you to submit credential if you want to protect your file from public access. If you want to use that, it looks like this:

NSString *authenticationURL = @"http://foo.bar";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:authenticationURL]];
NSString *escUsername = [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *escPassword = [password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:[[NSString stringWithFormat:@"username=%@&password=%@", escUsername, escPassword] dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

#pragma mark Url connection delegate methods

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSString *responseText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", responseText);  
}

Upvotes: 2

Related Questions