使用 Alamofire 时如何将令牌存储在 NSUserDefault 中?
Posted
技术标签:
【中文标题】使用 Alamofire 时如何将令牌存储在 NSUserDefault 中?【英文标题】:How can I Store the Token in NSUserDefault while using Alamofire? 【发布时间】:2016-07-18 05:38:40 【问题描述】:我正在使用 OTP 进行电子邮件验证。我使用了两个 API,一个用于注册,另一个用于 OTP 验证。当用户有效时,我想进入下一页。为此,我想使用 NSUserDefault 来存储 API 响应中的令牌。当我使用这个时,我无法存储它。请任何人帮助我。 这是我的代码
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)
)
【问题讨论】:
但是您的网络服务结果中没有保存token
先生,由于我是 swift 新手,请帮帮我
在哪个网络服务中获得令牌兄弟没有问题
【参考方案1】:
您没有发布将字符串保存到NSUserDefaults
的代码。您可以参考以下代码将字符串保存在NSUserDefaults
let myString = "Hello World"
NSUserDefaults.standardUserDefaults().setObject(myString, forKey: "myKey")
【讨论】:
不需要同步,需要延迟执行 好兄弟,再来一个请求采纳你关于 qustion 的回答,很容易解决以上是关于使用 Alamofire 时如何将令牌存储在 NSUserDefault 中?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Alamofire Swift 中刷新 JWT 令牌状态代码 500