Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MythicFrost

macrumors 68040
Original poster
Mar 11, 2009
3,944
40
Australia
Alright, I want to write the username that is input into the text box onto the disk so it's saved.
I've found this function at the iPhone Dev Center
Code:
- (BOOL)writeApplicationPlist:(id)plist toFile:(NSString *)fileName {
    NSString *error;
    NSData *pData = [NSPropertyListSerialization dataFromPropertyList:plist format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error];
    if (!pData) {
        NSLog(@"%@", error);
        return NO;
    }
    return ([self writeApplicationData:pData toFile:(NSString *)fileName]);
}

But I am unsure what to pass as the first parameter? this converts the plist to a data form to be saved, but what can I do to make my plist programatically?

I've already got the path stored in the variable sSettingsPath where I will write the data to.

I've also found this function, which would be better?
I want to store the username, and then other ints containing details about the character the user will be using in the game.

Other function:
Code:
- (BOOL)writeApplicationData:(NSData *)data toFile:(NSString *)fileName {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    if (!documentsDirectory) {
        NSLog(@"Documents directory not found!");
        return NO;
    }
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
    return ([data writeToFile:appFile atomically:YES]);
}

Any idea's? Thanks.
 
That's way, way overcomplicated for writing a single username which is basically a user preference. Use NSUserDefaults

Set:
Code:
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:myUserNameNSString forKey:@"username];
[def synchronize]

Retrieve:
Code:
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSString *myUserNameNSString = [def stringForKey:@"username"];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.