简体   繁体   English

使用Alamofire时如何将令牌存储在NSUserDefault中?

[英]How can I Store the Token in NSUserDefault while using Alamofire?

I am making Email verification using OTP . 我正在使用OTP进行电子邮件验证。 I have used two API , one for registration and other for OTP verification. 我使用了两个API,一个用于注册,另一个用于OTP验证。 I want to move on the next page when user is valid. 我想在用户有效时继续下一页。 For this , I want to use NSUserDefault to store the token from the API response. 为此,我想使用NSUserDefault存储来自API响应的令牌。 When , I use this , i am unable to store this . 当我用这个的时候,我无法存储这个。 Please anybody help me for this. 请任何人帮助我。 Here is my code 这是我的代码

class OTPVerification: UIViewController, UITextFieldDelegate {

@IBOutlet weak var tfReceivedOTP: UITextField!
var datapassed:String!
let loader = MFLoader()
override func viewDidLoad() {
  super.viewDidLoad()
  tfReceivedOTP.attributedPlaceholder =     NSAttributedString(string:"OTP",
                                                           attributes:[NSForegroundColorAttributeName: UIColor.whiteColor()])
  tfReceivedOTP.delegate = self
  print(datapassed)

}




override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)
    let defaults = NSUserDefaults.standardUserDefaults()
    if defaults.objectForKey("email") == nil {
        if let loginController = self.storyboard?.instantiateViewControllerWithIdentifier("ConfirmationMassage") as? SignInConformation {
            self.navigationController?.presentViewController(loginController, animated: true, completion: nil)
        }
    }
 let defaults = NSUserDefaults.standardUserDefaults()
 if defaults.objectForKey("token") == nil {
 if let loginController = self.storyboard?.instantiateViewControllerWithIdentifier("ConfirmationMassage") as? SignInConformation {
 self.navigationController?.presentViewController(loginController, animated: true, completion: nil)
   }
 }

} }

func textFieldShouldReturn(textField: UITextField) -> Bool {

    textField.resignFirstResponder()
    return true
}
override func prefersStatusBarHidden() -> Bool {
    return true
}
@IBAction func btnOTPVerificationTapped(sender: AnyObject) {

    loader.showActivityIndicator(self.view)

    let rgModel = CAOTPVerify()

    rgModel.email = datapassed!
    rgModel.otpPassword = tfReceivedOTP.text!
    rgModel.otpRegister({(Void, Any) -> Void in

        let defaults = NSUserDefaults.standardUserDefaults()
        if let name = defaults.stringForKey("userNameKey") {
            print("\(name )hjhjkhkhkh")
        }

        self.loader.hideActivityIndicator(self.view)
        if let response = Any as? [String : AnyObject] {
            //print(response)
            if let messgae = response["message"] as? String {

                let alert = UIAlertController(title: "Alert", message: messgae, preferredStyle: UIAlertControllerStyle.Alert)
                alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: {action in

                      let storyboard = UIStoryboard(name: "Main", bundle: nil)
                      let OPTView = storyboard.instantiateViewControllerWithIdentifier("sideBarMenu") as! SideBarMenu
                      self.navigationController!.pushViewController(OPTView, animated: true)
                }))
                self.presentViewController(alert, animated: true, completion: nil)
            }
        }

        }, errorCallback: {(Void, NSError) -> Void in

            self.loader.hideActivityIndicator(self.view)
    })    
}

You didnt posted the code to save a string to the NSUserDefaults . 您没有发布将字符串保存到NSUserDefaults的代码。 You can refer following code to save a string in NSUserDefaults 您可以参考以下代码在NSUserDefaults保存字符串

let myString = "Hello World"
NSUserDefaults.standardUserDefaults().setObject(myString, forKey: "myKey")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM