在Swift中使用UISwitch保存用户名和密码

时间:2015-05-25 03:12:15

标签: swift passwords nsuserdefaults username uiswitch

我已经搜索了很多线程,这是我的最后一招,因为我看到这个问题有不同的方式,但不完全是swift和用户名和密码保存。打开时我想要我的UISwitch来保存我的用户名和密码信息。我一直在努力让这个UISwitch使用NSUserDefaults保存我的用户名和密码。请帮助我,我试着尝试一下,我几乎每个论坛都找到了答案,但没有多少人告诉你如何实现它。以下是我的代码。我知道它很黯淡,因为我是初学者,但我有我的"登录按钮"保存我的用户名和密码,但我不知道如何在我点击UISwitch时保存该信息,以及如何将其保存在"视图中加载方法"。我在这里先向您的帮助表示感谢!!我不知道要包含在我的UISwitch方法中的代码。 这是我的登录按钮的第一部分,然后我的视图加载方法。我没有UISwitchMethod的任何代码

 override func viewDidLoad() {
super.viewDidLoad()

//Save username and password info if Save UISwitch is selected

  switchState.on = NSUserDefaults.standardUserDefaults().boolForKey("switchState")
NSUserDefaults.standardUserDefaults().boolForKey("keepUsername")
NSUserDefaults.standardUserDefaults().boolForKey("keepPassword")

}

@IBAction func LoginButton(sender: AnyObject) {

var username = self.usernameTextField.text
var password = self.passwordTextField.text
var user = PFUser.currentUser()


NSUserDefaults.standardUserDefaults().setObject(username, forKey: "keepUsername")
NSUserDefaults.standardUserDefaults().setObject(password, forKey: "keepPassword")

if count(username) < 4 || count(password) < 5 {

  var alert: UIAlertView = UIAlertView(title: "Sorry!", message: "Username Must be greater than 4 characters and the password greater that 5 characters", delegate: self, cancelButtonTitle: "Ok")

  alert.show()    
}else { 
  self.actInd.startAnimating()
  PFUser.logInWithUsernameInBackground(username, password: password, block: { (user, NSError) -> Void in

    self.actInd.stopAnimating()

    if ((user) != nil) {

      println("Success \(user) logged in")

    self.performSegueWithIdentifier("toHomeFromLogin", sender: self)

    }else {
       var alert: UIAlertView = UIAlertView(title: "error", message: "Please Sign up :)", delegate: self, cancelButtonTitle: "Ok")
      alert.show()
    }


 @IBAction func switchStateChanged(sender: UISwitch) {


NSUserDefaults.standardUserDefaults().setBool(switchState.on, forKey: "switchState")

  }

2 个答案:

答案 0 :(得分:2)

我会添加一些笔记和编辑,我希望它可以帮到你:

   var switchState = Bool()
   var userName = String()
   var password = String()

   override func viewDidLoad() {
    super.viewDidLoad()

//Load all values

    switchState = NSUserDefaults.standardUserDefaults().boolForKey("switchState")
    userName = NSUserDefaults.standardUserDefaults().stringForKey("keepUsername")
    password = NSUserDefaults.standardUserDefaults().stringForKey("keepPassword")
    //Display values somewhere
}

@IBAction func LoginButton(sender: AnyObject) {

    var enteredUser = self.usernameTextField.text
    var enteredPassword = self.passwordTextField.text
    var user = PFUser.currentUser()
    NSUserDefaults.standardUserDefaults().setObject(enteredUser, forKey: "keepUsername")
    NSUserDefaults.standardUserDefaults().setObject(enteredPassword, forKey: "keepPassword")
    NSUserDefaults.standardUserDefaults().synchronize()
    if count(username) < 4 || count(password) < 5 {
        var alert: UIAlertView = UIAlertView(title: "Sorry!", message: "Username Must be greater than 4 characters and the password greater that 5 characters", delegate: self, cancelButtonTitle: "Ok")   
        alert.show()    
    }else { 
       self.actInd.startAnimating()
       PFUser.logInWithUsernameInBackground(username, password: password, block: { (user, NSError) -> Void in 
       self.actInd.stopAnimating(   
        if ((user) != nil) {
             println("Success \(user) logged in")
             self.performSegueWithIdentifier("toHomeFromLogin", sender: self)
         }else {
             var alert: UIAlertView = UIAlertView(title: "error", message: "Please Sign up :)", delegate: self, cancelButtonTitle: "Ok")
         alert.show()
    }
 @IBAction func switchStateChanged(sender: UISwitch) {
    //var readValueFromSwitch = something bolean
    //NSUserDefaults.standardUserDefaults().setBool(readValueFromSwitch, forKey: "switchState")
    //NSUserDefaults.standardUserDefaults().synchronize() 
  }

答案 1 :(得分:0)

我发送了一个漏洞页面,以便您可以了解并且还有一种方法可以为Register页面发送警报消息,查看//存储数据

导入UIKit

类RegisterPageViewController:UIViewController {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    if (!isRunning) {
        mythread.start();
        isRunning = true;
    }
    sp = PreferenceManager.getDefaultSharedPreferences(this);
    return super.onStartCommand(intent, flags, startId);
}

public void accessWebPage() {

    try {
        busID = sp.getString("busNumber", "");
        ipadd = sp.getString("ipAddress", "");
        response = httpclient.execute(new HttpGet("http://" + ipadd
                + link+"?msg=ID" + busID + "%20TM" + time
                + "%20DT" + date + "%20LT" + latitude + "%20LN" + longitude
                + "%20AT" + altitude + "%20SP" + speed + "%20CO" + course));
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    } catch (Exception e) {
    }

}

class MyThread extends Thread {
    static final long DELAY = 3000;

    @Override
    public void run() {
        while (isRunning) {
                try {
                accessWebPage();
                Thread.sleep(DELAY);
            } catch (InterruptedException e) {

            }
        }
    }
    }

}