Sau khi người dùng đăng nhập bằng Google, bạn có thể nhận thông tin cơ bản về hồ sơ của người dùng: tên, URL ảnh hồ sơ và địa chỉ email của họ.
Trước khi bắt đầu
- Tải các phần phụ thuộc xuống và định cấu hình dự án Xcode của bạn.
- Tích hợp tính năng Đăng nhập bằng Google vào ứng dụng của bạn.
Đang truy xuất thông tin người dùng
Sau khi người dùng đã xác thực và cấp quyền truy cập vào các phạm vi mà bạn yêu cầu, bạn có thể truy cập thông tin hồ sơ người dùng thông qua đối tượng GIDGoogleUser
.
Swift
GIDSignIn.sharedInstance.signIn(withPresenting: self) { signInResult, error in
guard error == nil else { return }
guard let signInResult = signInResult else { return }
let user = signInResult.user
let emailAddress = user.profile?.email
let fullName = user.profile?.name
let givenName = user.profile?.givenName
let familyName = user.profile?.familyName
let profilePicUrl = user.profile?.imageURL(withDimension: 320)
}
Objective-C
[GIDSignIn.sharedInstance signInWithPresentingViewController:self
completion:^(GIDSignInResult * _Nullable signInResult,
NSError * _Nullable error) {
if (error) { return; }
if (signInResult == nil) { return; }
GIDGoogleUser *user = signInResult.user;
NSString *emailAddress = user.profile.email;
NSString *name = user.profile.name;
NSString *givenName = user.profile.givenName;
NSString *familyName = user.profile.familyName;
NSURL *profilePic = [user.profile imageURLWithDimension:320];
}];