Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Reading a .plist file that's hosted online

jordan.clark1993jordan.clark1993 Posts: 12Registered Users
Hi there, I've followed a tutorial for creating a UITableView and reading a plist file. The tutorial is here: [iPhoneDev central]; Blog Archive Hello UITableView!

Now what I am looking to do, is be able to host the .plist online, and change it when I need to. Here is the one from their tutorial: http://www.iphonedevcentral.com/wp-content/uploads/2009/07/TestData.plist

I'm sure it's just a couple of lines of code need to be changed, but I've tried several things and can't figure it out! Thanks to anyone that can help!

- Jordan
Post edited by jordan.clark1993 on

Replies

  • McCaffersMcCaffers Posts: 48Registered Users
    Downloading an online file is simple, changing it will require a bit of effort. You will need FTP access to the server to override the file.

    I would recommend checking out Apple's SimpleFTPSample. You just need the upload functions as your already accessing the plist through a HTTPRequest(?). You can get the sample here: SimpleFTPSample

    This would just be a single file you are downloading and uploading. Multiple users would be accessing the same file from (For example)http://www.iphonedevcentral.com/wp-content/uploads/2009/07/TestData.plist and modifying it. Is this your intention?
  • jordan.clark1993jordan.clark1993 Posts: 12Registered Users
    McCaffers;441487 said:
    Downloading an online file is simple, changing it will require a bit of effort. You will need FTP access to the server to override the file.

    I would recommend checking out Apple's SimpleFTPSample. You just need the upload functions as your already accessing the plist through a HTTPRequest(?). You can get the sample here: SimpleFTPSample

    This would just be a single file you are downloading and uploading. Multiple users would be accessing the same file from (For example)http://www.iphonedevcentral.com/wp-content/uploads/2009/07/TestData.plist and modifying it. Is this your intention?
    Well this is just an example. I just want to know how to change the code in the Xcode project to be able to access a file online. I have my own web server and what not so I know all about FTP and what not. My only problem is changing the code from accessing a local plist, to accessing it from a URL such as the one for TestData.plist.

    Thanks for the help!
  • McCaffersMcCaffers Posts: 48Registered Users
    jordan.clark1993;441488 said:
    Well this is just an example. I just want to know how to change the code in the Xcode project to be able to access a file online. I have my own web server and what not so I know all about FTP and what not. My only problem is changing the code from accessing a local plist, to accessing it from a URL such as the one for TestData.plist.

    Thanks for the help!
    Okie dokie, well your going to want to look at NSURLConnection. It allows you to download the contents of a URL.

    There are plenty examples from Apple on Using NSURLConnection
  • jordan.clark1993jordan.clark1993 Posts: 12Registered Users
    McCaffers;441491 said:
    Okie dokie, well your going to want to look at NSURLConnection. It allows you to download the contents of a URL.

    There are plenty examples from Apple on Using NSURLConnection
    Thanks I know of NSURLConnection, but I can't seem to get it working in this instance for some reason. I was hoping someone could help by showing the actual code I should change to.
  • Duncan CDuncan C Posts: 8,014Tutorial Authors, Registered Users
    jordan.clark1993;441496 said:
    Thanks I know of NSURLConnection, but I can't seem to get it working in this instance for some reason. I was hoping someone could help by showing the actual code I should change to.
    So post the relevant section along with the changes you've made, and indicate what doesn't work.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • jordan.clark1993jordan.clark1993 Posts: 12Registered Users
    Duncan C;441506 said:
    So post the relevant section along with the changes you've made, and indicate what doesn't work.
    Well this is how I read a txt file online:

    NSError *error = nil;
    NSURL *myurl = [NSURL URLWithString:@\"http://myserver.com/file.txt\"];
    NSString *mystring = [NSString stringWithContentsOfURL:myurl encoding:NSUTF8StringEncoding error:&error];
    NSArray *allLines = [mystring componentsSeparatedByString: @\"\n\"];
    NSString *lineNumber0 = [allLines objectAtIndex:0];


    So I tried something similar here, but I didn't think it would work:

    NSError *error = nil;
    NSURL *myurl = [NSURL URLWithString:@\"http://myserver.com/file.plist\"];
    NSString *mystring = [NSString stringWithContentsOfURL:myurl encoding:NSUTF8StringEncoding error:&error];
    NSArray *allLines = [mystring componentsSeparatedByString: @\"\n\"];
    libraryContent = allLines;


    This was in the DvdLibraryDao.m. Sorry for being such a newbie about this. I just don't know a whole lot.
  • jordan.clark1993jordan.clark1993 Posts: 12Registered Users
    Urgh I've been trying different stuff all day, and can't figure it out! I'm frustrated with myself haha. No but seriously I am a very beginner coder and thought I was starting to understand Objective-C a little better, but now just got more confused.
  • McCaffersMcCaffers Posts: 48Registered Users
    jordan.clark1993;441659 said:
    Urgh I've been trying different stuff all day, and can't figure it out! I'm frustrated with myself haha. No but seriously I am a very beginner coder and thought I was starting to understand Objective-C a little better, but now just got more confused.
    Okay well we need more information to help.

    The only difference between the two code snippets you've posted is the last line.

    NSString *lineNumber0 = [allLines objectAtIndex:0];

    v.s.
    libraryContent = allLines;


    I see you are just passing the array onto libraryContent, but connection wise its the same, which works. So are you getting a specific error or the UITableView is empty?
  • jordan.clark1993jordan.clark1993 Posts: 12Registered Users
    McCaffers;441675 said:
    Okay well we need more information to help.

    The only difference between the two code snippets you've posted is the last line.

    NSString *lineNumber0 = [allLines objectAtIndex:0];

    v.s.
    libraryContent = allLines;


    I see you are just passing the array onto libraryContent, but connection wise its the same, which works. So are you getting a specific error or the UITableView is empty?
    Well I believe I have found out two issues I'm having but unfortunately don't know how to solve them.

    The first is that if you check out the TestData.plist that's hosted here, you'll see that this line of code would not work for it:


    NSArray *allLines = [mystring componentsSeparatedByString: @\"\n\"];


    So I need to find a way to create the plist as an NSArray without using componentsSeperatedByString. I'm not entirely sure how to do this.

    The second problem is that in the original code you see here:

    libraryContent = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:libraryPlist ofType:@\"plist\"]];


    I'm replacing it with this:

    NSError *error = nil;
    NSURL *myurl = [NSURL URLWithString:@\"http://evansfireprotection.ca/TestData.plist\"];
    NSString *mystring = [NSString stringWithContentsOfURL:myurl encoding:NSUTF8StringEncoding error:&error];
    NSArray *allLines = [mystring componentsSeparatedByString: @\"\n\"];
    libraryContent = allLines;


    So just by glance it doesn't seem like this would work. I think I'm making it harder than it needs to be. This may seem like a big request, but I was wondering if one of you could download the sample code from the site here, and try and get it to work. Sorry again for this. Like I said I'm making it a lot more confusing than it needs to be, and one of you will probably be able to figure it out in literally 2-3 minutes.

    Thanks again, Jordan
  • jordan.clark1993jordan.clark1993 Posts: 12Registered Users
    bollcosett;441762 said:
    I'm frustrated with myself haha.
    Well I am??? :P
  • Duncan CDuncan C Posts: 8,014Tutorial Authors, Registered Users

    Duncan C;441506 said:
    So post the relevant section along with the changes you've made, and indicate what doesn't work.
    Well this is how I read a txt file online:

    NSError *error = nil;
    NSURL *myurl = [NSURL URLWithString:@\"http://myserver.com/file.txt\"];
    NSString *mystring = [NSString stringWithContentsOfURL:myurl encoding:NSUTF8StringEncoding error:&error];
    NSArray *allLines = [mystring componentsSeparatedByString: @\"\n\"];
    NSString *lineNumber0 = [allLines objectAtIndex:0];


    So I tried something similar here, but I didn't think it would work:

    NSError *error = nil;
    NSURL *myurl = [NSURL URLWithString:@\"http://myserver.com/file.plist\"];
    NSString *mystring = [NSString stringWithContentsOfURL:myurl encoding:NSUTF8StringEncoding error:&error];
    NSArray *allLines = [mystring componentsSeparatedByString: @\"\n\"];
    libraryContent = allLines;


    This was in the DvdLibraryDao.m. Sorry for being such a newbie about this. I just don't know a whole lot.
    Your first block of code for parsing a text file is fine. The second block of code for treating the data as a plist is completely wrong.

    You should read the data as NSData, not a string. It would be trivially simple to replace your call to initWithContentsOfFile to initWithConentsOfURL:
    NSError *error = nil;
    NSURL *myurl = [NSURL URLWithString:@\"http://evansfireprotection.ca/TestData.plist\"];
    libraryContent = [[NSArray alloc] initWithContentsOfURL: myurl];
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
Sign In or Register to comment.