- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 iOS Swift
应用程序中有 Google Sign In
。它工作正常,但每次我都需要输入 email id
和 password
。与此同时,我正在我的 iPhone 中使用 Gmail App
。如何从我的应用程序中使用该 Gmail 凭据并直接登录?是否可以使用 Google-SignIn-iOS SDK
?
我对 Google、FB、Outlook、Twitter、LinkedIn 也需要同样的东西。
View Controller :
import GoogleSignIn
class ViewController: UIViewController, GIDSignInUIDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
GIDSignIn.sharedInstance().uiDelegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func sign(inWillDispatch signIn: GIDSignIn!, error: Error!) {
}
func sign(_ signIn: GIDSignIn!, present viewController: UIViewController!) {
}
func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!) {
}
}
AppDelegate:
import GoogleSignIn
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
GIDSignIn.sharedInstance().clientID = "3578***********************.apps.googleusercontent.com"
GIDSignIn.sharedInstance().delegate = self
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])
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
withError error: Error!) {
if let error = error {
print("\(error.localizedDescription)")
} else {
// 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 fullName = user.profile.name
let givenName = user.profile.givenName
let familyName = user.profile.familyName
let email = user.profile.email
// ...
print("emailemailemail ", email)
}
}
func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!,
withError error: Error!) {
// Perform any operations when the user disconnects from app here.
// ...
}
}
最佳答案
按照这些步骤
第一步
在您的 appdelegate 中添加客户端 ID 和深度链接目的在源应用程序中添加返回值
import GoogleSignIn
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
GIDSignIn.sharedInstance().clientID = "3578***********************.apps.googleusercontent.com"
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])
}
出于可重用性的目的,我为谷歌登录创建了通用类
import UIKit
import GoogleSignIn
class GoogleSDK: NSObject,GIDSignInDelegate,GIDSignInUIDelegate {
static let shared = GoogleSDK()
//MARK: Internal Properties
var signInBlock: ((GIDGoogleUser) -> Void)?
func googleSignIn() {
GIDSignIn.sharedInstance().delegate = self
// GIDSignIn.sharedInstance().signOut()
// GIDSignIn.sharedInstance().signIn()
GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/plus.login")
GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/plus.me")
if GIDSignIn.sharedInstance().hasAuthInKeychain() == true {
GIDSignIn.sharedInstance().signInSilently()
} else {
GIDSignIn.sharedInstance().signIn()
}
}
//Google SignIn Delegates
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if error == nil {
self.signInBlock?(user)
} else {
print("\(error.localizedDescription)")
}
}
func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
// signInBlock(nil)
// if signInBlock != nil {
// signInBlock!(false, nil, error)
// }
}
}
在你当前的类上调用 NSObject 调用
import GoogleSignIn
class ViewController: UIViewController, GIDSignInUIDelegate {
// call the following method where you need
// MARK: - Google Login
func GoogleLogin(){
// getTypeofLogin = getLoginType.Google.getName()
GIDSignIn.sharedInstance().uiDelegate = self
GoogleSDK.shared.googleSignIn()
GoogleSDK.shared.signInBlock = { (user) in
if !user.userID.isEmpty{
print(user)
//self.handleSocialLogin(UID: user.userID, UName: user.profile.name, UEmail: user.profile.email, loginType:getLoginType.Google.getName())
}
}
}
}
注销使用这个
GIDSignIn.sharedInstance().signOut()
关于ios - 通过在 iOS 中安装 Gmail 应用凭据登录 Google 帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53398839/
我是一名优秀的程序员,十分优秀!