【问题标题】:The operation couldn’t be completed操作无法完成
【发布时间】:2019-05-27 20:53:27
【问题描述】:

我正在我的 Swift 项目中集成 google 登录,但出现了一些错误。

Error: The operation couldn’t be completed. (com.google.GIDSignIn error -4.)

我点击了这个链接:https://developers.google.com/identity/sign-in/ios/start-integrating?refresh=1

我的代码是:在 AppDelegate.swift

import UIKit
import GoogleSignIn

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        // Initialize sign-in
        GIDSignIn.sharedInstance().clientID = "15084783084-s3siolgukj7ikgqhfgcvo2e7i4jb9.apps.googleusercontent.com"
        GIDSignIn.sharedInstance().delegate = self

        return true
    }

    private func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        return GIDSignIn.sharedInstance().handle(url as URL?,
                                             sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
                                             annotation: options[UIApplication.OpenURLOptionsKey.annotation])
    }

    private func application(application: UIApplication,
                 openURL url: URL, sourceApplication: String?, annotation: Any?) -> Bool {
         var _: [String: AnyObject] = [UIApplication.OpenURLOptionsKey.sourceApplication.rawValue: sourceApplication as AnyObject,
                                        UIApplication.OpenURLOptionsKey.annotation.rawValue: annotation as AnyObject]
        return GIDSignIn.sharedInstance().handle(url,
                                                sourceApplication: sourceApplication,
                                                annotation: 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
            // ...
        }
    }

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

ViewController.swift

import UIKit
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

    }

    @IBAction func onClickGoogleSignIn(_ sender: UIButton) {

        GIDSignIn.sharedInstance().signInSilently()

        //        GIDSignIn.sharedInstance().signOut()

    }

}

我的要求是点击登录按钮时我想登录

【问题讨论】:

    标签: ios swift oauth google-oauth google-signin


    【解决方案1】:

    SignIn方法中,

    使用此代码:

    @IBAction func onClickGoogleSignIn(_ sender: UIButton) {
        GIDSignIn.sharedInstance().signOut() //if already signed in, then sign out and have fresh sign in again
        GIDSignIn.sharedInstance().signIn()
    }
    

    而不是

    GIDSignIn.sharedInstance().signInSilently()
    

    【讨论】:

    • 是的,谢谢 Mehul Thakkar。它正在工作,是否可以收集用户完整的详细信息,如注册手机号码、出生日期等
    • 您只能访问姓名、电子邮件、accessTokens、id 等。要获取 DOB、mobileNumber 等更多详细信息,您必须在登录前在 GIDSignIn 中添加更多范围。您可以参考此链接:@ 987654321@
    • 简单地说...将 GoogleSignIn 的完整代码从 AppDelegate 移动到您的 ViewController.swift。这对你有用。
    • 在 AppDelegate... 只保留 openURL GoogleSignIn 的方法代码
    • 您的代码中还有一个错误 - 您在您的 AppDelegate 中保留了 2 个 openURL 方法。在这 2 个中,一个用于 iOS 7 和以前的版本。因此,如果您不支持这些版本,请删除该方法
    猜你喜欢
    • 2013-10-31
    • 2013-07-25
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 2014-10-22
    相关资源
    最近更新 更多