Quick sign up for Google+ GPPSignIn.sharedInstance (). UserID is always nil

I am trying to embed the Google+ login in an iOS application using the Swift language.

My code is as follows:

var kClientId = "My Client ID from Dev Console"
var signIn = GPPSignIn.sharedInstance()
signIn.shouldFetchGooglePlusUser = true
signIn.shouldFetchGoogleUserEmail = true
signIn.shouldFetchGoogleUserID = true
signIn.clientID = kClientId
signIn.scopes = [kGTLAuthScopePlusLogin]
signIn.delegate = self
signIn.authenticate()

For test purposes, I created lable and wanted to change this for the email of the user who is logged in.

if (GPPSignIn.sharedInstance().userID != nil) {
    var user = GPPSignIn.sharedInstance().googlePlusUser
    userName.text = user.name.JSONString()
    if (user.emails != nil){
        userEmailLable.text = user.emails.first?.JSONString() ?? "no email"
    } else {
        userEmailLable.text = "no email"
    }
} else {
    println("User ID is nil")
}

After I click the "Login" button, the Safari tab will open, and I can enter my Google email address and password, and it asks permission for certain things, and after clicking the "Accept" button, it returns to the application. My userEmailLable does not change, and "User ID is no" is output. This happens all the time and there has not been a single successful login.

Google , URL- , Google Developer , .

AppDelegate.swift

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

- , ? !

+4
1

. , Google. , , .

, .

, - , URL- iOS .

.

import UIKit

class ViewController: UIViewController, GPPSignInDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let signIn = GPPSignIn.sharedInstance()
        signIn.shouldFetchGooglePlusUser = true
        signIn.shouldFetchGoogleUserEmail = true  // Uncomment to get the user email
        signIn.shouldFetchGoogleUserID = true

        signIn.clientID = "… my client ID"

        // Uncomment one of these two statements for the scope you chose in the previous step
        signIn.scopes = [ kGTLAuthScopePlusLogin ]  // "https://www.googleapis.com/auth/plus.login" scope

        signIn.delegate = self
        signIn.authenticate()
    }



    // MARK: - GPPSignInDelegate

    func finishedWithAuth(auth: GTMOAuth2Authentication!, error: NSError!) {
        print("received auth \(auth), error \(error)")

        if (GPPSignIn.sharedInstance().userID != nil) {
            let user = GPPSignIn.sharedInstance().googlePlusUser
            println("user name: " + user.name.JSONString() + "\nemail: ")
            if (user.emails != nil){
                print(user.emails.first?.JSONString() ?? "no email")
            } else {
                print("no email")
            }
        } else {
            println("User ID is nil")
        }
    }
}

( , )

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    return GPPURLHandler.handleURL(url, sourceApplication:sourceApplication, annotation: annotation)
}
+2

Source: https://habr.com/ru/post/1598753/


All Articles