download data into plist

Discussion in 'iOS Programming' started by MACloop, Mar 25, 2011.

  1. MACloop macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #1
    Hello,
    I have a problem with downloading data and save it as a plist, i.e. in the app's sandbox. I get the data downloaded but cannot save it into the plist...

    This is how I do:
    I use NSURLConnection to download the data. The data is available at an url and has the format like this:
    Code:
    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
      <array>
        <dict>
            <key>date</key>
            <string>a timestamp here</string>
            <key>info</key>
            <string>some info here</string>
        </dict>
      </array>
    </plist>
    
    in the
    Code:
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    i try to serialize the downloaded data and save it into the plist. I use the NSPropertyListSerialization which obviously does not work. The code lookes somthing like this:
    Code:
    NSFileManager *fileManager = [[NSFileManager alloc]init];
    NSString *error;//apple documentation says this may be nil
    NSPropertyListFormat format;//apple documentation says this may be nil
    NSPropertyListSerialization *p  = [NSPropertyListSerialization propertyListFromData:[[self.connectionToData objectForKey:connectionKey] objectForKey:@"data"]
                         mutabilityOption:NSPropertyListImmutable
                         format:&format
                         errorDescription:&error];
    	
    BOOL create_plist_success = [fileManager createFileAtPath:[[self.connectionToData objectForKey:connectionKey]objectForKey:@"current_plist_path"] contents:p attributes:nil];
    if(create_plist_success == NO) {//could not create file
    	//fire error method here
    }
    else {
    	//fire method in delegate. The path to the currently created plist is sent to the delegate which may use the file
    }
    
    Is this not the right way to do this?
    The data is just data and not a plist file - is that a problem?

    Thanks in advance!
    MACloop
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    When you say it doesn't work could you expand? What, exactly, was the result?
     
  3. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #3
    sorry - I forgot that ;-)
    Ok - the fiel exists on the path but does not contain anything.... I did test prints with both the data and the plist and the data (converted to a string) contains the correct information, and the plist i empty. From the code above I tried to print out the description for the object p, like this:
    NSLog(@"p description "%@, [p description]);
    and it returns p description (null)

    MACloop
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    If you can turn the plist into a string to print it why don't you just write that string to a file?
     
  5. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #5
    Well, I can turn the data which I have downloaded... I thought I have to define somehow a file to be a plist file... Perhaps I am making it all complicated? I will try to take the data, convert it to a string and then write it to a plist. I need the plist in my app as I in the class get the data from a plist, saved/downloaded at app start.... like this:
    Code:
    NSString *path = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"plist_info"] objectForKey:name];
    NSArray *content = [[NSArray alloc]initWithContentsOfFile:path];
    Thanks for your help!
    MACloop
     
  6. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #6
    ok I have tried to use a string, but it did not work, because I need the plist later on in the app. I have to, somehow save the downloded data into a plist and as I can see it must be possible. Perhaps is the data I am retrieving false formatted? It is a simple php site, sending the textdata... no file, nothing fancy at all...

    MACloop
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    When you print the plist string you have downloaded what do you get? If you copy/paste that into TextEdit (plain text mode) and save as a .plist file can you open it with the plist editor?
     
  8. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #8
    Thanks for the answer - I will try that!
    MACloop
     
  9. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #9
    ups - you where right - it did not work. It seem to be somthing wrong with the data i get. I have to analyze that a bit and talk to the developer who is sending me the data. Thanks for the help, again :)
    MACloop
     
  10. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #10
    Hello again,
    so I have talked to the web developer and the problem seems to be some html-tags send with the data and the also CDATA elements. I did as you suggested and removed one after antoher from those elements and the file was not "accepted" as a plist before all those html parts were removed.

    So my question now (which might be a bit off topic):
    The web developer did not have a good solution for this, i.e. he said one way might be to trim the data and remove those tags, which is not a robuste solution. So, does anyone have any idea about how to do this? Is there a nice php script or somthing like that, creating valid plists? Or should I go for xml-parsing instead?

    Any ideas?
    Thanks in advance,
    MACloop
     
  11. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #11

    Depends on how big the file is, what's important and what's not. I would also look into NSScanner once set it works very well, however read the documentation or you will become frustrated quickly.

    Remember you can always setup a timer and compare different methods or use instruments \ activity monitor to watch memory usage if that's a concern.
     
  12. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #12
    Thanks for the answer! I have used NSScanner in my parser classes before and know somehow how it works. So I could let the data run through the scanner and after that I could make a plist out of the data. I don't know if that is what you mean? I guess I have to define what tags are valid and remove all tags not being in the collection of valid tags.... or something like that... What I do not like with a solution with that is that I do not have any control over the data sent to the app. So if the web developer or the customer makes any structure changes, the app will not be able to read the data anymore...

    Again, thanks for your help - I will try something like that to see how it works!
    MACloop
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    If there is data in the XML that is not part of the structure of the XML, that data should be "escaped". So, for example:
    Code:
    <xmlTag><div>asdf</div></xmlTag>
    could become:
    Code:
    <xmlTag><div>asdf</div></xmlTag>
     
  14. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #14
    Thanks for the answer! Ok, that is interesting! I guess it has to be done in the web programming part and sent into the app with those escaped tags?
    Thanks for the help!
    MACloop
     
  15. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #15
    So I tried it out on some dummy plist data and it work, almost as I want it to work ;-)
    The parsing is working but those tags are printed out with the text... I found a solution where I can use a NSScanner to remove the f.i. <div> tag from a text. Is this the right way to go? link
    MACLoop
     
  16. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    What do you mean "printed out with the text"? How is the text printed out? UILabel? UIWebView? So, you have HTML-snippets that are part of the data and you want to ignore the HTML-tagging? If so, why is the HTML-tagging being sent with the XML at all? Maybe elaborate on what kind of data you are retrieving and what you are needing to do with it.
     
  17. Genocyde macrumors member

    Joined:
    Mar 12, 2009
    Location:
    Houston, TX
    #17
  18. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #18
    Thansk a lot! That's great information. I will give the info to him and try it out!
    MACloop
     

Share This Page