首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Google iOS登录单页示例AppDelegate.h协议->转换为segue to LoginPage ViewController AppDelegate.swift协议?

如何将Google iOS登录单页示例AppDelegate.h协议->转换为segue to LoginPage ViewController AppDelegate.swift协议?
EN

Stack Overflow用户
提问于 2021-09-20 23:19:51
回答 2查看 42关注 0票数 0

谷歌在GitHub SignInSampleForPod.xcworkspace上的登录示例使用AppDelegate.hSignInViewController.m等协议创建了一个单页面登录。

然而,许多应用程序,如我的,更喜欢分割到登录页面,只有当用户做出需要验证的选择时。我只想要基本的谷歌个人资料信息和认证令牌。

我已经对谷歌iOS ClientID进行了足够的配置,这样我的LoginPage.swift就可以通过AppDelegate.swift显示谷歌登录按钮:

代码语言:javascript
复制
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(
_ app: UIApplication,
open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]
) -> Bool {
var handled: Bool 
handled = GIDSignIn.sharedInstance.handle(url)
if handled {
  return true
}
// Handle other custom URL types.
// If not handled by this app, return false.
return false
}

func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in
  if error != nil || user == nil {
    // Show the app's signed-out state.
  } else {
    // Show the app's signed-in state.
  }
}
return true
}

和LoginPage.swift:

代码语言:javascript
复制
class LoginViewController: UIViewController {
let signInConfig = GIDConfiguration.init(clientID: "foo-bar85.apps.googleusercontent.com")
override func viewDidLoad() {
    super.viewDidLoad()
GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in
      guard error == nil else { return }
      guard let user = user else { return }

      let emailAddress = user.profile?.email

      let fullName = user.profile?.name
      let givenName = user.profile?.givenName
      let familyName = user.profile?.familyName

      let profilePicUrl = user.profile?.imageURL(withDimension: 320)
  }

所以我的问题是,下面显示基本个人资料信息的字段的AppDelegate.swift谷歌登录代码是什么:

代码语言:javascript
复制
    // Show the app's signed-out state.
  } else {
    // Show the app's signed-in state.
EN

回答 2

Stack Overflow用户

发布于 2021-09-21 14:46:23

我可能不能很清楚地理解你的问题。但我正试图根据我的理解来回答。

您可以为所有与google登录相关的内容创建一个类(GoogleLoginManager),并在UI中创建一个按钮,然后从按钮操作调用此方法(signIn)。

代码语言:javascript
复制
@IBAction func googleButtonAction(_ sender: Any) {
    
    GoogleLoginManager.shared.signIn(controller: self) { (profile) in
        print("GoogleLogin profile : \(String(describing: profile.name)), \(String(describing: profile.email))")
        
    } onFailure: { (error) in
        print("GoogleLogin error : \(String(describing: error.localizedDescription))")
    }
}

导入基础导入GoogleSignIn

类GoogleLoginManager: SocialLogin {

代码语言:javascript
复制
fileprivate var onSuccess : success?
fileprivate var onFailure : failure?

static let shared = GoogleLoginManager()
private override init() { }

func signIn(controller: UIViewController, onSuccess : @escaping success, onFailure : @escaping failure) {
    
    self.onSuccess = onSuccess
    self.onFailure = onFailure
    
    GIDSignIn.sharedInstance().clientID = GOOGLE_CLIENT_ID
    GIDSignIn.sharedInstance().delegate = self
    
    GIDSignIn.sharedInstance().presentingViewController = controller
    GIDSignIn.sharedInstance().signIn()
    
    // Automatically sign in the user.
    // GIDSignIn.sharedInstance()?.restorePreviousSignIn()
}

func signOut() {
    GIDSignIn.sharedInstance().signOut()
}

}

扩展GoogleLoginManager : GIDSignInDelegate {

代码语言:javascript
复制
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
          withError error: Error!) {
    if let error = error {
        if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue {
            print("The user has not signed in before or they have since signed out.")
        }
        else if (error as NSError).code == GIDSignInErrorCode.canceled.rawValue {
            print("user canceled the sign in request")
        }
        else {
            print("\(error.localizedDescription)")
        }
        self.onFailure?(error)
        return
    }
    var profile =  SocialProfileModel.init(user: user)
    profile.loginSuccess = true
    self.onSuccess?(profile)
}

func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!,
          withError error: Error!) {
    // Perform any operations when the user disconnects from app here.
    print("GIDSignIn : didDisconnectWith")
    
}

}

票数 0
EN

Stack Overflow用户

发布于 2021-09-22 20:34:47

我只需要稍微修改一下上面的AppDelegate.swift -添加一个链接到以下操作的标准UIbutton -获得个人资料信息:

代码语言:javascript
复制
 @IBAction func LogInButtonTouched(_ sender: UIButton) {
  GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in
      guard error == nil else { return }
      guard let user = user else { return }

      let emailAddress = user.profile?.email

      let fullName = user.profile?.name
      let givenName = user.profile?.givenName
      let familyName = user.profile?.familyName

      let profilePicUrl = user.profile?.imageURL(withDimension: 320)
    print("GoogleLogin profile : \(String(describing: user.profile?.name)), \(String(describing: user.profile?.email))")
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69261790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档