ios - 用户关闭应用程序时如何保持登录状态?斯威夫特 4
问题描述
我使用 Alamofire 登录,一切正常,但我尽一切努力在关闭时记录应用程序,但我做不到。注意:我还是不做注销功能
这是我的代码
import Alamofire
import UIKit
import AccountKit
import AlamofireObjectMapper
import Toast_Swift
import JGProgressHUD
import PasswordTextField
class ViewController: UIViewController, AKFViewControllerDelegate, UITextFieldDelegate {
@IBOutlet weak var txtmail: DesignableTextField!
@IBOutlet weak var txtcontrasena: DesignableTextField!
@IBOutlet weak var txtversion: UILabel!
@IBOutlet weak var btnlogin2: RoundedBorderButton!
@IBAction func textFieldPrimaryAction(_ sender: Any) {
login(email: txtmail.text!,password: txtcontrasena.text!)
}
var accountKit: AKFAccountKit!
override func viewDidLoad() {
super.viewDidLoad()
btnlogin2.setTitle("INGRESAR", for: .normal)
//Init Account Kit
if accountKit == nil {
self.accountKit = AKFAccountKit(responseType: .accessToken)
}
func version() -> String {
let dictionary = Bundle.main.infoDictionary!
let version = dictionary["CFBundleShortVersionString"] as! String
//let build = dictionary["CFBundleVersion"] as! String
return "\(version)"
}
txtversion.text = "Ver."+" "+version()
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "background.png")
backgroundImage.contentMode = UIViewContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
}
//Funcion Login
func login (email:String,password:String){
let hud = JGProgressHUD(style: .dark)
hud.textLabel.text = "Cargando"
hud.show(in: self.view)
let parameters: Parameters = ["email": email as Any,
"password": password as Any,
"businessId": 0,
"tokenFirebase": "string"]
Alamofire.request("http://00.00.00.00.00.00.00" , method: .post, parameters: parameters , encoding: JSONEncoding.default).validate().responseObject { (response :DataResponse<Result<User>> )in
switch response.result {
case .success:
let result = response.result.value
print (result?.message as Any)
//print(response.result.value?.message!)
self.showProgress(hud: hud, message: "Success", indicator: true)
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.performSegue(withIdentifier: "reveal", sender: self)
}
case .failure(let error):
print (error)
switch response.response?.statusCode{
case 400:
self.showProgress(hud: hud, message: "Datos inválidos", indicator: false )
break
case 401:
self.showProgress(hud: hud, message: "Credenciales inválidas", indicator: false)
break
default: break
}
}
}
}
func showProgress(hud: JGProgressHUD, message:String, indicator: Bool) {
UIView.animate(withDuration: 1, animations: {
hud.textLabel.text = message
hud.detailTextLabel.text = nil
if indicator {
hud.indicatorView = JGProgressHUDSuccessIndicatorView()
}else{
hud.indicatorView = JGProgressHUDErrorIndicatorView()
}
hud.dismiss(afterDelay: 2)
})
}
@IBAction func btnlogin(_ sender: Any) {
login(email: txtmail.text!,password: txtcontrasena.text!)
}
//Button Register
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if (accountKit.currentAccessToken != nil) {
//print("User already logged in, go to the home screen")
DispatchQueue.main.async(execute: {
self.performSegue(withIdentifier: "loginview", sender: self
)
})
}
}
解决方案
该应用程序不会从自身保持登录状态,您必须存储跟踪它的数据,您可以在 keyChain 中存储用户电子邮件和密码,或者在 userDefaults 中存储一个 bool 值,AppDelegate
当didFinishLaunchingWithOptions
应用程序打开并导航到适当的 VC
if loggedIn
{
self.window?.rootViewController = homeVC
}
else {
self.window?.rootViewController = loginVC
}
推荐阅读
- python - 如何更改 DataFrame 中值的长度
- python - 附加到来自 SQL 的列表结果
- ios - 在 iOS 运行时禁用 crashlytics/Fabric
- out-of-memory - ArrayFire CPU,它会因为迟到的GC而耗尽内存吗?
- javascript - 无法使用 jquery 插件从图像裁剪中获取值
- javascript - 如何在 Google Apps 脚本中按文件路径过滤
- r - 安装 R 包错误:安装二进制包失败
- angularjs - AngularJS自定义过滤器过滤项目类型
- git - 使用 git 推送或拉取时如何抑制存储库 url?
- c# - Windows 10 上的蓝牙 LE 外设角色