Loading a .plist file from url

2019-04-17 19:59发布

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.

2条回答
等我变得足够好
2楼-- · 2019-04-17 20:40

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]
查看更多
成全新的幸福
3楼-- · 2019-04-17 20:57

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!

查看更多
登录 后发表回答