该操作无法完成

时间:2018-12-31 09:00:12

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

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

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()

    }

}

我的要求是,当我单击要登录的登录按钮时

1 个答案:

答案 0 :(得分: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()
相关问题