使用Google登录,将登录视图控制器放在iOS Swift中的AppDelegate而不是Appdelegate。

4

我能否在登录视图控制器的按钮操作中实现Google登录功能?

现在,我们正在使用Google登录SDK在AppDelegate中实现,并使用另一个视图控制器来查看该登录按钮。

除了在AppDelegate中实现此功能外,我们是否能够在登录视图控制器中实现并在按钮操作中处理Google函数?

我刚开始在iOS中实现SDK。如果有任何帮助将非常有用。


你可以从任何地方调用Google的默认登录页面,但是你无法更改Google的默认登录页面以进行登录。 - Rajan Maheshwari
旧方法中是否还有其他已实现的示例? - Sridhar G.K
从那里我可能会有一个解决方案。 - Sridhar G.K
我已经发布了一个解决方案。 - Rajan Maheshwari
2个回答

5
如果您想在使用v5.0.2和Swift 5的Google登录过程中使用自定义视图控制器,请执行以下操作...
按照说明下载并安装GoogleSignIn工具包到您的应用程序。请参见开始将Google Sign-In集成到iOS应用程序中
在您的 AppDelegate.swift 文件中,执行以下操作:
  1. Add the following to the import portion:

    import GoogleSignIn
    
  2. Add the following lines at the bottom of this function:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
        // other code here...
    
        // Initialize Google sign-in
        GIDSignIn.sharedInstance().clientID = "YOUR_CLIENT_ID"
    
        return true
    }
    
  3. Make sure you REMOVE this line from your AppDelegate.swift file:

    GIDSignIn.sharedInstance().delegate = self // REMOVE THIS LINE!
    

    Also, DO NOT use GIDSignInDelegate in your AppDelegate.swift class header!

  4. Add this function, if it does not already exist and add the following inside:

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        return GIDSignIn.sharedInstance().handle(url)
    }
    

    If your app uses Facebook and Google to sign in, use this:

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        var flag: Bool = false
        if ApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) {
    
            // Handle Facebook URL scheme
            flag = ApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
        } else {
    
            // Handle Google URL scheme
            flag = GIDSignIn.sharedInstance().handle(url)
        }
        return flag
    }
    
你的 AppDelegate.swift 文件已完成!接下来,在自定义登录视图控制器 LogInViewController.swift 上执行以下操作,如下所示:
  1. Add the following to the import portion:

    import GoogleSignIn
    
  2. Add the GIDSignInDelegate to your class header like this:

    class LoginViewController: UIViewController, UITextFieldDelegate, UINavigationControllerDelegate, GIDSignInDelegate {
    
        // your class code here...
    
    }
    
  3. In your override func viewDidLoad(), add the following on the bottom:

    GIDSignIn.sharedInstance().delegate = self
    
  4. To conform with GIDSignInDelegate, make sure to add the following functions:

    // Retrieve user profile details from Google
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if (error == nil) {
    
            // perform your log in functions here...
    
            // ex. assign user profile data to variables
            self.appUser.email = user.profile.email
            self.appUser.firstname = user.profile.givenName
            self.appUser.lastname = user.profile.familyName
    
            // etc.
    
            // Sign out of Google when logic completes for security purposes
            GIDSignIn.sharedInstance().signOut()
    
        } else {
            print(error.localizedDescription)
        }
    }
    

    And also add this function to comply with the delegate:

    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
        if let error = error {
            print(error.localizedDescription)
        }
    }
    

只需要这样就可以创建一个自定义视图控制器了。我没有AppDelegate.swift中使用GIDSignInDelegate或其委托函数,事实上,我将它们全部删除了。应用程序完美地执行。我希望这能帮助到某个人!

只是回馈StackOverflow社区为我所做的一切。


5
我已经发布了完整的解决方案,用于iOS 9和iOS 8的Google +登录目的,并且已经注意到Google登录流程不会在Safari浏览器中移动到应用程序之外,这是Appstore拒绝应用程序的非常普遍的原因。

以下是链接
使用google sdk v3.x从appstore拒绝Google+登录iOS应用程序
您只需要在自己的自定义视图控制器中实现,而不是在AppDelegate中执行。因此,根据您的问题,您的自定义视图控制器将是LoginViewController。

哇,超级棒的解决方案!我就期待着这样的解决方案。 - Sridhar G.K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接