I'm trying to fetch the jobSeeker ID from my api using the userDefaults but I'm having a nil as response.
Basically, what I'm doing is:
Creating a helper class as NSObject and saving the token (which is working unlike my userID).
class helper: NSObject {
class func restartApp(){
guard let window = UIApplication.shared.keyWindow else {return}
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let viewController : UIViewController
if getApiToken() == nil {
//go to auth screen
viewController = storyBoard.instantiateInitialViewController()!
} else {
//go to main screen
viewController = storyBoard.instantiateViewController(withIdentifier: "profileNavigationController")
}
window.rootViewController = viewController
UIView.transition(with: window, duration: 0.5, options: .transitionFlipFromTop, animations: nil, completion: nil)
}
class func saveApiToken(token: String){
//save api token to UserDefailts
let def = UserDefaults.standard
def.setValue(token, forKey: "token2")
def.synchronize()
restartApp()
}
class func saveJobSeekerIdDefault(defaultID: String){
//save userDefaultID to UserDefailts
let defID = UserDefaults.standard
defID.setValue(defaultID, forKey: "default_profile_id")
defID.synchronize()
restartApp()
}
class func getApiToken() -> String? {
let def = UserDefaults.standard
return def.object(forKey: "token2") as? String
}
class func getJobseekerID() -> String? {
let defID = UserDefaults.standard
return defID.object(forKey: "default_profile_id") as? String
}
}
After this, the initializers:
class UserProfileDefaults{
var userID : String
var defaultProfileType : String
var defaultProfileID : String
init?(profileDefaultsDict: [String:Any]){
guard let userID = profileDefaultsDict["user_id"] as? String,
let defaultProfileType = profileDefaultsDict["default_profile_type"] as? String,
let defaultProfileID = profileDefaultsDict["default_profile_id"] as? String else {return nil}
self.userID = userID
self.defaultProfileID = defaultProfileID
self.defaultProfileType = defaultProfileType
}
}
Then, I used Alamofire to fetch the id:
class func userProfileDefault(completion: @escaping( _ error: Error?, _ userProfileDefaults: UserProfileDefaults?) -> Void){
let url = URLs.profileDefaultsUrl
guard let api_token = helper.getApiToken() else {
completion(nil, nil)
return
}
let headers: HTTPHeaders = [
"Authorization": "Bearer \(api_token)",
"Accept": "application/json"
]
Alamofire.request(url, method: .get, encoding: URLEncoding.default, headers: headers)
.responseJSON { response in
switch response.result{
case .failure(let error):
completion(error, nil)
print(error)
case .success(let result):
let json = JSON(result)
print(json)
//Data - JobSeekerDefaults
guard let jobSeekerDefaults = json["data"].dictionaryObject else {
completion(nil, nil)
return
}
let userDefaultsData = UserProfileDefaults(profileDefaultsDict: jobSeekerDefaults)
//print("Test userID01: \(userDefaultsData?.userID)")
helper.saveJobSeekerIdDefault(defaultID: "97")
completion(nil, userDefaultsData)
}
}
}
I thought this was the way to get this data but I'm having nil as return. I really would like someone to help me because I'm stuck on this for a long time and it's driving me crazy.
Look more precisely on your server response most probably you mismatch it. In UserProfileDefaults
class initialisation set breakpoint on guard
and print in console value of profileDefaultsDict
like po profileDefaultsDict
. Make sure that model you provided in initialisation in compliance with server side