Skip to content

Instantly share code, notes, and snippets.

@charleshkang
Created February 11, 2017 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charleshkang/fbe2812f96740ee3d79a44201d1aa4fa to your computer and use it in GitHub Desktop.
Save charleshkang/fbe2812f96740ee3d79a44201d1aa4fa to your computer and use it in GitHub Desktop.
coffee mapper app delegate 2/11
// AppDelegate.swift
// Coffee Mapper
//
// Created by Charles Kang on 4/13/16.
// Copyright © 2016 Charles Kang. All rights reserved.
//
import UIKit
import Firebase
import Realm
import GoogleSignIn
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
var window: UIWindow?
var databaseRef: FIRDatabaseReference!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()?.options.clientID
GIDSignIn.sharedInstance().delegate = self
// if NSUserDefaults.standardUserDefaults().valueForKey("uid") == nil {
// let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("signInIdentifier")
// window?.rootViewController = loginVC
// } else if NSUserDefaults.standardUserDefaults().valueForKey("uid") != nil && DataService.dataService.CURRENT_USER_REF.authData != nil {
// let homeVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("homeIdentifier")
// let navigationController = UINavigationController(rootViewController: homeVC)
// window?.rootViewController = navigationController
// }
// let config = Realm.Configuration(
// schemaVersion: 3,
// migrationBlock: { migration, oldSchemaVersion in
// if (oldSchemaVersion < 1) {
// }
// })
// Realm.Configuration.defaultConfiguration = config
// _ = try! Realm()
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return GIDSignIn.sharedInstance().handle(url,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
print(error.localizedDescription)
return
}
print("User signed into google")
let authentication = user.authentication
let credential = FIRGoogleAuthProvider.credentialWithIDToken(authentication.idToken, accessToken: authentication.accessToken)
FIRAuth.auth()?.signIn(with: credential) { (user, error) in
print("User Signed Into Firebase")
self.databaseRef = FIRDatabase.database().reference()
self.databaseRef.child("user_profiles").child(user!.uid).observeSingleEvent(of: .value, with: { (snapshot) in
let snapshot = snapshot.value as? NSDictionary
if(snapshot == nil)
{
self.databaseRef.child("user_profiles").child(user!.uid).child("name").setValue(user?.displayName)
self.databaseRef.child("user_profiles").child(user!.uid).child("email").setValue(user?.email)
}
let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main", bundle: nil)
self.window?.rootViewController?.performSegue(withIdentifier: "HomeViewSegue", sender: nil)
})
}
}
func sign(signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment