开发者

Saving the login details into textfile in iphone

开发者 https://www.devze.com 2023-04-04 06:17 出处:网络
Hi could anyone help me how to save the login details that are entered into the text feild of login page should be saved in the text file when submit button is click and i also wanted to retrieve the

Hi could anyone help me how to save the login details that are entered into the text feild of login page should be saved in the text file when submit button is click and i also wanted to retrieve the login details saved in the text file....please suggest me开发者_高级运维 some solution


you should NOT save private data in UserDefaults. Use KeyChain instead.


You should store it in the keychain. You can just include this class, SFHFKeychainUtils in your project and use the methods:

+ (NSString *) getPasswordForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error;

+ (void) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error;

More details here: http://log.scifihifi.com/post/55837387/simple-iphone-keychain-code


It's really not secure to do so. NSUserDefaults or Core Data is much more robust; you can also consider an encryption feature. But if you must save to a text file, you can use NSString's [writeToFile:atomically:encoding:error][1] method. You pass in the path to a file, a BOOL specifying whether it should be atomically written, an encoding constant that specifies the text encoding, and a pointer to an NSError if you want error details.

You can fetch the text by using the text property of your input text fields.


If you wish to do it insecurely then here you go;

// Saving the login details

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
NSUserDefaults *defaults1=[NSUserDefaults standardUserDefaults];

NSString* Username = UsernameBox.text;
[defaults setObject:Username forKey:@"UsernameKey"];
NSString* temp = [defaults objectForKey:@"[UsernameKey"];

NSString* Password = PasswordBox.text;
[defaults1 setObject:Password forKey:@"PasswordKey"];
NSString* temp1 = [defaults1 objectForKey:@"[PasswordKey"];

[defaults synchronize];
[defaults1 synchronize];

// Loading the login details

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
NSUserDefaults *defaults1=[NSUserDefaults standardUserDefaults];

NSString* Username = UsernameBox.text;
NSString* temp = [defaults objectForKey:@"UsernameKey"];
UsernameBox.text = temp;
NSLog(@"Loaded Username: %@", temp); // Recommend removing for security after testing

NSString* Password = PasswordBox.text;
NSString* temp1 = [defaults1 objectForKey:@"PasswordKey"];
PasswordBox.text = temp1;
NSLog(@"Loaded Password: %@", temp1); // Recommend removing for security after testing

or...

I'd recommend using an encryption feature if you wanted it to be more secure, just like @Inspire48 mentioned.

Thanks,

James

0

精彩评论

暂无评论...
验证码 换一张
取 消