2016-06-06 22 views
7

Ich möchte den Benutzer automatisch anmelden, wenn er bereits signiert ist und nur zur Hauptansicht gehen, aber der Code läuft zweimal und Sie können den Übergang statt der gerade angezeigten anzeigen. Wie repariere ich es?Swift: Auto-Login Benutzer mit 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

haben Sie die Auth-Daten protokolliert? – Shubhank

+0

Befolgen Sie dieses Dokument von Firebase: https://firebase.google.com/docs/auth/ios/google-signin#2_implement_google_sign-in –

+0

Ich bin diesem Dokument gefolgt. Es funktioniert, aber der Code läuft nur zweimal, wie Sie am Ende des Protokolls sehen können. Und dann gibt es einen Übergang vom Home-Bildschirm zum Hauptbildschirm –

Antwort

6

Versuchen:

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

Und denken Sie daran, den Code nicht in 'viewDidLoad()' sondern in 'viewDidAppear' zu schreiben. Ich machte diesen Fehler und wusste nicht, warum die Seque nicht durchging, obwohl sie aufgerufen wurde. – oey

+0

Was passiert, wenn der Benutzer die App schließt und wieder öffnet? –

2

Was die Dokumentation aktualisiert, ist dies die Recommened Weg, es zu tun auf der Grundlage der Dokumentation Firebase und es hat für mich funktioniert:

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

Firebase geändert ein, aber ihre Namenskonvention, die meisten noticably:

FIRAuth bekam

Auch für die besten Ergebnisse Auth umbenannt, ich habe dies in der viewDidAppear() wie folgt aus:

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

Ohne Kennung für Firebase 4 und Swift 4, es hat gut für mich funktioniert ...

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