After authenticating a user with the following code (below is a trimmed version of my code, so only the successful login logic is shown)...
let firebaseReference = Firebase(url: "https://MY-FIREBASE.firebaseio.com")
FBSession.openActiveSessionWithReadPermissions(["public_profile", "user_friends"], allowLoginUI: true,
completionHandler: { session, state, error in
if state == FBSessionState.Open {
let accessToken = session.accessTokenData.accessToken
firebaseReference.authWithOAuthProvider("facebook", token: accessToken,
withCompletionBlock: { error, authData in
if error != nil {
// Login failed.
} else {
// Logged in!
println("Logged in! \(authData)")
}
})
}
})
}
(I.e. Launching and running the app, logging in successfully).
If you then delete the app and reinstall it on the same device, this call - which I am using in the app delegate to determine if a user is logged in - will always return that they are logged in.
if firebaseReference.authData == nil {
// Not logged in
} else {
// Logged in
}
Why is that? I would have thought deleting the app and reinstalling it should wipe all data.
If you reset the Content and Settings in the iOS simulator, and the install the app, the firebaseReference.authData property will once again be nil.
The Firebase authentication session is persisted on the user's device in the iOS keychain. The keychain data for the application is not removed when the application is uninstalled.
If you're looking to manually clear the data, you can store some additional metadata along with your application and manually call FirebaseRef.unauth() to clear the persisted session. See #4747404: Delete keychain items when an app is uninstalled for an additional reference.
Adding below code at the end of didFinishLaunchingWithOptions function (before return true) of AppDelegate works swiftly.
Swifty way
let userDefaults = UserDefaults.standard
if userDefaults.value(forKey: "appFirstTimeOpend") == nil {
//if app is first time opened then it will be nil
userDefaults.setValue(true, forKey: "appFirstTimeOpend")
// signOut from Auth
do {
try Auth.auth().signOut()
}catch {
}
// go to beginning of app
} else {
//go to where you want
}
Swift 3.*
let userDefaults = NSUserDefaults.standardUserDefaults()
if userDefaults.valueForKey("appFirstTimeOpend") == nil {
//if app is first time opened then it will be nil
userDefaults.setValue(true, forKey: "appFirstTimeOpend")
// signOut from FIRAuth
do {
try FIRAuth.auth()?.signOut()
}catch {
}
// go to beginning of app
} else {
//go to where you want
}
For swift 4 the same Answer:
let userDefaults = UserDefaults.standard
if userDefaults.value(forKey: "appFirstTimeOpend") == nil {
//if app is first time opened then it will be nil
userDefaults.setValue(true, forKey: "appFirstTimeOpend")
// signOut from FIRAuth
do {
try Auth.auth().signOut()
}catch {
}
// go to beginning of app
} else {
//go to where you want
}
Use below extension :
extension AppDelegate{
func signOutOldUser(){
if let _ = UserDefaults.standard.value(forKey: "isNewuser"){}else{
do{
UserDefaults.standard.set(true, forKey: "isNewuser")
try Auth.auth().signOut()
}catch{}
}
}
}
and call this in '...didFinishLaunchingWithOptions...' method of Appdelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
signOutOldUser()
return true
}
Related
I want user to login once and not have to reenter their login info everytime they open app unless they logout in the last session.
Login screen is currently displayed everytime the app is open. This is my rootview
struct AppRootView: View {
var body: some View {
AnyView {
// check if user has already logged in here and then route them accordingly
if auth.token != nil {
homeMainView()
} else {
LoginController()
}
}
}
}
currently this is what I use to login users
#objc func signUp() {
setLoading(true);
app.usernamePasswordProviderClient().registerEmail(username!, password: password!, completion: {[weak self](error) in
// Completion handlers are not necessarily called on the UI thread.
// This call to DispatchQueue.main.sync ensures that any changes to the UI,
// namely disabling the loading indicator and navigating to the next page,
// are handled on the UI thread:
DispatchQueue.main.sync {
self!.setLoading(false);
guard error == nil else {
print("Signup failed: \(error!)")
self!.errorLabel.text = "Signup failed: \(error!.localizedDescription)"
return
}
print("Signup successful!")
// Registering just registers. Now we need to sign in, but we can reuse the existing username and password.
self!.errorLabel.text = "Signup successful! Signing in..."
self!.signIn()
}
})
}
#objc func signIn() {
print("Log in as user: \(username!)");
setLoading(true);
app.login(withCredential: AppCredentials(username: username!, password: password!)) { [weak self](maybeUser, error) in
DispatchQueue.main.sync {
self!.setLoading(false);
guard error == nil else {
// Auth error: user already exists? Try logging in as that user.
print("Login failed: \(error!)");
self!.errorLabel.text = "Login failed: \(error!.localizedDescription)"
return
}
guard let user = maybeUser else {
fatalError("Invalid user object?")
}
print("Login succeeded!");
//
let hostingController = UIHostingController(rootView: ContentView())
self?.navigationController?.pushViewController(hostingController, animated: true)
}
how could I implement one time login so that users do have to login each time they open the app?
A correctly configured and initialized RealmApp class will persist the session information for you between app restarts, you can check for an existing session using the .currentUser() method from this class. So in your case something like:
if app.currentUser() != nil {
homeMainView()
} else {
LoginController()
}
While using Realm to persist login is a good idea, but I would highly
advice against using it for managing user authentication credentials such
as passwords. A better approach if you want to save sensitive information is
using KeyChain just like what Apple and password manager apps do. With a light
weight keyChain wrapper library such as SwiftKeychainWrapper You can easily
save your login credentials in the most secure way.
Here is a sample using a keyChain wrapper linked above.
With simple modification you can use this helper class to manage your sign in credentials anywhere in your app.
import SwiftKeychainWrapper
class KeyChainService {
// Make a singleton
static let shared = KeyChainService()
// Strings which will be used to map data in keychain
private let passwordKey = "passwordKey"
private let emailKey = "emailKey"
private let signInTokenKey = "signInTokenKey"
// Saving sign in info to keyChain
func saveUserSignInInformation(
email: String,
password: String,
token: String
onError: #escaping() -> Void,
onSuccess: #escaping() -> Void
) {
DispatchQueue.global(qos: .default).async {
let passwordIsSaved: Bool = KeychainWrapper.standard.set(password, forKey: self.passwordKey)
let emailIsSaved: Bool = KeychainWrapper.standard.set(email, forKey: self.emailKey)
let tokenIsSaved: Bool = KeychainWrapper.standard.set(token, forKey: self.signInTokenKey)
DispatchQueue.main.async {
// Verify that everything is saved as expected.
if passwordIsSaved && emailIsSaved && tokenIsSaved {
onSuccess()
}else {
onError()
}
}
}
}
// Retrieve signIn information for auto login
func retrieveSignInInfo(onError: #escaping() -> Void, onSuccess: #escaping(UserModel) -> Void) {
DispatchQueue.main.async {
let retrievedPassword: String? = KeychainWrapper.standard.string(forKey: self.passwordKey)
let retrievedEmail: String? = KeychainWrapper.standard.string(forKey: self.emailKey)
let retrievedToken: String? = KeychainWrapper.standard.string(forKey: self.signInTokenKey)
if let password = retrievedPassword,
let email = retrievedEmail,
let token = retrievedToken {
// Assuming that you have a custom user model named "UserModel"
let user = UserModel(email: email, password: password,token: token)
// Here is your user info which you can use to verify with server if needed and auto login user.
onSuccess(user)
}else {
onError()
}
}
}
}
I'm trying and failing badly to implement the cool Firebase email link login feature. I successfully setup sending an email link. However, I can't get the email link to open up the app. It just opens up the preview page like it can't open the app.
I've tested the dynamic link I setup and I can get it to open up the app in a device. I just can't get the email link to do the same.
Code in my app:
func sendFirebaseEmailLink() {
let actionCodeSettings = ActionCodeSettings.init()
// userEmail comes from a textField
let email = userEmail
actionCodeSettings.url = URL.init(string: String(format: "https://<myappname>.firebaseapp.com/?email=%#", email))
// The sign-in operation has to always be completed in the app.
actionCodeSettings.handleCodeInApp = true
actionCodeSettings.setIOSBundleID(Bundle.main.bundleIdentifier!)
Auth.auth().sendSignInLink(toEmail: email,
actionCodeSettings: actionCodeSettings) { error in
if let error = error {
print(error.localizedDescription)
return
}
else {
UserDefaults.standard.set(email, forKey: "Email")
print("email sent to user")
}
}
}
When I say I've successfully gotten my dynamic link to open the app what I mean is when I follow the link I created (mylinkname.page.link/emaillogin) on a device that has the app installed, it opens the app. Because of that and [this helpful Firebase video][1] on setting up a dynamic link it seems like I've got those details correct and the issue is with the code, but I'm new to this so I'm not sure.
I've spend few days going around in circles to figure this out, and trying to parse the dense Firebase documentation, so any ideas are greatly appreciated.
I finally figured it out. The code was fine. It was an issue related to the dynamic link. I had a couple links setup in Firebase because I had to create a new Bundle ID at one point. When I deleted out the old one in Firebase the email link started working.
It shows up in my app association site like this, and oddly still does even though I deleted out the old link, but at least it works now!
{"applinks":{"apps":[],"details":[{"appID":"TEAMID.com.OLDBUNDLEIDENTIFIER.APPNAME","paths":["NOT //*","/*"]},{"appID":"TEAMID.com.NEWBUNDLEIDENTIFIER.APPNAME","paths":["NOT //","/"]}]}}
UPDATE: My full code to implement passwordless email login is below. It was painful for me to piece together using the documentation so hopefully this saves you the trouble.
Key steps assuming you understand the basics of Firebase Setup.
1) Setup a Dynamic Link Using the Firebase Video tutorial.
2) Code in View Controller:
var userEmail: String?
var link: String?
func sendFirebaseEmailLink() {
let actionCodeSettings = ActionCodeSettings.init()
let email = userEmail
actionCodeSettings.url = URL.init(string: String(format: "https://<myappname>.page.link/emaillogin/?email=%#", email!))
// The sign-in operation has to always be completed in the app.
actionCodeSettings.handleCodeInApp = true
actionCodeSettings.setIOSBundleID(Bundle.main.bundleIdentifier!)
Auth.auth().sendSignInLink(toEmail: email!,
actionCodeSettings: actionCodeSettings) { error in
if let error = error {
print(error.localizedDescription)
return
}
else {
UserDefaults.standard.set(email, forKey: "Email")
print("email sent to user")
}
// TODO: Notify user to check email and click the link.
}
}
// Sign in user after they clicked email link called from AppDelegate
#objc func signInUserAfterEmailLinkClick() {
// Get link url string from the dynamic link captured in AppDelegate.
if let link = UserDefaults.standard.value(forKey: "Link") as? String {
self.link = link
}
// Sign user in with the link and email.
Auth.auth().signIn(withEmail: userEmail!, link: link!) { (result, error) in
if error == nil && result != nil {
if (Auth.auth().currentUser?.isEmailVerified)! {
print("User verified with passwordless email")
// TODO: Do something after user verified like present a new View Controller
}
else {
print("User NOT verified by passwordless email")
}
}
else {
print("Error with passwordless email verfification: \(error?.localizedDescription ?? "Strangely, no error avaialble.")")
}
}
}
3) Code in AppDelegate
// For Passwordless Email Login to Handle Dynamic Link after User Clicks Email Link
func application(_ application: UIApplication, continue userActivity: NSUserActivity,
restorationHandler: #escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if let incomingURL = userActivity.webpageURL {
print("Incoming URL is \(incomingURL)")
// Parse incoming
let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
guard error == nil else {
print("Found an error: \(error!.localizedDescription)")
return
}
if let dynamicLink = dynamicLink {
self.handleIncomingDynamicLink(dynamicLink)
}
}
if linkHandled {
return true
}
else {
// Maybe do other things with dynamic links in future?
return false
}
}
return false
}
// Handles the link and saves it to userDefaults to assist with login.
func handleIncomingDynamicLink(_ dynamicLink: DynamicLink) {
guard let url = dynamicLink.url else {
print("My dynamic link object has no url")
return
}
print("Incoming link parameter is \(url.absoluteString)")
let link = url.absoluteString
if Auth.auth().isSignIn(withEmailLink: link) {
// Save link to userDefaults to help finalize login.
UserDefaults.standard.set(link, forKey: "Link")
// Send notification to ViewController to push the First Time Login VC
NotificationCenter.default.post(
name: Notification.Name("SuccessfulPasswordlessEmailNotification"), object: nil, userInfo: nil)
}
}
For anyone using SwiftUI with AppDelegate and SceneDelegate files instead of UIKit, here's what I've done:
Create a function to send a link to the user's email
func sendSignLink(email: String) async throws {
do {
let actionCodeSettings = ActionCodeSettings()
actionCodeSettings.url = URL(string: "*enter your Firebase Dynamic link here*")
actionCodeSettings.handleCodeInApp = true
actionCodeSettings.setIOSBundleID(Bundle.main.bundleIdentifier!)
try await Auth.auth().sendSignInLink(toEmail: email, actionCodeSettings: actionCodeSettings)
UserDefaults.standard.set(email, forKey: "email")
}
catch {
throw error
}
}
In the SceneDelegate file, import FirebaseDynamicLinks and add the below code
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
if let incomingURL = userActivity.webpageURL {
print("\n \nIncoming URL is \(incomingURL)")
_ = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
guard error == nil else {
print("\n \nError with handling incoming URL: \(error!.localizedDescription)")
return
}
if let dynamicLink = dynamicLink {
guard let url = dynamicLink.url else {
print("\n \nDynamic link object has no url")
return
}
print("\n \nIncoming link parameter is \(url.absoluteString)")
let link = url.absoluteString
if Auth.auth().isSignIn(withEmailLink: link) {
// Send notification to trigger the rest of the sign in sequence
NotificationCenter.default.post(name: Notification.Name("Success"), object: nil, userInfo: ["link": link])
} else {
// Send error notification
NotificationCenter.default.post(name: Notification.Name("Error"), object: nil, userInfo: nil)
}
}
}
}
}
Create a function to handle the sign in after the user has clicked on the link in their email
func signInWithEmail(link: String) async throws {
do {
let email = UserDefaults.standard.value(forKey: "email")
try await Auth.auth().signIn(withEmail: email, link: link)
}
catch {
throw error
}
}
In a relevant view, handle the notifications which get posted
struct MyView: View {
var body: some View {
VStack {
Text("View")
}
.onReceive(NotificationCenter.default.publisher(for: Notification.Name("Success"))) { notificationInfo in
if let userInfo = notificationInfo.userInfo {
if let link = userInfo["link"] as? String {
Task.init {
do {
try await signInWithEmail(link: link)
} catch {
print(error)
}
}
}
}
}
.onReceive(NotificationCenter.default.publisher(for: Notification.Name("Error"))) { _ in
//do something with error
}
}
}
I am trying to implement a function to delete current user's account on iOS. Account deletion works properly but the problem is that I cannot delete the account's data from Database and Storage when deleting an account.
"currentUser.delete" deletes the account but I think there is no authentication to delete its data from Database and Storage. Permission denied message shows up in the log. After running this function, I get to see the account is gone in Firebase Console Authentication page but data from Database and Storage persists.
Is this the correct way to delete an account?
I tried to delete data from Database and Storage before deleting the account. However, Firebase asks for re-authentication if session is more than 5 minutes old. Re-login shows empty data to the user before performing account deletion again so this is misleading and very confusing.
Please let me know how to remove data when deleting an account.
private func deleteAccount() {
guard let currentUser = Auth.auth().currentUser else {
return print("user not logged in")
}
currentUser.delete { error in
if error == nil {
// 1. Delete currentUser's data from Database. Permission denied
// 2. Delete currentUser's data from Storage. Permission denied
// present login screen (welcome page)
self.presentLoginScreen()
} else {
guard let errorCode = AuthErrorCode(rawValue: error!._code) else { return }
if errorCode == AuthErrorCode.requiresRecentLogin {
self.showMessage("Please re-authenticate to delete your account.", type: .error)
do {
try Auth.auth().signOut()
self.presentLoginScreen()
} catch {
print("There was a problem logging out")
}
}
}
}
}
Swift 5 | Firebase 8.11.0
To solve the problems that you've mentioned (delete the data before deleting the actual user and potentially get the AuthErrorCode.requiresRecentLogin error), you may use DispatchGroup and check the lastSignInDate, like this (just call deleteUserProcess()):
let deleteDataGroup = DispatchGroup()
func deleteUserProcess() {
guard let currentUser = Auth.auth().currentUser else { return }
deleteUserData(user: currentUser)
// Call deleteUser only when all data has been deleted
deleteDataGroup.notify(queue: .main) {
self.deleteUser(user: currentUser)
}
}
/// Remove data from Database & Storage
func deleteUserData(user currentUser: User) {
// Check if `currentUser.delete()` won't require re-authentication
if let lastSignInDate = currentUser.metadata.lastSignInDate,
lastSignInDate.minutes(from: Date()) >= -5 {
deleteDataGroup.enter()
Database.database().reference().child(userId).removeValue { error, _ in
if let error = error { print(error) }
self.deleteDataGroup.leave()
}
// Delete folders from Storage isn't possible,
// so list and run over all files to delete each one independently
deleteDataGroup.enter()
Storage.storage().reference().child(userId).listAll { list, error in
if let error = error { print(error) }
list.items.forEach({ file in
self.deleteDataGroup.enter()
file.delete { error in
if let error = error { print(error) }
self.deleteDataGroup.leave()
}
})
deleteDataGroup.leave()
}
}
}
/// Delete user
func deleteUser(user currentUser: User) {
currentUser.delete { error in
if let error = error {
if AuthErrorCode(rawValue: error._code) == .requiresRecentLogin {
reauthenticate()
} else {
// Another error occurred
}
return
}
// Logout properly
try? Auth.auth().signOut()
GIDSignIn.sharedInstance.signOut()
LoginManager().logOut()
// The user has been deleted successfully
// TODO: Redirect to the login UI
}
}
func reauthenticate() {
// TODO: Display some UI to get credential from the user
let credential = ... // Complete from https://stackoverflow.com/a/38253448/8157190
Auth.auth().currentUser?.reauthenticate(with: credential) { _, error in
if let error = error {
print(error)
return
}
// Reload user (to update metadata.lastSignInDate)
Auth.auth().currentUser?.reload { error in
if let error = error {
print(error)
return
}
// TODO: Dismiss UI
// Call `deleteUserProcess()` again, this time it will delete the user
deleteUserProcess()
}
}
}
The minuets function can be added in an extension to Date (thanks to Leo Dabus):
extension Date {
/// Returns the amount of minutes from another date
func minutes(from date: Date) -> Int {
return Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0
}
}
you can first make your specific user deleted and and its value through its UID then you can deleted user and take him to root view controller or login screen after deleting it.
// removing user data from firebase and its specific user id
let user = Auth.auth().currentUser
user?.delete { error in
if let error = error {
// An error happened.
print(error.localizedDescription)
} else {
Database.database().reference().child("users").child(user?.uid ?? "").removeValue()
self.navigationController?.popToRootViewController(animated: true)
// Account deleted and logout user
// do {
// try Auth.auth().signOut()
// take you to root
// self.navigationController?.popToRootViewController(animated: true)
}
I am using phone authentication using cloud firestore. In firestore db, I am storing user phone number and uid. Here is the code which, I have tried for mobile number login:
#IBAction func signUp(_ sender: Any) {
// dismiss keyboard
view.endEditing(true)
if sendOTP == false {
let mobileNumber = "+91" + phoneNumberTextField.text!
self.Userdefaults.set(mobileNumber, forKey: "mobileNumber")
print("mobileNumber::::\(mobileNumber)")
sendOTPCode()
sendOTP = true
} else {
let codestring = OTPCodeTextField.text
if codestring?.count == 6 {
loginusingOTP(OTPtext: codestring!)
} else {
print("Enter 6 digit code")
}
}
func sendOTPCode() {
let mymobilenumber = Userdefaults.string(forKey: "mobileNumber")
PhoneAuthProvider.provider().verifyPhoneNumber(mymobilenumber!) { (verificationID, error) in
self.Userdefaults.set(verificationID, forKey: "authVerificationID")
if error != nil
{
print ("insde SendCode, there is error")
print("error: \(String(describing: error?.localizedDescription))")
} else {
print ("code sent")
self.phoneNumberTextField.allowsEditingTextAttributes = false
}
}
}
func loginusingOTP(OTPtext: String) {
let db = Firestore.firestore()
let verificationID = self.Userdefaults.string(forKey: "authVerificationID")
let credential: PhoneAuthCredential = PhoneAuthProvider.provider().credential(withVerificationID: verificationID!,
verificationCode: OTPtext)
Auth.auth().signIn(with: credential)
{
(user, error) in
if error != nil
{
print("error: \(String(describing: error?.localizedDescription))")
}
else if user != nil
{
print("Phone number: \(String(describing: user?.phoneNumber))")
let userInfo = user?.providerData[0]
print("Provider ID: \(String(describing: userInfo?.providerID))")
var _: DocumentReference? = nil
print("currentUser:::\(String(describing: currentUser))")
db.collection("users").document(currentUser!).setData([
"User_Phone_number": user?.phoneNumber as Any,
"uid": currentUser as Any
]) { err in
if let err = err {
print("Error writing document: \(err)")
} else {
print("Document successfully written!")
if PrefsManager.sharedinstance.isFirstTime == false{
let when = DispatchTime.now() + 0
DispatchQueue.main.asyncAfter(deadline: when) {
self.performSegue(withIdentifier: "signUpToTabBar", sender: nil)
}
}else{
let when = DispatchTime.now() + 0
DispatchQueue.main.asyncAfter(deadline: when) {
let storyboard = UIStoryboard(name: "Start", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "onboardvc")
self.present(initialViewController, animated: true, completion: nil)
}
}
}
}
} else {
print("error::::::")
}
}
}
User login flow - First user enter phone number and then taps on send otp then user enters otp code, logged in successfully. Since user login first time, user needs to fill user detail page and then goes to home page. If its already logged in user, after successful of login user will redirected to home screen not the user detail page.
My question is now user is login each and every time to get inside of the app, I want user to login automatically without login each and every time unless user logout. How to check already logged in user UID or phone in cloud firestore for user exist or new user.
Any help much appreciated pls...
Let me give you brief scenario.
SwiftyUserDefaults is the best library to store UserDefaults throughout the app.
Create one extension like this,
extension DefaultsKeys {
static let username = DefaultsKey<String?>("username")
static let phoneNo = DefaultsKey<String?>("phoneNo")
static let islogin = DefaultsKey<Bool?>("islogin")
}
After successful login, you can set the values of the above DefaultKeys like below, first import SwiftyUserDefaults,
Defaults[.username] = Your_User_Name
Defaults[.phoneno] = Your_Phone_No
Defaults[.islogin] = true
Now on your first LoginViewController, in viewDidLoad() method, Please check following,
if Defaults[.islogin] == true {
//Go to Home with animation false
}
Here you go, let me know in case of any queries.
FYI. This is just the scenario, actual may be different depending on your final requirement. This answer may help you.
For Firebase (and it looks like FireStore). Once the User is Authenticated on the device, they will automatically be "logged in" to Firebase/Firestore next sessions unless there is a specific SignOut (Auth.auth().signOut()) or unless there is a super long delay (not sure how long, maybe a month).
To check to see if the user is already logged in.
On Start Up (didFinishLaunchingWithOptions) set up an Auth Listener and it will fire once and return the current Auth Status.
func addUserListener() {
listenHandler = Auth.auth().addStateDidChangeListener { (auth, user) in
if user == nil {
// We are Logged Out of Firebase.
// Move to Login Screen
} else {
// we are Logged In to Firebase.
// Move to Main Screen
}
}
You really need to use an Auth Listener for Login calls as if someone has two devices and logs out on one device, they will be logged out of firebase and the 2nd device will crash when you try a firebase call, because it still thinks its logged in.
Update your login credentials OR loggedIn flag to user Defaults
UserDefaults.standard.set("value", forKey: "username")
UserDefaults.standard.set("value", forKey: "password")
(NB: i recommended not to store password directly, you could store authentication token in Userdefaults, & if you want password to be stored, use keychain instead)
And redirect to corresponding page from AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var initialViewController: UIViewController?
let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let username = UserDefaults.standard.value(forKey: "username"), let password = UserDefaults.standard.value(forKey: "password") {
initialViewController = mainStoryboard.instantiateViewController(withIdentifier: "HomeVC")
} else {
initialViewController = mainStoryboard.instantiateViewController(withIdentifier: "LoginVC")
}
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
I would like to know what is the best way to bypass the login viewcontroller if the user is logged in already. I have integrated Facebook in my app. I tried to check for the FBSDKAccessToken.currentAccessToken if it is nil or not in the appdelegate class so the app could start either from the loginviewcontroller or not, but it is not working. This is what I tried so far.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if ((FBSDKAccessToken.currentAccessToken()) != nil) {
print("it is logged in")
}else{
print("it is not ")
}
// Override point for customization after application launch.
FBSDKLoginButton.classForCoder()
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
Either the user is logged in or not I get the same message.
What am I doing wrong? Thank you.
NOTE:
I don't know how to solve the issue in your code, but there is another way of doing this, which I consider better!
LOGIN:
SWIFT 2:
You can add this code to the function that does things if the user login attempt was successful:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue("loggedin", forKey: "yourKey")
And then, in AppDelegate.swift add this code, inside ApplicationDidFinishLaunchingWithOptions:
let defaults = NSUserDefaults.standardUserDefaults()
if defaults.valueForKey("yourKey") != nil{
let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
self.window.rootViewController = viewController!
}
LOGOUT:
In the logout function, add this:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue("loggedout", forKey: "yourKey")
And change the if-statement in didFinishLaunching... to this:
if defaults.valueForKey("yourKey") != nil{
let vc = UIViewController()
let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
let value = defaults.valueForKey("yourKey") as! String!
if value == "loggedin"{
vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController!
}
else if value == "loggedout"{
vc = storyboard.instantiateViewControllerWithIdentifier("loginViewController") as! UIViewController!
}
self.window.rootViewController = viewController!
}
Hope This Helps!
I solved it by moving the following code
if ((FBSDKAccessToken.currentAccessToken()) != nil) {
print("user is logged in")
}else{
print("user is not ")
}
to my
func applicationDidBecomeActive(application: UIApplication) {}
Thank you for your replies.
You cannot check if the user is logged in device facebook app. You can only check if there is a valid access token or not. If there is one, you don't have to prompt user to login again.Because if accessToken is valid, you can get the user info . Again the currentAccessToken has nothing to do whether you are logged in your phone/computer facebook app or not.
You should always depend on the currentAccessToken.
if ((FBSDKAccessToken.currentAccessToken()) != nil) {
print("user is logged in")
}else{
print("user is not ")
}
If the current access token is nil which is expected by default, you need to initiate the login flow through your app. Please refer to the documentation. Once you have finished the login flow and come back to the app, you will then have a valid access token.
In your case, the access token is nil because you are calling it before the FBSDKApplicationDelegate properly set.
You should do the access token check only after the call to didFinishLaunchingWithOptions is finished. This reason explained in here.