Zer0_byt3
Zer0_byt3

Reputation: 13

Loading a .plist file from url

I have the following code that loads a plist file from the resource folder and uses it to populate a tableview:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"AnimeDB" ofType:@"plist"]; 
    NSMutableArray* tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
    self.dataList1 = tmpArray; 
    [tmpArray release];

But I need to load this .plist file from an url. I tried some solutions that I found on the net, but I couldn't manage to get it working. Does someone know how can i do this?

Maybe there's an easy solution, but I'm at the beginning with xcode (this is my first app), so I can't find it.

Any help is really appreciated!

P.S. Sorry for any mistake that i may have committed, my english is not that good.

Upvotes: 0

Views: 4252

Answers (2)

Zer0_byt3
Zer0_byt3

Reputation: 13

In the end I managed to do what I needed using just this simple piece of code:

NSMutableArray *tmpArray = [[NSMutableArray alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://zer0soft.altervista.org/AnimeDB.plist"]];    
self.dataList1 = tmpArray; 

And it works like a charm! Thanks to everyone who spent time answering my question, in particular thanks to Jaybit!

Upvotes: 1

Jaybit
Jaybit

Reputation: 1864

I suggest looking into using Json. It will allow you to pass plist like structured data from a url to your app.

NSData *dataReturn = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://someUrl.com/test.json"]];

    if (dataReturn) {
        NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:dataReturn options:kNilOptions error:Nil];

If you want to use your plist then its just about the same code:

// This will get the plist into data format
NSData *dataReturn = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://someUrl.com/test.plist"]];

// This will convert data format to array
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:dataReturn]

Upvotes: 5

Related Questions