將Google登錄添加到您的iOS應用

要在您的應用中使用Google登錄,請在您的應用的登錄屏幕上添加一個GIDSignInButton視圖。

然後,使用共享的GIDSignIn對象註冊登錄屏幕的視圖控制器:

物鏡
- (void)viewDidLoad {
  [super viewDidLoad];
  [GIDSignIn sharedInstance].presentingViewController = self;
}
迅速
func viewDidLoad() {
    super.viewDidLoad()
    GIDSignIn.sharedInstance().presentingViewController = self
}

用戶單擊此按鈕並使用其Google帳戶登錄後,該用戶的登錄狀態將使用GIDSignInDelegate協議傳遞給您的應用。

與您的應用程序委託或其他類signIn:didSignInForUser:withError此協議,並實現該協議的signIn:didSignInForUser:withError方法:

物鏡
- (void)signIn:(GIDSignIn *)signIn
    didSignInForUser:(GIDGoogleUser *)user
           withError:(NSError *)error {
  // Perform any operations on signed in user here.
  // ...
}
迅速
func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
  withError error: NSError!) {
    if (error == nil) {
      // Perform any operations on signed in user here.
      // ...
    } else {
      println("\(error.localizedDescription)")
    }
}

然後,在您的應用程序委託中,註冊您的GIDSignInDelegate並使用您的客戶端ID配置Google登錄

物鏡
- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GIDSignIn sharedInstance].clientID = @"YOUR_CLIENT_ID";
  [GIDSignIn sharedInstance].delegate = self;  // If AppDelegate conforms to GIDSignInDelegate
  return YES;
}
迅速
func application(application: UIApplication,
  didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Initialize sign-in
    GIDSignIn.sharedInstance().clientID = "YOUR_CLIENT_ID"
    GIDSignIn.sharedInstance().delegate = self  // If AppDelegate conforms to GIDSignInDelegate
    return true
}

準備將Google登錄功能集成到您的應用中了嗎?

開始使用