Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

ios - Google Sign In API

First of all need to say that i don't use CocoaPods. And it's first time when i use Google API.
In Google guide says that i need to configure GIDSignIn in application:didFinishLaunchingWithOptions: method, but i'm also use Facebook API which is configured in this method. Also when i try to configure G API in this method i receive errors: Type 'AppDelegate' does not conform to protocol 'GIDSignInDelegate' and Value of type 'GIDSignIn' has no member 'configureWithError'.
How can i configure GIDSignIn not in the 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();
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For swift 5 Xcode 10.3 (Latest google signin: Pod: 5.0.2)

Replace:

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

With: 

    GIDSignIn.sharedInstance().handle(url)

On Button click

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

        GIDSignIn.sharedInstance().signIn()
    }

Remove Delegate: GIDSignInUIDelegate


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...