I'm having trouble persisting users across app launches using the Parse iOS SDK. I know when you login/signup using "saveInBackgroundWithBlock", the SDK automatically caches the PFUser, but now when I'm using PFLoginView + PFSignUpView, the user doesn't persist.
I know this because when the code:
if PFUser.currentUser() != nil {
self.presentNextView()
}
is run on viewDidLoad of the sign up view, the current user is set to nil after relaunching the app. Could someone please shed some light???
Related
I want to make an app from a PWA on iOS using a simple WKWebView. I've managed to implement it in a simple ViewController and it is working fine.
Problem : when i login and then quit the app, the session is lost and i have to login again.
Question : Is it possible to keep the session informations after the user quit the app ?
Thanks in advance for your answers !
You need to look into persisting your session data. For WKWebView, all of that is part of WKProcessPool. So when your app goes into the background, you need to look into persisting the WKProcessPool session, and when you use your web view, always use that same WKProcessPool instance.
Here's an answer that may help you save that data to your UserDefaults.
https://stackoverflow.com/a/52109021/2658489
I think you should implement login API, for authorization and Login UI on Native App, and then you can have dashboard items using single WKWebView controller.
When a user logged in with the native page - loginViewController, you should store user name, password (maybe encrypted) in user preferences, e,g.
UserDefaults.standard.set(userName, forKey: keyUserName)
UserDefaults.standard.set(userPassword, forKey: keyPassword)
UserDefaults.standard.synchronize()
Next time (after quit App) when you back you can check for auto login in AppDelegate - didFinishLaunchingWithOptions
e.g.
if let username = UserDefaults.standard.value(forKey: keyUserName) as? String,
password = UserDefaults.standard.value(forKey: keyPassword) as? String
{
/// DO AUTOLOGIN .. CALL API AND LAND TO DASHBOARD PAGE (WKWebView)..
}
XCode 9 (currently in beta) has a method called activate that can be used to bring an application to the foreground:
https://developer.apple.com/documentation/xctest/xcuiapplication/2873317-activate
Using the following code, I put the application into the background using the home button and then bring the application back to the foreground:
XCUIDevice.shared().press(XCUIDeviceButton.home)
let app = XCUIApplication(bundleIdentifier:"com.aaa.abc.xyz")
app.activate()
When the application is brought to the foreground, the user is taken back to the login screen. It behaves as if it does not have access to the token anymore which the tokens are currently stored in NSUserdefaults. Does activate not check here for tokens or clear it?
Using app.launch() again does work but curious why activate() does not?
I understand that by default Firebase invalidates a login token after 24 hours. However, I am finding the behavior strange after this time period. When the app is run it checks to see if the user is logged in and if so it goes into the app otherwise it stays on the login screen:
if self.ref.authData != nil
{
self.performSegueWithIdentifier("mainTabSegue", sender: self)
}
This works fine unless the token has expired after 24 hours. What will happen then is that the app will still see that authData is not nil and it will send it to the next VC. The next VC makes uses of the UID which then causes the app to crash. Running the app again will then show that authData is in fact nil and the user will be asked to login as is expected.
So the question is why, after the 24 hour period, is authData not nil when the user is clearly not logged in? The Firebase documentation seems to indicate that checking authData as above is the correct way to determine whether a user is logged in.
Before your segue if you extract the uid from authData then you can pass that user to the first view controller. If you pass the user object to your first VC constructed in the App Delegate, then your app won't crash. I vaguely remember something similar happening to my app (i.e. where it thinks the user is logged in but then changes back to login). I'm not sure why this happened but it's possible the app tried to start where it left off?
You can also change the token expiration length on Firebase, as you may know.
I am building an app with Parse in swift. PFUser.currentuser() always returns nil after the app is stopped and run again. I am using the iOS simulator and the local datastore is enabled.
I am using something like this -
if PFUser.currentuser() == nil {
// Perform segue to login screen
}
And for login I am using
PFUser.loginWithUsernameInBackground(...)
The currentuser remains till the app is restarted, after which it is reset to nil. I have even tried to pin the currentuser but it doesn't work.
How can I check if the currentuser is being cached locally.
Any help would be appreciated. Thanks.
For me, the reason is simply the following part is not executed. Be careful of multi-thread. Usually that's the cause.
Parse.setApplicationId("xx","xx")
Are you sure you're not resetting the simulator and are running on the same one? It should not clear the currentUser.
Is it possible you are calling
PFUser.logout()
in 'applicationWillTerminate' in AppDelegate or anywhere else?
PFUser currentUser works fine on iOS after logging in, but whenever I deploy a new app or crash the app manually, I have to re-sign it, which is not only annoying but not how Parse CurrentUser system works. Anybody know why? I am checking it in the completion block, and it displays correctly there...