Reputation: 71
I've got a button, where you can create a new password by entering and reentering it.
When both are the same the password should be stored somewhere on the phone for further use.
For the moment i can ignore password safety.
How is the simplest way to store and read the password on the device?
btnLogin.TouchUpInside += delegate {
if(password.Exists /*My Isolated storage or password file*/){
ReturnToView();
} else{
new UIAlertView("Password doesn't exists", "Create a new password, to log in", null, "Ok").Show();
}
};
I've tried something like this, but I don't know how to check if the password exists in the .TXT
file.
void CreateUser(){
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter Writer = new StreamWriter(new IsolatedStorageFileStream("Password.txt", FileMode.OpenOrCreate, fileStorage));
btnCreateUser.TouchUpInside += delegate {
if(txtPassword.Text == txtRepeat.Text && txtPassword.Text.Length == 4){
GoBackToView(); //Login
Writer.WriteLine(txtPassword.Text);
Writer.Close();
} //here comes my else function, but that's not important.
};
}
After this, a password should be created in a .TXT
.
In a new method called Login()
I'll do this.
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamReader Reader = null;
try
{
Reader = new StreamReader(new IsolatedStorageFileStream("Password.txt", FileMode.Open, fileStorage));
Reader.Close();
}
catch
{
MessageBox.Show("File it not created");
}
Somewhere in my login method, I want to check if the password is created when I've entered a password, and when I press my login button.
Upvotes: 0
Views: 397
Reputation: 24460
The answer @manujmv posted can be translated to:
void StoreLoginCredentials(string password)
{
var userDetails = NSUserDefaults.StandardUserDefaults;
userDetails.SetString(password, "password");
userDetails.Synchronize();
}
bool CheckUserSession()
{
var userDetails = NSUserDefaults.StandardUserDefaults;
return userDetails["password"] != null;
}
bool DestroyLoginCredentials()
{
var defaults = NSUserDefaults.StandardUserDefaults;
defaults.RemovePersistentDomain(NSBundle.MainBundle.BundleIdentifier);
return defaults.Synchronize();
}
string PasswordOfUser()
{
var userDetails = NSUserDefaults.StandardUserDefaults;
return userDetails["password"];
}
Upvotes: 1
Reputation: 6445
You can store the password in NSUserDefaults. Just create the below method and pass the password to this method.
-(void)storeLoginCredentials:(NSString*)password {
userDetails = [NSUserDefaults standardUserDefaults];
[userDetails setObject:password forKey:@"password"];
[userDetails synchronize];
}
Then if you want to check whether the user is logged in or not you can use the below method:
-(BOOL)checkUserSession {
if ([userDetails objectForKey:@"password"]!= nil ) {
return YES;
} else {
return NO;
}
}
If you want to logged out the user, use below method.
-(void)destroyLoginCredentials {
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
}
You can take the password as
-(NSString *)passwordOfUser {
return [userDetails objectForKey:@"password"];
}
Upvotes: 2