首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google登录API

Google登录API
EN

Stack Overflow用户
提问于 2016-04-11 08:39:22
回答 3查看 5.7K关注 0票数 2

首先,我需要说我不使用CocoaPods。这是我第一次使用Google。

在谷歌指南中,我说我需要在GIDSignIn方法中配置application:didFinishLaunchingWithOptions:,但我也使用了在这个方法中配置的Facebook。同样,当我试图在这个方法中配置G时,我会收到错误:Type 'AppDelegate' does not conform to protocol 'GIDSignInDelegate'Value of type 'GIDSignIn' has no member 'configureWithError'

我如何配置GIDSignIn而不是在AppDelegate中?

代码语言:javascript
复制
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();
    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-11 09:40:51

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

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

在您的视图控制器中,viewDidload方法编写如下

代码语言:javascript
复制
func viewDidLoad() {
    GIDSignIn.sharedInstance().delegate = self
    GIDSignIn.sharedInstance().uiDelegate = self
}

别忘了处理应用程序委托中的url。

代码语言:javascript
复制
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
}
票数 2
EN

Stack Overflow用户

发布于 2019-08-29 10:59:39

最新的: Pod: 5.0.2

代码语言:javascript
复制
Replace:

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

With: 

    GIDSignIn.sharedInstance().handle(url)

点击按钮

代码语言:javascript
复制
@IBAction func googleLoginBtnPressed(_ sender: AnyObject) {
        GIDSignIn.sharedInstance()?.presentingViewController = self
        GIDSignIn.sharedInstance()?.restorePreviousSignIn()

        GIDSignIn.sharedInstance().signIn()
    }

删除代表:GIDSignInUIDelegate

票数 5
EN

Stack Overflow用户

发布于 2016-04-11 09:35:11

GGLContext是Google的一部分,所以只导入GoogleSignIn就会给你带来错误。您需要导入Google库。

链接到Google 2.0.3 https://www.gstatic.com/cpdc/a96d915a636d0afb-Google-2.0.3.tar.gz

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

代码语言:javascript
复制
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中

代码语言:javascript
复制
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
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36543729

复制
相关文章

相似问题

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