2016-06-06 23 views
7

Desidero effettuare l'accesso automatico all'utente se è già firmato e basta andare alla vista principale ma il codice è in esecuzione due volte e si può vedere la transistione invece della vista che mostra. Come lo aggiusto?Swift: accesso automatico utente con Firebase

AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    // Override point for customization after application launch.slideMenuController 
    FIRApp.configure() 
    FIRAuth.auth()?.addAuthStateDidChangeListener { 
     auth, user in 
     if user != nil { 
      // User is signed in. 
      print("Automatic Sign In: \(user?.email)") 

      let storyboard = UIStoryboard(name: "Main", bundle: nil) 
      let initialViewController = storyboard.instantiateViewControllerWithIdentifier("EmployeeRevealViewController") 
      self.window!.rootViewController = initialViewController 

     } else { 
      // No user is signed in. 
     } 
    } 

    return true 
} 

Log

2016-06-06 01:00:55.585 Untitled[13009:6258910] Configuring the default app. 
2016-06-06 01:00:55.657 Untitled[13009:] <FIRAnalytics/INFO> Firebase Analytics v.3200000 started 
2016-06-06 01:00:55.666 Untitled[13009:] <FIRAnalytics/INFO> To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled 
2016-06-06 01:00:55.714 Untitled[13009:6258910] Firebase Crash Reporting: Successfully enabled 
2016-06-06 01:00:55.739: <FIRInstanceID/WARNING> FIRInstanceID AppDelegate proxy enabled, will swizzle app delegate remote notification handlers. To disable add "FirebaseAppDelegateProxyEnabled" to your Info.plist and set it to NO 
2016-06-06 01:00:55.739: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)" 
2016-06-06 01:00:55.760: <FIRMessaging/INFO> FIRMessaging library version 1.1.0 
2016-06-06 01:00:55.781: <FIRMessaging/WARNING> FIRMessaging AppDelegate proxy enabled, will swizzle app delegate remote notification receiver handlers. Add "FirebaseAppDelegateProxyEnabled" to your Info.plist and set it to NO 
2016-06-06 01:00:55.788 Untitled[13009:] <FIRAnalytics/INFO> Successfully created Firebase Analytics App Delegate Proxy automatically. To disable the proxy, set the flag FirebaseAppDelegateProxyEnabled to NO in the Info.plist 
Automatic Sign In: Optional("[email protected]") 
2016-06-06 01:00:56.759: <FIRInstanceID/WARNING> APNS Environment in profile: development 
Automatic Sign In: Optional("[email protected]") 
2016-06-06 01:00:57.811 Untitled[13009:] <FIRAnalytics/INFO> Firebase Analytics enabled 
+0

hai registrato i dati di autenticazione? – Shubhank

+0

segui questo documento da firebase: https://firebase.google.com/docs/auth/ios/google-signin#2_implement_google_sign-in –

+0

Ho seguito quel documento. Funziona, ma il codice viene eseguito due volte come si può vedere alla fine del registro. E poi c'è una transizione dalla schermata iniziale alla schermata principale –

risposta

6

Prova:

if let alreadySignedIn = FIRAuth.auth()?.currentUser { 
    // segue to main view controller 
} else { 
    // sign in 
} 
+2

E ricorda di non inserire il codice in 'viewDidLoad()', ma piuttosto in 'viewDidAppear'. Ho commesso questo errore e non ho capito perché il seque non è andato in porto nonostante fosse stato chiamato. – oey

+0

Cosa succede quando l'utente chiude e riapre l'app? –

2

Per quanto riguarda la documentazione aggiornata, questo è il modo Recommened di farlo sulla base della documentazione Firebase e ha funzionato per me:

if Auth.auth().currentUser != nil { 
    // User is signed in. 
    // ... 
} else { 
    // No user is signed in. 
    // ... 
} 

Firebase cambiato ma la loro convenzione di denominazione, la maggior parte noticably:

FIRAuth ottenuto rinominato Auth

anche per i migliori risultati, ho messo questo all'interno del viewDidAppear() in questo modo:

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(true) 
    if Auth.auth().currentUser != nil { 
     performSegue(withIdentifier: "yourIdentifier", sender: self) 
    } 
} 
0

Senza identificativo per Firebase 4 e swift 4, ha funzionato bene per me ...

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(true) 
    if Auth.auth().currentUser != nil { 
     let newViewController: YourViewController = YourViewController() 
     self.present(newViewController, animated: true, completion: nil) 
    } 
} 
Problemi correlati