2012-07-03 100 views
0

我想在用戶選擇記住我複選框時保存密碼。我有這樣的代碼:這是正確的嗎?如何使用NSUserDefault在UserDefault中保存密碼?

在Appdelegate.m

@property(strong,nonatomic)NSString *savedno; 

在.m文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 

    // Override point for customization after application launch. 
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; 

    self.navcontrol =[[UINavigationController alloc]initWithRootViewController:self.viewController ]; 

    self.window.rootViewController = self.navcontrol; 
    [self.window makeKeyAndVisible]; 

    self.savedno=[[NSUserDefaults standardUserDefaults]objectForKey:@"pass"]; 

    if(savedno==nil) 
    { 
     NSDictionary *saveDict=[NSDictionary dictionaryWithObject:savedno forKey:@"pass"]; 
     [[NSUserDefaults standardUserDefaults]registerDefaults:saveDict]; 
    } 
    return YES; 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    /* 
    Called when the application is about to terminate. 
    Save data if appropriate. 
    See also applicationDidEnterBackground:. 
    */ 

    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults]; 
    [userdefault setObject:savedno forKey:@"pass"]; 
} 

//ViewDelegate.m文件

//事件複選框

-(IBAction)check:(id)sender 
{ 
    if (checkboxSelected == 0) 
    { 
     [checkboxButton setSelected:YES]; 
     [checkboxButton setImage:[UIImage imageNamed:@"checkbox-checked.png"]forState:UIControlStateNormal]; 

     // userdefault=[NSUserDefaults standardUserDefaults]; 
     // [userdefault setObject:user.text forKey:@"user"]; 
     // [userdefault setObject:pass.text forKey:@"pass"]; 
     self.callno=pass.text; 
     pass.text=self.callno; 
     AppDelegate *appdelegate=[[UIApplication sharedApplication]delegate]; 
     appdelegate.savedno=self.callno; 

     NSLog(@"Data saved");  
     checkboxSelected = 1;   
    } 
    else 
    { 
     [checkboxButton setSelected:NO]; 
     [checkboxButton setImage:[UIImage imageNamed:@"checkbox.png"]forState:UIControlStateNormal];         
     [email protected]""; 
     [email protected]""; 
     checkboxSelected = 0; 
    } 
} 

- (void)viewDidLoad 
{ 
    user.clearButtonMode=UITextFieldViewModeWhileEditing; 

    AppDelegate *appdel=[[UIApplication sharedApplication]delegate]; 

    if([user.text isEqualToString:@"bhoomi"]) 
    { 
     pass.text=appdel.savedno; 
    } 
    [super viewDidLoad]; 

    checkboxSelected = 0; 

    // Do any additional setup after loading the view, typically from a nib. 
} 
` 
+6

你爲什麼不保存在鑰匙串的密碼? – Felix

+4

鑰匙串:http://stackoverflow.com/a/2600619/14955 – Thilo

+3

請使用鑰匙串。觀看WWDC2012 - 從大約17分鐘英寸704會話。 – hypercrypt

回答

0

使用此代碼...

NSUserDefault *userDefault = [NSUserDefaults standardUserDefaults]; 

[userDefault setValue:pass_str forKey:@"pass"]; 

[userDefault synchronize]; 

可能,這將幫助你...

+0

你會得到你的密碼..我的意思是如果在viewController你有文本字段,那麼你必須做viewController – Abhishek

+0

它是爲你工作嗎? – Abhishek

+0

只需填寫您的用戶名在文本字段中,當他將提交按鈕或登錄按鈕,無論你想..在你的按鈕功能的文本字段中獲取用戶名和密碼,並將其保存到NSUsedrDefalut像上面...得到我的觀點? – Abhishek

1

要保存以用戶默認值執行以下

[[NSUserDefaults standardUserDefaults] setValue:@"YourPassword" forKey:@"passowrd"]; 
//then call 
[[NSUserDefaults standardUserDefaults] synchronize]; 

要設定以你NSUserDefaults的必須同步,一旦得到它保存後拿回來

NSString *password = [[NSUserDefaults standardUserDefaults] valueForKey:@"password"]; 
1

。使用以下語句來做到這一點。

[[NSUserDefaults standardUserDefaults] synchronize]; 

希望這會有所幫助。

編輯:

+0

爲什麼downvote?也沒有說明理由?這太奇怪了。 –

7

不要。 NSUserDefaults不是密碼等機密信息的地方。相反,請使用KeyChain

3

您應該使用鑰匙串存儲密碼等信息。

看看SFHFKeychainUtils,它將允許在您的應用程序中輕鬆使用鑰匙串。它提供了兩個方法:storeUserName:andPassword:和getPasswordForUsername,它將使它成爲一個快速安全的!