I am new to iOS and am having trouble with app delegate URL handling in Swift 3, and I could really use some pointers.
The below code works perfectly fine in Swift 2
For swift 5 Xcode 10.3 (Latest google signin)
@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
let googleDidHandle = GIDSignIn.sharedInstance().handle(url)
return googleDidHandle
}
func application(_ application: UIApplication,open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
let googleDidHandle = GIDSignIn.sharedInstance().handle(url)
return googleDidHandle
}
To open signIn screen on Button click
@IBAction func btnGoogleSigninClick(_ sender: AnyObject) {
GIDSignIn.sharedInstance()?.presentingViewController = self
GIDSignIn.sharedInstance()?.restorePreviousSignIn()
GIDSignIn.sharedInstance().signIn()
}
Xcode 8 Swift 3
If you are using multiple URL Schemes along with Google Sign In, use it like this:
func application(_ app: UIApplication,
open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if(url.scheme!.isEqual("fbXXXXXXXXXXX")) {
return SDKApplicationDelegate.shared.application(app, open: url, options: options)
} else {
return GIDSignIn.sharedInstance().handle(url as URL!,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String!,
annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
}
Try to perform any operations at function "signIn" in AppDelegate.
After Google sends you some data:
func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
withError error: NSError!) {
if (error == nil) {
// Perform any operations on signed in user here.
let userId = user.userID
// Perform some operation to change the scene.
// ........
}
else {
print("\(error.localizedDescription)")
}
}
Maybe the code below can give you some idea:
let myStoryBoard:UIStoryboard = UIStoryboard(name:"Main", bundle:nil)
let protectedPage = myStoryBoard.instantiateViewControllerWithIdentifier("ProtectedPageViewController") as! ProtectedPageViewController
let protectedPageNav = UINavigationController(rootViewController: protectedPage)
let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = protectedPageNav
??? I reference from YouTube's video "Sign-in with Google Account button. Example in Swift. Video 2", that are provided from "Sergey Kargopolov".
and...
I have a question similar to this and didn't solve it...
Good luck! :)
Swift 4, Xcode 9
If you are using multiple URLSchemes for facebook and Google SignIn:
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
let fbSignIn = FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
let googleSignIn = GIDSignIn.sharedInstance().handle(url, sourceApplication: sourceApplication, annotation: annotation)
return fbSignIn || googleSignIn
}
While n.by.n answer is okay I saw a better answer in another thread (Swift 2.3):
if GIDSignIn.sharedInstance().handle(url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: options[UIApplicationOpenURLOptionsKey.annotation]) {
return true
} else if something else {
return true
} else {
return false
}