NSCoding/NSUserDefaults

Discussion in 'iOS Programming' started by nashyo, Jun 22, 2012.

  1. nashyo macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    I was data to persist when a user is filling out a form. However, I want that data erased from temporary storage when the form is completed.

    Would it be a good idea to use NSUserDefaults when the user is filling out the form (in the event the App terminates/pauses), and NSCoding when the form is submitted and the data is sent to local storage?

    Any advice would be great.
     
  2. Mike73 macrumors newbie

    Joined:
    Jun 23, 2012
    #2
    I'm using NSUserDefaults for my data entry forms of my app "Gas By Numbers".
    The distance driven, the gasamount refilled and the gasprice is being entered on 2 different UIViews. On the 3rd View, the data gets calculated and displayed. If the user decides to save the data, it's being written to a local file which is not visible via iTunes sharing. If the user decides to make the data available for use on a PC or Mac, all the data is being written to a new file in the documents folder on the iDevice and it's shared via iTunes.

    Hope this helps, Mike.
     
  3. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #3
    Thanks Mike.

    I didn't realise that storing data in the documents folder meant that it appeared in iTunes sharing.

    When creating a plist file I look for the doc directory like so.

    Code:
    -(NSString*)documentDirectory
    {
        NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        return [path objectAtIndex:0];
    }
    
    -(NSString*)path
    {
        return [[self documentDirectory] stringByAppendingPathComponent:@"Options.plist"];
    }
    I then call 'path'.

    How do you store data in a different directory to the one I have mentioned here. For instance, something other than the documents folder.
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    It won't show up in iTunes file sharing unless you have iTunes file sharing set to yes. Also, files that you don't want to be backupped to iCloud you should set to "not backupped", otherwise your app will also get rejected (happened to me).
    for 1 app, we write everything to plist, with NSCoding, instead of NSUserDefaults, also Base64 encrypted, because you can drag plists from the device with iExplorer, even if it's not non-jailbroken phone. so better to be a bit safe with your users stuff ^_-
     
  5. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #5
    thanks
    By conforming to NSCoding protocols, are you automatically encrypting with Base64? If no, then how to you go about encrypting a plist with that?
     
  6. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #6
    Nope, Base64 is not standard in iOS, it is for Mac OSX though..
    You basically need to find a library online for Base64, include it in your project.
    Mostly it's just a category on NSString which makes you do [stringPointer encryptBase64]; which then encrypts it, and then you can store that to your plist :)
     

Share This Page