ios - 谷歌登录 API

标签 ios swift gidsignin

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

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

最佳答案

对于 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()
    }

移除委托(delegate):GIDSignInUIDelegate

关于ios - 谷歌登录 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36543729/

相关文章:

ios - Google 登录获取额外数据

ios - 'GIDSignIn' 没有可见的 @interface 声明选择器 'handleURL :

ios - React Native 标签栏应用倒置不旋转

ios - UIPageViewController 缓存问题

ios - 如何对 tableView 中的单元格进行排序?

swift - macOS/swift 使用 AVCaptureSession 捕获音频

ios - 检索 Firebase 数据的更有效方法?

ios - 使用 GIDSignin 进行谷歌多次登录

ios - 从 Restkit 获取错误描述

ios - 最后一个 block 返回时触发方法