I have setup the parse & all, sign up works perfectly as needed. But after that, I am unable to get the user details back on device for offline use.
I need those objects offline once the user Logs In / Signs Up.
Class Name : User
Objects:
coverPhoto
: Image.png (saved as File)
profilePicture
: Image.png (saved as File)
usersUsername
: String
username
/ email
: String
firstName
: String
middleName
: String
lastName
: String
mobileNumber
: String
birthDate
: String
gender
: String
about
: String
I tried to store it using UserDefaults.standard.set(_variable name_, forKey: "String")
but didn't work. :\
This is what i am geting repeatedly.
This is what i wrote:
@IBAction func loginButtonPressed(_ sender: AnyObject) {
activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = .gray
activityIndicator.layer.zPosition = 1
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.shared.beginIgnoringInteractionEvents()
if email.text == "" || password.text == "" {
createAlert(title: "Error", message: "Please enter your username & password!")
self.activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
} else {
PFUser.logInWithUsername(inBackground: email.text!, password: password.text!, block: { (suser, error) in
if error != nil {
var displayErrorMessage = "Some Error Occured! Please Try Again Later!"
if let errorMessage = error as? NSError {
displayErrorMessage = errorMessage.userInfo["error"] as! String
}
self.createAlert(title: "Dang it!", message: displayErrorMessage)
self.activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
} else {
self.performSegue(withIdentifier: "toMainFunction", sender: nil)
let query = PFUser.query()
query?.findObjectsInBackground(block: { (objects, error) in
if error != nil {
print(error)
} else if let users = objects {
for object in users {
if let user = object as? PFUser {
if user.objectId != PFUser.current()?.objectId {
if let email = user.username {
UserDefaults.standard.set(email, forKey: "email")
}
if let coverFile = user["coverPhoto"] {
UserDefaults.standard.set(coverFile, forKey: "coverPhoto")
}
if let profileFile = user["profilePicture"] {
UserDefaults.standard.set(profileFile, forKey: "profilePicture")
}
if let firstName = user["firstName"] {
UserDefaults.standard.set(firstName, forKey: "firstName")
}
if let middleName = user["middleName"] {
UserDefaults.standard.set(middleName, forKey: "middleName")
}
if let lastName = user["lastName"] {
UserDefaults.standard.set(lastName, forKey: "lastName")
}
if let mobileNumber = user["mobileNumber"] {
UserDefaults.standard.set(mobileNumber, forKey: "mobileNumber")
}
if let birthday = user["BirthDate"] {
UserDefaults.standard.set(birthday, forKey: "BirthDate")
}
if let gender = user["gender"] {
UserDefaults.standard.set(gender, forKey: "gender")
}
if let aboutYou = user["about"] {
UserDefaults.standard.set(aboutYou, forKey: "about")
}
if let username = user["usersUsername"] {
UserDefaults.standard.set(username, forKey: "username")
}
gloFirstName = UserDefaults.standard.object(forKey: "firstName") as! String
gloMiddleName = UserDefaults.standard.object(forKey: "middleName") as! String
gloLastName = UserDefaults.standard.object(forKey: "lastName") as! String
gloMobNumber = UserDefaults.standard.object(forKey: "mobileNumber") as! String
gloEmail = UserDefaults.standard.object(forKey: "email") as! String
gloUName = UserDefaults.standard.object(forKey: "username") as! String
gloBirthDate = UserDefaults.standard.object(forKey: "BirthDate") as! String
gloGenderStr = UserDefaults.standard.object(forKey: "gender") as! String
gloAbout = UserDefaults.standard.object(forKey: "about") as! String
if let coverImageData = UserDefaults.standard.object(forKey: "coverPhoto"),
let coverPhoto = UIImage(data: coverImageData as! Data){
gloCoverImage = coverPhoto
}
if let profileImageData = UserDefaults.standard.object(forKey: "profilePicture"),
let profilePicture = UIImage(data: profileImageData as! Data){
gloProfilePicture = profilePicture
}
}
}
}
}
})
self.activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
}
})
}
}
I am in a bind here.
EDIT:
EDIT#2:
Highlighted Code :
let query = PFUser.query()
query?.findObjectsInBackground(block: { (objects, error) in
if error != nil {
print(error)
} else if let users = objects {
for object in users {
if let user = object as? PFUser {
//-------------------------------------------------------------------------------------//
if user.objectId != PFUser.current()?.objectId {
if let email = user.username {
UserDefaults.standard.set(email, forKey: "email")
}
if let coverFile = user["coverPhoto"] {
UserDefaults.standard.set(coverFile, forKey: "coverPhoto")
}
if let profileFile = user["profilePicture"] {
UserDefaults.standard.set(profileFile, forKey: "profilePicture")
}
if let firstName = user["firstName"] {
UserDefaults.standard.set(firstName, forKey: "firstName")
}
if let middleName = user["middleName"] {
UserDefaults.standard.set(middleName, forKey: "middleName")
}
if let lastName = user["lastName"] {
UserDefaults.standard.set(lastName, forKey: "lastName")
}
if let mobileNumber = user["mobileNumber"] {
UserDefaults.standard.set(mobileNumber, forKey: "mobileNumber")
}
if let birthday = user["BirthDate"] {
UserDefaults.standard.set(birthday, forKey: "BirthDate")
}
if let gender = user["gender"] {
UserDefaults.standard.set(gender, forKey: "gender")
}
if let aboutYou = user["about"] {
UserDefaults.standard.set(aboutYou, forKey: "about")
}
if let username = user["usersUsername"] {
UserDefaults.standard.set(username, forKey: "username")
}
gloFirstName = UserDefaults.standard.object(forKey: "firstName") as! String
gloMiddleName = UserDefaults.standard.object(forKey: "middleName") as! String
gloLastName = UserDefaults.standard.object(forKey: "lastName") as! String
gloMobNumber = UserDefaults.standard.object(forKey: "mobileNumber") as! String
gloEmail = UserDefaults.standard.object(forKey: "email") as! String
gloUName = UserDefaults.standard.object(forKey: "username") as! String
gloBirthDate = UserDefaults.standard.object(forKey: "BirthDate") as! String
gloGenderStr = UserDefaults.standard.object(forKey: "gender") as! String
gloAbout = UserDefaults.standard.object(forKey: "about") as! String
if let coverImageData = UserDefaults.standard.object(forKey: "coverPhoto"),
let coverPhoto = UIImage(data: coverImageData as! Data){
gloCoverImage = coverPhoto
}
if let profileImageData = UserDefaults.standard.object(forKey: "profilePicture"),
let profilePicture = UIImage(data: profileImageData as! Data){
gloProfilePicture = profilePicture
}
}//-----------------------------------------------------------------------------//
}
}
}
})
Nevermind, solved it. This
if user.objectId != PFUser.current()?.objectId {
should've been this,
if user.objectId == PFUser.current()?.objectId {
since i needed my currently logged in account details to show up.
Also images can't be saved to UserDefaults
. So just load it in every time you open the app and store it in a var myVariableName = UIImage()
along with few if
statement checks you can force unwrap it to this: var myVariableName = UIImage()!
Thanks Anyway!