Google and Facebook Firebase login authentication using swift 3 in ios -
i want implement both login using google , login using facebook in view using firebase auth
my appdelegate.swift follows:
class appdelegate: uiresponder, uiapplicationdelegate {
func application(_ application: uiapplication, didfinishlaunchingwithoptions launchoptions: [uiapplicationlaunchoptionskey: any]?) -> bool { // override point customization after application launch. firebaseapp.configure() fbsdkapplicationdelegate.sharedinstance().application(application, didfinishlaunchingwithoptions: launchoptions) return true } func application(_ application: uiapplication, open url: url, sourceapplication: string?, annotation: any) -> bool { let handled = fbsdkapplicationdelegate.sharedinstance().application(application, open: url, sourceapplication: sourceapplication, annotation: annotation) return handled } func application(_ app: uiapplication, open url: url, options: [uiapplicationopenurloptionskey : any] = [:]) -> bool { return gidsignin.sharedinstance().handle(url, sourceapplication:options[uiapplicationopenurloptionskey.sourceapplication] as? string, annotation: [:]) }
}
viewcontroller.swift as
class viewcontroller: uiviewcontroller, gidsignindelegate, gidsigninuidelegate {
override func viewdidload() { super.viewdidload() gidsignin.sharedinstance().uidelegate = self gidsignin.sharedinstance().clientid = firebaseapp.app()?.options.clientid gidsignin.sharedinstance().delegate = self } @ibaction func gsigninpressed(_ sender: any) { print("gsignin pressed") gidsignin.sharedinstance().signin() } @ibaction func fbpressed(_ sender: any) { let facebooklogin = fbsdkloginmanager() facebooklogin.login(withreadpermissions: ["email", "public_profile"], from: self) { (result, error) in if error != nil { print("uhhhh! unable connect facebook") } else if result?.iscancelled == true { print("uhhh! user cancelled fb auth") } else { print("uhhh! sucessfully authenticated fb") let credential = facebookauthprovider.credential(withaccesstoken: fbsdkaccesstoken.current().tokenstring) self.firebaseauth(credential) } } } func firebaseauth(_ credential: authcredential) { auth.auth().signin(with: credential) { (user, error) in if error != nil { // ... print("error in credentials") print(error!) return } // user signed in // ... let email = user?.email print(email!) } } func sign(_ signin: gidsignin!, didsigninfor user: gidgoogleuser!, witherror error: error!) { if error != nil { print("error in google sign in") return } guard let authentication = user.authentication else { return } let credential = googleauthprovider.credential(withidtoken: authentication.idtoken,accesstoken: authentication.accesstoken) self.firebaseauth(credential) }
}
//there
//mark: - handle url fb , google sign - func application(_ application: uiapplication, open url: url, sourceapplication: string?, annotation: any) -> bool { //handle url application receives @ end of authentication process - var flag: bool = false // handle facebook url scheme if let washandled:bool = fbsdkapplicationdelegate.sharedinstance().application(application, open: url, sourceapplication: sourceapplication, annotation: annotation) { flag = washandled } // handle google url scheme if let googleplusflag: bool = gidsignin.sharedinstance().handle(url, sourceapplication: sourceapplication!, annotation: annotation) { flag = googleplusflag } return flag }
try in appdelegate
Comments
Post a Comment