2

首先需要说的是我不使用 CocoaPods。这是我第一次使用 Google API。
在 Google 指南中说我需要GIDSignInapplication:didFinishLaunchingWithOptions:方法中进行配置,但我也使用在此方法中配置的 Facebook API。此外,当我尝试以这种方法配置 G API 时,我收到错误:Type 'AppDelegate' does not conform to protocol 'GIDSignInDelegate'Value of type 'GIDSignIn' has no member 'configureWithError'.
如何GIDSignIn在 AppDelegate 中不配置?

Bridging Header

#ifndef Bridging_Header_h
#define Bridging_Header_h

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <Bolts/Bolts.h>
#import <GoogleSignIn/GoogleSignIn.h>

#endif

AppDelegate

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//        var configureError: NSError?
//        GGLContext.sharedInstance().configureWithError(&configureError)
//        assert(configureError == nil, "Error configuring Google services: \(configureError)")
//
//        GIDSignIn.sharedInstance().delegate = self
        return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    }

    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(
            application,
            openURL: url,
            sourceApplication: sourceApplication,
            annotation: annotation)
    }

    func applicationDidBecomeActive(application: UIApplication) {
        FBSDKAppEvents.activateApp()
    }
}

ViewController

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
        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 fullName = user.profile.name
            let givenName = user.profile.givenName
            let familyName = user.profile.familyName
            let email = user.profile.email

            print(userId)
            print(idToken)
            print(fullName)
            print(givenName)
            print(familyName)
            print(email)

        } else {
            print("\(error.localizedDescription)")
        }
    }

    @IBAction func gPlusLoginButtonPressed(sender: AnyObject) {
        var googleSignIn: GIDSignIn!
        googleSignIn = GIDSignIn.sharedInstance();
        googleSignIn.delegate = self
        googleSignIn.uiDelegate = self
        googleSignIn.shouldFetchBasicProfile = true;
        googleSignIn.clientID = "24189713900-d5i1fokf9eubmb03thavk7ht371210ji.apps.googleusercontent.com"
        googleSignIn.scopes.append("https://www.googleapis.com/auth/plus.login")
        googleSignIn.scopes.append("https://www.googleapis.com/auth/plus.me")
        googleSignIn.scopes.append("profile")
//        googleSignIn.signInSilently()
        googleSignIn.signIn();
    }
4

3 回答 3

5

对于 swift 5 Xcode 10.3(最新的谷歌登录:Pod:5.0.2)

Replace:

    GIDSignIn.sharedInstance().handleURL(url,
                sourceApplication: sourceApplication,
                annotation: annotation)

With: 

    GIDSignIn.sharedInstance().handle(url)

在按钮上单击

@IBAction func googleLoginBtnPressed(_ sender: AnyObject) {
        GIDSignIn.sharedInstance()?.presentingViewController = self
        GIDSignIn.sharedInstance()?.restorePreviousSignIn()

        GIDSignIn.sharedInstance().signIn()
    }

删除委托:GIDSignInUIDelegate

于 2019-08-29T10:59:39.067 回答
2

从 didFinishLaunch GIDSignIn.sharedInstance().delegate = self 中删除这一行

并在您的视图控制器类中实现 GIDSignInDelegate、GIDSignInUIDelegate 协议

并在您的视图控制器 viewDidload 方法中写下这个

func viewDidLoad() {
    GIDSignIn.sharedInstance().delegate = self
    GIDSignIn.sharedInstance().uiDelegate = self
}

并且不要忘记在应用程序委托中处理 url。

func application(application: UIApplication,
               openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {

var flag: Bool = false

// handle Facebook url scheme
if let wasHandled:Bool = FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) {
  flag = wasHandled
}

if let googlePlusFlag: Bool = GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication!, annotation: annotation) {
  flag = googlePlusFlag
}

return flag
}
于 2016-04-11T09:40:51.943 回答
1

GGLContext 是 Google 的一部分,因此仅导入 GoogleSignIn 会给您该错误。您需要导入谷歌库。

链接到谷歌图书馆 2.0.3 https://www.gstatic.com/cpdc/a96d915a636d0afb-Google-2.0.3.tar.gz

使用谷歌登录。您需要同时遵守 GIDSignInDelegate 和 GIDSignInUIDelegate 并实现委托方法。

class LoginViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate {

    func viewDidLoad() {
        GIDSignIn.sharedInstance().clientID = Resources.googlePlusClientId()
        GIDSignIn.sharedInstance().shouldFetchBasicProfile = true
        GIDSignIn.sharedInstance().scopes = ["profile", "email"]
        GIDSignIn.sharedInstance().delegate = self
        GIDSignIn.sharedInstance().uiDelegate = self
    }

    func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
            withError error: NSError!) {
    }

}

在 AppDelegate 中

func application(application: UIApplication,
    openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
        let isFacebookURL = FBSDKApplicationDelegate.sharedInstance().application(application,
            openURL: url,
            sourceApplication: sourceApplication,
            annotation: annotation)

        let isGooglePlusURL = GIDSignIn.sharedInstance().handleURL(url,
            sourceApplication: sourceApplication,
            annotation: annotation)

        return isFacebookURL || isGooglePlusURL
}
于 2016-04-11T09:35:11.993 回答