gpt4 book ai didi

ios - 如何快速使用 Google Sign-In API

转载 作者:行者123 更新时间:2023-11-28 06:21:10 27 4
gpt4 key购买 nike

我正在尝试实现 Google Sign In API,以便用户可以在我的 iOS 应用程序中对 Gmail 和 Google Drive 进行身份验证。我想要两个 Google 登录按钮(一个在第一个教程中,一个在应用程序设置中,以防您以后想要更改帐户),但无法使任何一个工作。我尝试关注 these three tutorials但无法使登录按钮正常工作。现在点击按钮会弹出登录屏幕,让你登录,带你回到应用程序,然后没有其他事情发生(我不能使用任何帐户信息)。这是我的代码:

应用委托(delegate)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().barTintColor = .red
UINavigationBar.appearance().barStyle = .blackOpaque
UINavigationBar.appearance().tintColor = .white

// Initialize Google sign-in
var configureError: NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError as Optional)")

return true
}

func application(_ app: UIApplication,
open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

return GIDSignIn.sharedInstance().handle(url as URL!,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String!,
annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}

TableView Controller (设置屏幕)

override func viewDidLoad() {
super.viewDidLoad()

GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/gmail.send")

// Uncomment to automatically sign in the user.
GIDSignIn.sharedInstance().signInSilently()

// Implement these methods only if the GIDSignInUIDelegate is not a subclass of UIViewController.

// Stop the UIActivityIndicatorView animation that was started when the user
// pressed the Sign In button
func signInWillDispatch(signIn: GIDSignIn!, error: NSError!) {
//myActivityIndicator.stopAnimating()
}

// Present a view that prompts the user to sign in with Google
func signIn(signIn: GIDSignIn!,
presentViewController viewController: UIViewController!) {
self.present(viewController, animated: true, completion: nil)
}

// Dismiss the "Sign in with Google" view
func signIn(signIn: GIDSignIn!,
dismissViewController viewController: UIViewController!) {
self.dismiss(animated: true, completion: nil)
}
}

//MARK: - Google Stuff
public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if (error == nil) {
// Perform any operations on signed in user here.
//let userId = user.userID // For client-side use only!
//let idToken = user.authentication.idToken // Safe to send to the server
let email = user.profile.email
let fullName = user.profile.name

UserDefaults.standard.set(email, forKey: "googleEmail")
print("signed in \(fullName as Optional) to Google")
} else {
print("\(error.localizedDescription)")
}
}

@IBAction func didTapSignOut(_ sender: AnyObject) {
GIDSignIn.sharedInstance().signOut()
UserDefaults.standard.removeObject(forKey: "googleEmail")
}

最佳答案

第 1 步:

您必须需要客户端 ID 才能登录 Google。确保您拥有 Google 帐户以获取客户端 ID

让我解释一下从谷歌帐户获取客户端ID的过程。

您需要使用您的 Google 帐户登录 Google Developers Console

第 2 步:

如果您尚未创建,请使用 Xcode 创建一个简单的 Swift 应用程序。

如果您已经创建了一个应用程序,请选择您的应用程序名称。添加您的应用程序包标识符。

如果它询问您的国家/地区。您应该输入您的国家/地区并单击继续按钮。

第 3 步:

选择您要使用的 Google 服务。

点击Google Sign-in选择并点击按钮启用Google Sign-in服务。

现在您的应用程序已启用 Google 登录服务。

第 4 步:

下载 GoogleService-info.plist

现在您可以下载 GoogleService-info.plist 文件。稍后您将在配置 Xcode 项目时使用它

第 5 步

配置你的 Xcode 项目

现在,创建一个新的 Xcode 项目:

但是,要实现Google Sign-in服务,你需要Google Sign-in SDK

Download Google Sign-in SDK.

将 GoogleService-Info.plist 添加到 Xcode 项目的根目录。

我找到了最好的tutorial in detail by Huntmyideas

关于ios - 如何快速使用 Google Sign-In API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43439542/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com