Manually copy NSData to a resource file

Discussion in 'iOS Programming' started by scanf, Oct 31, 2010.

  1. scanf macrumors newbie

    Joined:
    Apr 23, 2010
    #1
    i have some application-specific data that i want to encrypt and store in a file (before i've compiled the app - not at runtime) as a resource in my app bundle. the problem is that my encryption algorithm returns an NSData object that looks like <5956b4f7 b8aecea1 633f6178 18368c78> and if i paste that manually into that file and subsequently read it using stringWithContentsOfFile and convert that string back to NSData to decrypt it, it doesn't work.

    bottom line, how can i manually store an NSData object so it can be read later?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  3. scanf thread starter macrumors newbie

    Joined:
    Apr 23, 2010
    #3
    yeah, i tried that and it kept causing this exception:

    -[NSConcreteData writeToFile:atomically:encoding:error:]: unrecognized selector sent to instance
    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData writeToFile:atomically:encoding:error:]: unrecognized selector sent to instance 0x5a1e480'

    Code:
    NSError *error = nil;
    NSString* filePath = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"txt"];
    [encryptedData writeToFile:filePath atomically:NO encoding:NSUTF8StringEncoding error:&error];
    
    i'm also getting strange compiler warnings:
    'NSData' may not respond to '-writeToFile:atomically:encodeing:error:'
    Initialization makes integer from pointer without cast
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    No, you tried to use -writeToFile:atomically:encodeing:error:. This is a NSData method you have invented. Look at the NSData documentation (I linked to it in my previous post). Do you see that method? No, bit you do see -writeToFile:atomically: which is what I suggest you use before and what I suggest you use now.
     
  5. scanf thread starter macrumors newbie

    Joined:
    Apr 23, 2010
    #5
    oops, i hurredly copied that from a different part of my code where i first tried converting the NSData output to a string then writing it.

    anyway, it compiles and runs fine and writeToFile returns YES, but the file is not being written to for some reason. any extra step i need to take to write to the file? i can read from them just fine.

    many thanks for your help and quick responses!
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    If it returns YES then it wrote to the file. Check the path.
     
  7. scanf thread starter macrumors newbie

    Joined:
    Apr 23, 2010
    #7
    ahhhhh, ok - this is strange. here's the path it's writing to:
    /Users/me/Library/Application Support/iPhone Simulator/4.0/Applications/5F1F25E5-35D8-484E-8853-1589F8E1AB3F/testApp.app/temp.txt

    not sure what that location is for as opposed to the app in my build directory. i was expecting to see my project's temp.txt file updated. is that possible?
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Not for an app running in the iPhone simulator, no.
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    You can't write to your app's bundle. It's read-only. Write to the Documents folder or elsewhere inside your app's sandbox.

    However, on the Sim a write to the app's bundle will succeed but on the device it will fail.
     

Share This Page