سؤال

I have two UITextField

  1. email
  2. password

These two field values are stored in a NSMutableDictionary call userData. Now i want to save these two field values in a file, so that the records keep there and i can restore this record to check user login correctly. Here i want to accomplish that, but not working.

My code :

-(void) saveRegistrationData
{
    userData = [[NSMutableDictionary alloc]
                initWithObjects:[[NSMutableArray alloc] initWithObjects:@"123456", nil]
                forKeys:[[NSMutableArray alloc] initWithObjects:@"admin@gmail.com", nil]];

    [userData setObject:passwordTextField.text forKey:emailTextField.text];

    NSString   *savePath = [@"/MediaFiles/Documents/" stringByExpandingTildeInPath];
    [userData writeToFile: savePath atomically: YES];

    //[userData writeToFile:@"MediaFiles/Documents/" atomically:YES];

    for (id key in userData)
    {
        NSLog(@"%@ is for %@", key, [userData objectForKey:key]);
    }
}

I think the path is not setting correctly. If any one similar with the solution, please share with me. Thanks in advanced. Have a good day.

هل كانت مفيدة؟

المحلول

It's not working for a few reasons.

1) You're writing to a folder in the root of the device's filesystem. Apple uses sandboxing to prevent this from happening as you could overwrite and modify any system files.

2) You're writing to a folder rather than a file. To write to a file, you need to specify a filename (and extension). i.e. "/MediaFiles/Documents/dnt_lk_at_dis.plist"

In order to fix these issues, you need to be firstly getting the path to the sandbox (documents directory) and then append the filepath.

NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = @"secret.plist";
NSString *filePath = [docsPath stringByAppendingPathComponent:filename];

Furthermore, I strongly suggest that you instead use the keychain for storing sensitive user information. Anybody with a jailbroken device, or anybody that has access to the file system will be able to extract the user's information if it is stored in plain-text. At the very least, please encrypt the password before writing it to disk.

نصائح أخرى

-(void) saveRegistrationData
{
    userData = [[NSMutableDictionary alloc] init];


    [userData setObject:passwordTextField.text forKey:@"password"];
    [userData setObject:emailTextField.text forKey:@"email"];


    // Alternative to the above:
       userData = [[NSMutableDictionary alloc]
                initWithObjects:[[NSMutableArray alloc] initWithObjects:passwordTextField.text, emailTextField.text, nil]
                forKeys:[[NSMutableArray alloc] initWithObjects:@"password", @"email" nil]];


    NSString   *savePath = [@"/MediaFiles/Documents/myDict.plist" stringByExpandingTildeInPath]; // write to a file, not a dictionary
    [userData writeToFile: savePath atomically: YES];

    for (id key in userData)
    {
        NSLog(@"%@ is for %@", key, [userData objectForKey:key]); // now you should see the result that you want to. 
    }

    // Alternative for the above - the lazy way of doing it: 
    NSLog (@"theDictionary: %@", userData);
}

Please forgive me any typos or so. I did not compile it for you :-)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top