I have successfully created an app that reads from a bundled .plist file and displays the contents in a UITableView. I would like to move that plist to an external site, as it is updated frequently.开发者_如何学C even better, I would love to have it generated with PHP. I have got the server side working, but the Objective C is giving me a headache...
My code used to read like this:
NSString *myfile = [[NSBundle mainBundle] pathForResource:@"notices" ofType:@"plist"];
according to various google searches, my code should now look something like this:
NSString *myfile = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"http://localhost/plistgen1.php"]];
Obviously this is not going to work, mixing NSString with NSDictionary, but I have tried (and failed) to get it to work properly. does anyone have a solution, or a different way to approach the problem? The data I am using is on a mysql server, and the plistgen1.php just "fills in the blanks" in the plist file and echoes it out...
I may have got this all wrong, don't shoot me :)
The first case ("My code used to read like this") gives you the local filesystem path to the bundled plist file and saves it to the variable myfile
. In the second case you're downloading the plist file's contents from the server and creating a dictionary from that.
You probably have some code after the assignment of the file path to myfile
that reads the contents of that file into an NSDictionary
. You need to replace both the assignment to myfile
and that statement with the second example.
So you'd replace something like this:
// determine filesystem path to bundled plist file
NSString *myfile = [[NSBundle mainBundle] pathForResource:@"notices" ofType:@"plist"];
// read contents of that plist file and parse them into a dictionary
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:myfile];
With this:
// download plist file contents from URL and parse them into a dictionary
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"http://localhost/plistgen1.php"]];
精彩评论