2012-09-06 113 views
-2

我需要在NSUserDefault中保存用戶名和密碼。我打算在IB中放置一個圓形按鈕。按下其中用戶名和密碼將被保存在NSUserDefault中,以便當用戶終止應用程序並在一段時間後嘗試再次登錄時,他們不需要再次輸入他們的登錄信息。 任何幫助將不勝感激。如何在NSUserDefault中保存用戶名和密碼?

感謝和問候,

PC

+9

你有沒有考慮把他們在鑰匙串呢? – Simon

+6

請不要將用戶名和密碼存儲到用戶默認值中,請查看[Keychain](http://stackoverflow.com/questions/6972092/ios-how-to-store-username-password-within-an-app/6972305#6972305)。 –

回答

7

用於保存用戶名和密碼,我會親自建議用Keychain,因爲他們在安全方面比NSUserDefault更安全,因爲加密Keychain存儲數據而NSUserDefault則以純文本形式存儲。如果你仍然想使用NSUserDefault這裏的方式

保存

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 

// saving an NSString 
[prefs setObject:txtUsername.text forKey:@"userName"]; 
[prefs setObject:txtPassword.text forKey:@"password"]; 

[prefs synchronize]; 

檢索

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 

// getting an NSString 
NSString *savedUsername = [prefs stringForKey:@"userName"]; 
NSString *savedPassword = [prefs stringForKey:@"password"]; 
3

拯救:

[[NSUserDefaults standardUserDefaults] setValue:_Username forKey:@"Username"]; 
[[NSUserDefaults standardUserDefaults] setValue:_password forKey:@"password"]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 

爲:

NSString * _UserName = [[NSUserDefaults standardUserDefaults] stringForKey:@"Username"]; 
NSString * _password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"]; 
1

您可以存儲你這樣的憑據:

-(void)saveToUserDefaults:(NSString*)stringUserName pswd:(NSString*)strPassword 
{ 
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults]; 
    if (standardUserDefaults) { 
     [standardUserDefaults setObject:stringUserName forKey:@"UserName"]; 
     [standardUserDefaults setObject:strPassword forKey:@"Password"]; 
     [standardUserDefaults synchronize]; 
    } 
} 

而且你可以retrive他們是這樣的:

-(NSArray*)retrieveFromUserDefaults 
{ 
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults]; 
    if (standardUserDefaults) { 
     NSString *userName = (NSString*)[standardUserDefaults objectForKey:@"UserName"]; 
     NSString *password = (NSString*)[standardUserDefaults objectForKey:@"Password"]; 
    } 
    NSArray* credentials = [NSArray arrayWithObjects:userName, password, nil]; 
    return credentials; 
} 
4

不要明文密碼存儲在用戶的默認值,即使它們不重要。使用Keychain ServicesGeneric Keychain Sample提供了示例KeychainWrapper類,它可以用於讀取和寫入鑰匙鏈中的數據,使用與NSUserDefaults使用的接口完全相同的setObject:forKey:接口。

2

首先,我不會將密碼存儲在NSUserDefaults中。我寧願使用鑰匙鏈。

這是如何保存NSUserDefaults的中的用戶名:

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults]; 
[standardUserDefaults setObject:myString forKey:@"username"]; 

NSString* username = [standardUserDefaults objectForKey:@"username"]; 

在另一方面,使用鑰匙鏈一個簡單的方法是使用SSKeychain class by Sam Soffes;在這種情況下,你只是說:

NSString* password = [SSKeychain passwordForService:@"YOUSERVICENAMEHERE" account:username]; 

[SSKeychain setPassword:password forService:@"YOUSERVICENAMEHERE" account:username]; 
0

Cannot set NSUserDefaults field

發佈我的代碼鏈接到儘管放心,這個問題的答案仍然是,即使上述職務已被移走或刪除將來有用的社區。

代碼:

你可以試試這個代碼。我很確定它會爲你工作。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSString *[email protected]"1"; 
[defaults setObject:uid forKey:@"init_val"]; 
[defaults synchronize]; 

用於檢索數據,你應該使用下面的代碼:

NSString *initVal=[[NSUserDefaults standardUserDefaults] valueForKey:@"init_val"]; 

OR

NSString *initVal=[[NSUserDefaults standardUserDefaults] objectForKey:@"init_val"]; 

編輯:

如果它仍然給人nil,則您可以使用下面的代碼:

NSString *initVal=[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"init_val"]]; 

OR

NSString *initVal=[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"init_val"]]; 

在上面的鏈接,你會發現我的答案和替換在我的代碼中有「init_val」與您的「用戶名」和「密碼」作爲鍵

希望這可以幫助你。

0

很好的答案已經給出,但這裏是保存/載入一個乾淨的方式/刪除用戶憑據鑰匙扣。考慮這一點,你可以創建一個單獨的類,幷包括下面的代碼:

.H

#import <Foundation/Foundation.h> 

@interface KeychainUserPass : NSObject 

+ (void)save:(NSString *)service data:(id)data; 
+ (id)load:(NSString *)service; 
+ (void)delete:(NSString *)service; 

@end 

.M

#import "KeychainUserPass.h" 

@implementation KeychainUserPass 

+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service { 

    return [NSMutableDictionary dictionaryWithObjectsAndKeys: 
      (__bridge id)kSecClassGenericPassword, (__bridge id)kSecClass, 
      service, (__bridge id)kSecAttrService, 
      service, (__bridge id)kSecAttrAccount, 
      (__bridge id)kSecAttrAccessibleAfterFirstUnlock, (__bridge id)kSecAttrAccessible, 
      nil]; 
} 

+ (void)save:(NSString *)service data:(id)data { 

    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; 
    SecItemDelete((__bridge CFDictionaryRef)keychainQuery); 
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData]; 
    SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL); 
} 

+ (id)load:(NSString *)service { 

    id ret = nil; 
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; 
    [keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData]; 
    [keychainQuery setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit]; 
    CFDataRef keyData = NULL; 

    if (SecItemCopyMatching((__bridge CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) { 
     @try { 
      ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData]; 
     } 
     @catch (NSException *e) { 
      NSLog(@"Unarchive of %@ failed: %@", service, e); 
     } 
     @finally {} 
    } 
    if (keyData) CFRelease(keyData); 
    return ret; 
} 

+ (void)delete:(NSString *)service { 

    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; 
    SecItemDelete((__bridge CFDictionaryRef)keychainQuery); 
} 


@end 
相關問題