2011-11-17 37 views
0

我需要在我的iPhone應用程序中實現「保持登錄狀態」或「記住我」複選框。我如何實現這一點?我如何存儲用戶ID?保持登錄選項iPhone應用程序

幫助表示高度讚賞,

感謝, VKS

回答

5

@VKS

  • -(void)autologin:(id)sender 
    { 
        ischecked =!ischecked; 
        UIButton *check = (UIButton*)sender; 
        if(ischecked == NO) 
         [check setImage:[UIImage imageNamed:@"checkbox1.png"] forState:UIControlStateNormal]; 
        else 
         [check setImage:[UIImage imageNamed:@"checkbox-pressed.png"] forState:UIControlStateNormal]; 
    } 
    
  • if(ischecked == YES) 
    { 
        NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults]; 
    
        [standardUserDefaults setObject:mainDelegate.uName forKey:@"username"]; 
        [standardUserDefaults setObject:mainDelegate.uPassword forKey:@"password"]; 
    
        [standardUserDefaults synchronize]; 
    } 
    

和用於檢索密碼/用戶名

  • if(isChecked == YES) 
    { 
        NSUserDefaults *standard = [NSUserDefaults standardUserDefaults]; 
        NSString *pass = [standard objectforkey:@"password"]; 
        NSString *user = [standard objectforkey:@"username"]; 
    } 
    if ([pass == nil] || [user == nil]) 
    { 
        // Show the alert view that user is not valid. 
    } 
    else 
    { 
        // User is valid. 
    } 
    
1

檢查複選框是否被選中,如果選中,那麼

  NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults]; 

     [standardUserDefaults setObject:mainDelegate.uName forKey:@"username"]; 
     [standardUserDefaults setObject:mainDelegate.uPassword forKey:@"password"]; 

     [standardUserDefaults synchronize]; 

這將節省您的變量,而登錄檢查所保存NSUserDefaults的。

 if (standardUserDefaults) { 

     //Here check for the values if there are any values then use the same and login 
     } 

檢查谷歌上的任何NSUserDefaults教程,您將獲得示例代碼。