ios – Google登录API

前端之家收集整理的这篇文章主要介绍了ios – Google登录API前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
首先需要说我不使用 CocoaPods.这是我第一次使用Google API.
在Google指南中说我需要在应用程序中配置GIDSignIn:didFinishLaunchingWithOptions:方法,但我也使用在此方法中配置的Facebook API.此外,当我尝试在此方法中配置G API时,我收到错误:类型’AppDelegate’不符合协议’GIDSignInDelegate’,类型’GIDSignIn’的值没有成员’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();
    }

解决方法

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

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

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

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

并且不要忘记在app delegate中处理url.

func application(application: UIApplication,annotation: AnyObject) -> Bool {

var flag: Bool = false

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

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

return flag
}
原文链接:https://www.f2er.com/iOS/335647.html

猜你在找的iOS相关文章