首先,按照此说明在Google API 控制台中设置您的项目(并下载配置文件 GoogleService-Info.plist)并将最新的GoogleSignIn SDK
集成
到您的项目中:集成指南
一段代码。
这是目前使用最新版本的GoogleSignIn SDK获取用户信息的代码。
#import <GoogleSignIn/GoogleSignIn.h>
//...
@interface ViewController() <GIDSignInDelegate, GIDSignInUIDelegate>
//...
- (IBAction)googleSignInTapped:(id)sender {
[GIDSignIn sharedInstance].clientID = kClientId;// Replace with the value of CLIENT_ID key in GoogleService-Info.plist
[GIDSignIn sharedInstance].delegate = self;
[GIDSignIn sharedInstance].shouldFetchBasicProfile = YES; //Setting the flag will add "email" and "profile" to scopes.
NSArray *additionalScopes = @[@"https://www.googleapis.com/auth/contacts.readonly",
@"https://www.googleapis.com/auth/plus.login",
@"https://www.googleapis.com/auth/plus.me"];
[GIDSignIn sharedInstance].scopes = [[GIDSignIn sharedInstance].scopes arrayByAddingObjectsFromArray:additionalScopes];
[[GIDSignIn sharedInstance] signIn];
}
#pragma mark - GoogleSignIn delegates
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {
if (error) {
//TODO: handle error
} else {
NSString *userId = user.userID;
NSString *fullName = user.profile.name;
NSString *email = user.profile.email;
NSURL *imageURL = [user.profile imageURLWithDimension:1024];
NSString *accessToken = user.authentication.accessToken; //Use this access token in Google+ API calls.
//TODO: do your stuff
}
}
- (void)signIn:(GIDSignIn *)signIn didDisconnectWithUser:(GIDGoogleUser *)user withError:(NSError *)error {
// Perform any operations when the user disconnects from app here.
}
- (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController {
[self presentViewController:viewController animated:YES completion:nil];
}
// Dismiss the "Sign in with Google" view
- (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController {
[self dismissViewControllerAnimated:YES completion:nil];
}
你的AppDelegate.m
#import <GoogleSignIn/GoogleSignIn.h>
//...
@implementation AppDelegate
//...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//...
return YES;
}
//For iOS 9.0 and later
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
//...
return [[GIDSignIn sharedInstance] handleURL:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
}
//For your app to run on iOS 8 and older,
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(nonnull id)annotation {
//...
return [[GIDSignIn sharedInstance] handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
@end
还要确保您添加了带有您的REVERSED_CLIENT_ID的 URL Scheme,您可以在 GoogleService-Info.plist 中找到它。
为了能够在 Google+ 中获取您的连接列表,您需要在Google API Console中为您的项目启用Google People API
和启用。Google+ API
然后只需将 HTTP 请求发送到以下网址:
https ://www.googleapis.com/plus/v1/people/me/people/visible?access_token=YOUR_ACCESS_TOKEN
要检查用户是否有 Google+ 个人资料,只需将 HTTP 请求发送到以下网址:
https"isPlusUser"
://www.googleapis.com/plus/v1/people/USER_ID?access_token=YOUR_ACCESS_TOKEN 并检查其
值为"true"
or"false"
小红利
示例项目