to save and retrieve the userinfo in userdefault you need to something like
class Helper {
static func setUserInfo( userinfo: UserInfo?) {
if let info = userinfo {
UserDefaults.standard.setValue(NSKeyedArchiver.archivedData(withRootObject: info), forKey: "USERINFO")
}
else {
UserDefaults.standard.setValue(nil, forKey: "USERINFO")
}
UserDefaults.standard.synchronize()
}
static func getUserInfo() -> UserInfo? {
if let data = UserDefaults.standard.value(forKey: "USERINFO") {
return (NSKeyedUnarchiver.unarchiveObject(with: data as! Data) as! UserInfo)
}
return nil
}
}
and here is the userInfo class //here i am using objectmapper lib, you might/not need accordingly
class UserInfo: NSObject, NSCoding, Mappable {
var patientId: String?
var authKey : String?
override init() {
super.init()
}
required init?(map: Map){
}
func mapping(map: Map) {
patientId <- map["patient_id"]
authKey <- map["authKey"]
}
required init?(coder aDecoder: NSCoder) {
patientId = aDecoder.decodeObject(forKey: "patientId") as? String
authKey = aDecoder.decodeObject(forKey: "authKey") as? String
}
func encode(with aCoder: NSCoder) {
aCoder.encode(patientId, forKey: "patientId")
aCoder.encode(authKey, forKey: "authKey")
}
}
now in your registration view controller, in viewdidload method
if let _ = Helper.getUserInfo() { // there is user logged in, i.e. user installed the app and registered earlier
if let mainModuleTabBar = StoryboardHelper.mainModuleStory().instantiateViewController(withIdentifier: MyTabBarController.controllerIdentifier) as? MyTabBarController {
let window = (UIApplication.shared.delegate as! AppDelegate).window
window?.rootViewController = mainModuleTabBar
window?.makeKeyAndVisible()
}
return
}