当与其他用户登录时,它仍然会从无处选择以前的用户 uuid
Posted
技术标签:
【中文标题】当与其他用户登录时,它仍然会从无处选择以前的用户 uuid【英文标题】:When login with other user it still picks previous user uuid from nowhere 【发布时间】:2019-10-11 23:12:24 【问题描述】:我正在处理我的一些项目,我面临的问题是当我使用用户“A”登录并使用该应用程序一段时间然后我注销该用户,然后我使用另一个用户“B”登录然后在我必须在其中显示用户个人资料图片(Baseurl + profilepic 名称)的一些屏幕。它没有显示,当我记录个人资料图片 url 的日志时,我发现它仍在获取旧登录用户“A”的 uuid。
注意:- BaseUrl 由与 uuid 连接而成。
这里是登录和注销方法类和常量类,我用它们来保存登录响应的数据和 nil 注销响应的数据
如果状态为 1 的登录方法:-
if(status == 1)
DispatchQueue.main.async
GAReusableClass.sharedInstance.hideActivityIndicator()
UserDefaults.standard.set(self.DataDict, forKey:MaindataKey)
let Dict = self.mainDict[KData] as! [String: AnyObject]
print("self.DataDict", self.DataDict)
let User_ID = Dict[KUuid]as! String
print(User_ID)
let HMACSECRETKEY = self.deviceToken + "+" + User_ID
kHMACKey = HMACSECRETKEY
let cipher:String = CryptoHelper.encrypt(input:HMACSECRETKEY)!;
print(HMACSECRETKEY)
UserDefaults.standard.setValue(cipher, forKey:HmacKey)
UserDefaults.standard.set(true, forKey: "isLogin")
GAloginUserInfo.shared.saveUserInfo(dict: Dict )
let tabar = self.storyboard?.instantiateViewController(withIdentifier: "GAtHomeTabbarViewController") as! GAtHomeTabbarViewController
self.navigationController?.pushViewController(tabar, animated: true)
如果状态为 1 的注销方法:-
if(status == 1)
DispatchQueue.main.async
GAReusableClass.sharedInstance.hideActivityIndicator()
defaults.removeObject(forKey: MaindataKey)
defaults.removeObject(forKey: HmacKey)
defaults.set(false, forKey: "isLogin")
GAloginUserInfo.shared.removeUserInfo()
let login = self.storyboard?.instantiateViewController(withIdentifier: "GALoginViewController") as! GALoginViewController
self.navigationController?.pushViewController(login, animated: true)
常量类:-
import UIKit
let defaults = UserDefaults.standard
var kDeviceToken = String()
var kHMACKey = String()
let Headerkey = "header"
let KData = "data"
let KStatus = "status"
let KMessage = "message"
let MaindataKey = "MAINDATA"
let KUuid = "uuid"
let HmacKey = "HMACKEY"
let imageThumbBaseURL = "http://demodemodemo.ga/uploads/"+"\(GAloginUserInfo.shared.loginUserUuid!)"+"/video_thumbs/"
let videoFileBaseURL = "http://demodemodemo.ga/uploads/"+"\(GAloginUserInfo.shared.loginUserUuid!)"+"/video_files/"
let BaseURl = "http://demodemodemo.ga/index.php?api/"
let BaseURLProfilePic = "http://demodemodemo.ga/uploads/"+"\(GAloginUserInfo.shared.loginUserUuid!)"+"/profile_pic/"
登录用户共享类:-
import UIKit
class GAloginUserInfo: NSObject
var loginUserMobileNo : String?
var loginUserId : String?
var loginUserUuid : String?
var loginUserCountry : String?
var loginUserCountryCode : String?
var loginUserEmail : String?
var loginUserlatitude : String?
var loginUserLongitude : String?
var loginUserName : String?
var loginUserQrcode : String?
var loginUserProfilePic : String?
var isverify : String?
var loginPassword : String?
var dateOfBirth: String?
var earnedPoints:String?
var loginUserGender:String?
var loginUserFollowers:Int = 0
static let shared = GAloginUserInfo()
func saveUserInfo (dict : [String : AnyObject?] )
if let loginUserMobileNo = dict["mobile"] as? String
self.loginUserMobileNo = loginUserMobileNo
if let loginUserId = dict["id"] as? String
self.loginUserId = loginUserId
if let loginUserUuid = dict["uuid"] as? String
self.loginUserUuid = loginUserUuid
print(loginUserUuid)
if let loginUserCountry = dict["countryNameCode"] as? String
self.loginUserCountry = loginUserCountry
if let loginUserCountryCode = dict["country_code"] as? String
self.loginUserCountryCode = loginUserCountryCode
if let loginUserEmail = dict["email"] as? String
self.loginUserEmail = loginUserEmail
if let loginUserProfilePic = dict["profile_pic"] as? String
self.loginUserProfilePic = loginUserProfilePic
if let loginUserLongitude = dict["logitude"] as? String
self.loginUserLongitude = loginUserLongitude
if let loginUserName = dict["name"] as? String
self.loginUserName = loginUserName
if let loginUserQrcode = dict["qr_code"] as? String
self.loginUserQrcode = loginUserQrcode
if let Password = dict["password"] as? String
self.loginPassword = Password
if let dateOfBirth = dict["dob"] as? String
self.dateOfBirth = dateOfBirth
if let earnedPoints = dict["points"] as? String
let myDouble = Double(earnedPoints)
let doubleStr = String(format: "%.2f", myDouble!)
self.earnedPoints = doubleStr
if let loginUserGender = dict["gender"] as? String
self.loginUserGender = loginUserGender
if let loginUserFollowers = dict["followersCount"] as? Int
self.loginUserFollowers = loginUserFollowers
extension GAloginUserInfo
func removeUserInfo()
self.loginUserMobileNo = nil
self.loginUserId = nil
self.loginUserUuid = nil
self.loginUserCountry = nil
self.loginUserCountryCode = nil
self.loginUserEmail = nil
self.loginUserlatitude = nil
self.loginUserLongitude = nil
self.loginUserName = nil
self.loginUserQrcode = nil
self.loginUserProfilePic = nil
self.isverify = nil
self.loginPassword = nil
self.dateOfBirth = nil
self.earnedPoints = nil
self.loginUserGender = nil
self.loginUserFollowers = 0
【问题讨论】:
【参考方案1】:我认为当你注销 UIKit 时会保留一些值,以删除它, 当您注销时,您应该清除所有视图控制器,您的所有 UIKit 数据将被删除,您的问题得到解决。如果您的注销 API 状态为 1,请输入此代码。
let toViewController = mainStoryboard.instantiateViewController(withIdentifier: "KSLoginViewController") as! KSLoginViewController// Login view controller here
let fromView = UIApplication.shared.keyWindow!.rootViewController!.view
UIApplication.shared.keyWindow?.rootViewController = toViewController
let toView = toViewController.view
toView?.addSubview(fromView!)
UIView.animate(withDuration: 0.38, delay: 0.2, options: [], animations:
fromView?.transform = CGAffineTransform(translationX: -UIScreen.main.bounds.width, y: 0)
, completion: finished in
fromView?.removeFromSuperview()
)
注销后,当您尝试在登录后推送另一个视图控制器时,请使用此代码。
let teacher = studentStoryboard.instantiateViewController(withIdentifier: "KSStudentDashboardVC") as! KSStudentDashboardVC
let aObjNavi = UINavigationController(rootViewController: teacher)
let appDelegate: AppDelegate = (UIApplication.shared.delegate as? AppDelegate)!
appDelegate.window?.rootViewController = aObjNavi
【讨论】:
【参考方案2】:我认为问题是由于线程造成的。将非 uikit 类操作从主调度队列中移出。
如果状态为 1 的登录方法:-
if(status == 1)
UserDefaults.standard.set(self.DataDict, forKey:MaindataKey)
let Dict = self.mainDict[KData] as! [String: AnyObject]
print("self.DataDict", self.DataDict)
let User_ID = Dict[KUuid]as! String
print(User_ID)
let HMACSECRETKEY = self.deviceToken + "+" + User_ID
kHMACKey = HMACSECRETKEY
let cipher:String = CryptoHelper.encrypt(input:HMACSECRETKEY)!;
print(HMACSECRETKEY)
UserDefaults.standard.setValue(cipher, forKey:HmacKey)
UserDefaults.standard.set(true, forKey: "isLogin")
GAloginUserInfo.shared.saveUserInfo(dict: Dict )
DispatchQueue.main.async
GAReusableClass.sharedInstance.hideActivityIndicator()
let tabar = self.storyboard?.instantiateViewController(withIdentifier: "GAtHomeTabbarViewController") as! GAtHomeTabbarViewController
self.navigationController?.pushViewController(tabar, animated: true)
如果状态为 1 的注销方法:-
if(status == 1)
defaults.removeObject(forKey: MaindataKey)
defaults.removeObject(forKey: HmacKey)
defaults.set(false, forKey: "isLogin")
GAloginUserInfo.shared.removeUserInfo()
DispatchQueue.main.async
GAReusableClass.sharedInstance.hideActivityIndicator()
let login = self.storyboard?.instantiateViewController(withIdentifier: "GALoginViewController") as! GALoginViewController
self.navigationController?.pushViewController(login, animated: true)
【讨论】:
以上是关于当与其他用户登录时,它仍然会从无处选择以前的用户 uuid的主要内容,如果未能解决你的问题,请参考以下文章