我能否在登录视图控制器的按钮操作中实现Google登录功能?
现在,我们正在使用Google登录SDK在AppDelegate中实现,并使用另一个视图控制器来查看该登录按钮。
除了在AppDelegate中实现此功能外,我们是否能够在登录视图控制器中实现并在按钮操作中处理Google函数?
我刚开始在iOS中实现SDK。如果有任何帮助将非常有用。
我能否在登录视图控制器的按钮操作中实现Google登录功能?
现在,我们正在使用Google登录SDK在AppDelegate中实现,并使用另一个视图控制器来查看该登录按钮。
除了在AppDelegate中实现此功能外,我们是否能够在登录视图控制器中实现并在按钮操作中处理Google函数?
我刚开始在iOS中实现SDK。如果有任何帮助将非常有用。
AppDelegate.swift
文件中,执行以下操作:
Add the following to the import portion:
import GoogleSignIn
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
}
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!
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
上执行以下操作,如下所示:
Add the following to the import portion:
import GoogleSignIn
Add the GIDSignInDelegate
to your class header like this:
class LoginViewController: UIViewController, UITextFieldDelegate, UINavigationControllerDelegate, GIDSignInDelegate {
// your class code here...
}
In your override func viewDidLoad()
, add the following on the bottom:
GIDSignIn.sharedInstance().delegate = self
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社区为我所做的一切。