开发者

Load array from string on iPhone

开发者 https://www.devze.com 2023-03-28 09:45 出处:网络
I have a PHP file on my website which outputs a list of names, one per line. I want to load these items into a tableview, but can\'t get it to work.

I have a PHP file on my website which outputs a list of names, one per line. I want to load these items into a tableview, but can't get it to work.

I have tried:

NSURL *urlVersion = [NSURL URLWithString: @"http://mywebsite.com/users.php"];
NSString *webVersion = [NSString stringWithContentsOfURL:urlVersion encoding:1 error:NULL];
NSString *contents = [NSString stringWithContentsOfFile:webVersion encoding:NSUTF8StringEncoding error:nil];
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[contents componentsSeparatedByString:@"\n"]];

The file on the server is users.php

Thanks!

EDIT: Got it by getting the whole site as a string, an开发者_运维技巧d then parsing it to separate it by each line.


You must make a request to that script:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://mywebsite.com/users.php"]];

NSURLResponse *response = nil;
NSError *resultError = nil;
NSData *resultsData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &resultError];

NSString *resultString = [[NSString alloc] initWithData:resultsData encoding:NSUTF8StringEncoding];

Then you must parse that string.

0

精彩评论

暂无评论...
验证码 换一张
取 消