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)..
}
Related
Auth.auth().signIn(withEmail:
In my case, When I first time open app and sign in firebase account like above code
then I reopen app
let user = Auth.auth().currentUser
user is not nil and then I reopen app get currentUser again user is nil that mean I did not sign in
I have the other app do like this, but the user always not nil.
I wonder why this happens.
Firebase automatically restores the authenticated user when the app restarts. But this requires a call to the servers, which may take some time. If you check Auth.auth().currentUser while the check is still in progress, you will get nil as the result.
To ensure you get the value once this initial check is done, use an auth state listener as shown in the first snippet in the documentation on getting the current user. From there:
Auth.auth().addStateDidChangeListener { auth, user in
// ...
}
Once this listener fires for the first time:
either the user variable will have a value if the user authentication state could be restored,
or it is nil if the user was not signed in last time the app ran, or the authentication state couldn't be restored (for example, because the account is disabled, or the password was changed).
I think it a big possibility you might be signed in on another device or signed in a different profile and didn't know, you might have some sync problems or security issues that did a safety measurement until you fix the security issue
This is how users navigate through my app:
Enter a phone number
Enter verification number (This allows them access to my Firestore)
Enters a username
Now they're in the main app
Pretty straightforward however the problem is if the app crashes (for whatever reason) or closes when they are entering the username, they are still technically signed in to my Firestore.
It's a problem because I programmed the app such that when the user reopens the app, they immediately go to the main screen (I created an app manager to handle this). If they close the app (or if it crashes) before they enter a username, they can go right to the main screen without a username.
I've made several different attempts in the applicationWillTerminate method to track which view controller the user is in, sign out so that when the user open the app again, my App Manager says, Auth.auth().currentUser == nil and goes to the initial view controller to start the registration process again.
var viewController : UIViewController?
var vc = UsernameViewController()
if viewController == vc {
let firebaseAuth = Auth.auth()
do {
try firebaseAuth.signOut()
} catch let signOutError as NSError {
print ("Error signing out: %#", signOutError)
}
I expect the user who did not get a chance to create a username because the app closed or crashed to to return to the initial view controller rather than the main screen of the app.
I am currently experiencing the opposite.
This question already has answers here:
Why does viewWillAppear not get called when an app comes back from the background?
(7 answers)
Closed 4 years ago.
I am working on project that needs to load the users contacts. For this I need the contacts information. Thus I am using Contacts Framework. It is easy to use and really very fast also. I am new to iOS so I just used the code snippet of getting contacts information.
What I have done: But I have problem, and that is when the user install the application and go the respective ViewController, the ViewController shows Dialog for permission. There user can Deny and Allow the permission. My app works well when user allows the permission but does not work in other way. So I used a function to check if user has given my app the permission or not.
So I read that when user has not granted the permission we can not do anything. Except we can take him to settings where he can allow the permission and get back the app. here is the code I am using to go to the settings app.
if let appSettings = URL(string: UIApplicationOpenSettingsURLString + Bundle.main.bundleIdentifier!) {
if UIApplication.shared.canOpenURL(appSettings) {
UIApplication.shared.open(appSettings)
}
}
Problem:
now my problem is critical, and that is I know I can take user to
settings view, now what if user still do not allow the Permission and
just get back to our App, in this case how to check if user has given
us permission or not??
I am new to iOS and swift so please help me through example. I have searched a lot but did not find anything. In Android there are callback and also onResume could be used, but in iOS I used ViewWillAppear thinking as equivalent of onResume but that did not work.
If you had a refresh button you could do something like the following:
#IBAction func refreshButtonPressed(_ sender: UIButton) {
ContactsService.sharedInstance.CNContactStore.requestAccess(to: .contacts, completion: { granted, error in
if !granted {
//TODO: - Do something (e.g)
self.[method to display an access denied alert to the user]
return
} else {
[insert your code here]
}
}
Also, as another user has stated in a reply to your original question: Why does viewWillAppear not get called when an app comes back from the background? may be of interest to you. You may want to call the contents of the ContactsService.sharedInstance.CNContactStore.requestAccess( .... { } block inside a function and hook that up to a listener for the UIApplication.willEnterForegroundNotification
https://developer.apple.com/documentation/contacts
https://developer.apple.com/documentation/contacts/cncontactstore
https://developer.apple.com/documentation/contacts/cncontactstore/1402873-requestaccess
This question already has answers here:
How to maintain user session after exiting app in Firebase
(2 answers)
Closed 2 years ago.
I have been looking around for some ways to have my app remember that a user is signed in but could find solutions for Firebase 3. I can login just fine with Firebase authentication but when I quit the app it takes me again to the login. This is closest solution I feel like I have. code snippet
This is what I followed from a youtube tutorial.
Thanks!
You stay logged in with firebase authentication, there's no need to try and re-login users. As long as the user doesn't log out, even if he kills the app, the next time he uses the app, he's automatically logged in. If you want to check if a user is logged in or not, this line of code will get you what you want:
let loggedIn = Auth.auth().currentUser == nil
if loggedIn is true then don't go to login page
If you want to listen to the authentication state of a user, just add an auth state listener after firebase configuration in AppDeledate like so:
FirebaseApp.configure()
Auth.auth().addStateDidChangeListener { (auth, user) in
if let currentUser = user {
// Do something with the current user, for example, fetch user info
// Suppose you have a "users" table in your database and each
// user is distinguished by their 'uid' property, then
Database.database().reference()
.child("users/currentUser.uid")
.observe(.value, with: { snapshot in ... })
}
}
Whenever the auth state changes, this code will be executed
BTW, this auth state listener is very helpful when you want to re-render some screens whenever the auth state changes. You can post a notification in the code above using NotificationCenter.default.post(...), and receive the notifications in your view controllers
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???