views:

288

answers:

2

I have a database online that I would like to be able to load into an NSArray in my app. I can use arrayWithContentsOfURL with a static file, but I really need to go to a url that generates a plist file from the database and loading it into the array. I can use ASP or PHP. I tried setting the response type to "text/xml", but that doesn't help.

Any thoughts?

A: 

Apple's plist format is not just any xml schema. You have to generate a valid plist file on your server.

Use for example CFPropertyList.

hop
A: 

the best thing is using json. well formatted, very short code, lightweight, fast and easy to use.

for example generate your array with php, what you want to get in your obj-c code.

$arr = array("foo" => "bar", "foobar" => "barfoo");

then encode your array to a json object and echo

echo '{"myarray":'.json_encode($arr).'}';

copy the json framework to your app and just use:

NSString *resultString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://foo.bar/yourfile.php"]];
NSDictionary *resultJson =  [resultString JSONValue];
NSArray *resultSet = [resultJson objectForKey:@"comments"];

now you got exactly the same array in your iphone code, as in your php file.

//edit:

you can use NSURLRequest for better performance and other cool things. check it out.

choise
The only difference between json and original plist is the delimiters, so you could just make a plist and skip the json framework.
drawnonward
but a plist needs much more traffic then a json object.
choise