คุณสามารถอนุญาตให้ผู้ใช้ตรวจสอบสิทธิ์กับ Firebase โดยใช้บัญชี Google ของผู้ใช้ได้โดยผสานรวม Google Sign-In เข้ากับแอปของคุณ
ก่อนเริ่มต้น
ใช้ Swift Package Manager เพื่อติดตั้งและจัดการทรัพยากร Dependency ของ Firebase
- เปิดโปรเจ็กต์แอปใน Xcode แล้วไปที่ไฟล์ > เพิ่มแพ็กเกจ
- เมื่อได้รับข้อความแจ้ง ให้เพิ่มที่เก็บ SDK สำหรับแพลตฟอร์ม Firebase ของ Apple ดังนี้
- เลือกไลบรารีการตรวจสอบสิทธิ์ Firebase
- เพิ่มแฟล็ก
-ObjC
ลงในส่วนแฟล็ก Linker อื่นๆ ของการตั้งค่าบิลด์ของเป้าหมาย - เมื่อเสร็จสิ้นแล้ว Xcode จะเริ่มแก้ปัญหาและดาวน์โหลดทรัพยากร Dependency ในเบื้องหลังโดยอัตโนมัติ
https://github.com/firebase/firebase-ios-sdk.git
เพิ่ม Google Sign-In SDK ลงในโปรเจ็กต์
เปิดโปรเจ็กต์แอปใน Xcode แล้วไปที่ไฟล์ > เพิ่มแพ็กเกจ
เมื่อได้รับข้อความแจ้ง ให้เพิ่มที่เก็บ Google Sign-In SDK โดยทำดังนี้
https://github.com/google/GoogleSignIn-iOS
เมื่อเสร็จแล้ว Xcode จะเริ่มแก้ปัญหาและดาวน์โหลดการขึ้นต่อกันของคุณในเบื้องหลังโดยอัตโนมัติ
เปิดใช้ Google Sign-In สําหรับโปรเจ็กต์ Firebase
หากต้องการอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้ด้วย Google Sign-In ได้ คุณต้องเปิดใช้ผู้ให้บริการ Google Sign-In สำหรับโปรเจ็กต์ Firebase ก่อน โดยทำดังนี้
- ในคอนโซล Firebase ให้เปิดส่วนการตรวจสอบสิทธิ์
- เปิดใช้ผู้ให้บริการ Google ในแท็บวิธีการลงชื่อเข้าใช้
คลิกบันทึก
ดาวน์โหลดสำเนาใหม่ของไฟล์
GoogleService-Info.plist
ของโปรเจ็กต์ และคัดลอกไปยังโปรเจ็กต์ Xcode เขียนทับเวอร์ชันที่มีอยู่ด้วยเวอร์ชันใหม่ (โปรดดูเพิ่ม Firebase ลงในโปรเจ็กต์ iOS)
นำเข้าไฟล์ส่วนหัวที่จำเป็น
ก่อนอื่นคุณต้องนำเข้าไฟล์ส่วนหัวของ Firebase SDK และ Google Sign-In SDK ลงในแอป
Swift
import FirebaseCore import FirebaseAuth import GoogleSignIn
Objective-C
@import FirebaseCore; @import GoogleSignIn;
นำ Google Sign-In ไปใช้
ใช้ Google Sign-In โดยทำตามขั้นตอนต่อไปนี้ โปรดดูรายละเอียดเกี่ยวกับการใช้ Google Sign-In ด้วย iOS ในเอกสารประกอบสำหรับนักพัฒนาซอฟต์แวร์ Google Sign-In
- เพิ่มสคีม URL ที่กำหนดเองลงในโปรเจ็กต์ Xcode ของคุณดังนี้
- เปิดการกำหนดค่าโปรเจ็กต์: คลิกชื่อโปรเจ็กต์ในมุมมองแบบต้นไม้ด้านซ้าย เลือกแอปของคุณจากส่วนเป้าหมาย จากนั้นเลือกแท็บข้อมูล แล้วขยายส่วนประเภท URL
- คลิกปุ่ม + แล้วเพิ่มรูปแบบ URL สำหรับรหัสไคลเอ็นต์ที่ย้อนกลับ หากต้องการค้นหาค่านี้ ให้เปิดไฟล์การกำหนดค่า
แล้วมองหาคีย์GoogleService-Info.plist REVERSED_CLIENT_ID
คัดลอกค่าของคีย์นั้นแล้ววางลงในช่องรูปแบบ URL ในหน้าการกำหนดค่า ไม่ต้องแก้ไขช่องอื่นๆเมื่อดำเนินการเสร็จแล้ว การกำหนดค่าควรมีลักษณะคล้ายกับรายการต่อไปนี้ (แต่มีค่าเฉพาะแอปพลิเคชัน)
- ในเมธอด
application:didFinishLaunchingWithOptions:
ของการมอบสิทธิ์แอป ให้กำหนดค่าออบเจ็กต์FirebaseApp
Swift
FirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
- ใช้เมธอด
application:openURL:options:
ของการมอบสิทธิ์แอป เมธอดนี้ควรเรียกใช้เมธอดhandleURL
ของอินสแตนซ์GIDSignIn
ซึ่งจะจัดการ URL ที่แอปพลิเคชันได้รับเมื่อสิ้นสุดกระบวนการตรวจสอบสิทธิ์อย่างเหมาะสมSwift
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { return GIDSignIn.sharedInstance.handle(url) }
Objective-C
- (BOOL)application:(nonnull UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *, id> *)options { return [[GIDSignIn sharedInstance] handleURL:url]; }
- ส่งตัวควบคุมการดูการนำเสนอและรหัสไคลเอ็นต์สำหรับแอปไปยังเมธอด
signIn
ของผู้ให้บริการ Google Sign-In แล้วสร้างข้อมูลเข้าสู่ระบบ Firebase Authentication จากโทเค็นการตรวจสอบสิทธิ์ของ Google ที่ได้Swift
guard let clientID = FirebaseApp.app()?.options.clientID else { return } // Create Google Sign In configuration object. let config = GIDConfiguration(clientID: clientID) GIDSignIn.sharedInstance.configuration = config // Start the sign in flow! GIDSignIn.sharedInstance.signIn(withPresenting: self) { [unowned self] result, error in guard error == nil else { // ... } guard let user = result?.user, let idToken = user.idToken?.tokenString else { // ... } let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: user.accessToken.tokenString) // ... }
Objective-C
GIDConfiguration *config = [[GIDConfiguration alloc] initWithClientID:[FIRApp defaultApp].options.clientID]; [GIDSignIn.sharedInstance setConfiguration:config]; __weak __auto_type weakSelf = self; [GIDSignIn.sharedInstance signInWithPresentingViewController:self completion:^(GIDSignInResult * _Nullable result, NSError * _Nullable error) { __auto_type strongSelf = weakSelf; if (strongSelf == nil) { return; } if (error == nil) { FIRAuthCredential *credential = [FIRGoogleAuthProvider credentialWithIDToken:result.user.idToken.tokenString accessToken:result.user.accessToken.tokenString]; // ... } else { // ... } }];
- เพิ่ม
GIDSignInButton
ลงในสตอรีบอร์ด, ไฟล์ XIB หรือสร้างอินสแตนซ์แบบเป็นโปรแกรม หากต้องการเพิ่มปุ่มในสตอรีบอร์ดหรือไฟล์ XIB ให้เพิ่มมุมมองและตั้งค่าคลาสที่กำหนดเองเป็นGIDSignInButton
- ไม่บังคับ: หากต้องการปรับแต่งปุ่ม ให้ทำดังนี้
Swift
- ในตัวควบคุมการแสดงผล ให้ประกาศว่าปุ่มลงชื่อเข้าใช้เป็นพร็อพเพอร์ตี้
@IBOutlet weak var signInButton: GIDSignInButton!
- เชื่อมต่อปุ่มกับพร็อพเพอร์ตี้
signInButton
ที่คุณเพิ่งประกาศ - ปรับแต่งปุ่มโดยการตั้งค่าคุณสมบัติของออบเจ็กต์ GIDSignInButton
Objective-C
- ในไฟล์ส่วนหัวของตัวควบคุมการดู ให้ประกาศว่าปุ่มลงชื่อเข้าใช้เป็นพร็อพเพอร์ตี้
@property(weak, nonatomic) IBOutlet GIDSignInButton *signInButton;
- เชื่อมต่อปุ่มกับพร็อพเพอร์ตี้
signInButton
ที่คุณเพิ่งประกาศ - ปรับแต่งปุ่มโดยการตั้งค่าคุณสมบัติของออบเจ็กต์ GIDSignInButton
- ในตัวควบคุมการแสดงผล ให้ประกาศว่าปุ่มลงชื่อเข้าใช้เป็นพร็อพเพอร์ตี้
ตรวจสอบสิทธิ์ด้วย Firebase
ขั้นตอนสุดท้าย ดำเนินการเข้าสู่ระบบ Firebase ให้เสร็จสมบูรณ์ด้วยข้อมูลเข้าสู่ระบบการตรวจสอบสิทธิ์ที่สร้างขึ้นในขั้นตอนก่อนหน้า
Swift
Auth.auth().signIn(with: credential) { result, error in // At this point, our user is signed in }
Objective-C
[[FIRAuth auth] signInWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (isMFAEnabled && error && error.code == FIRAuthErrorCodeSecondFactorRequired) { FIRMultiFactorResolver *resolver = error.userInfo[FIRAuthErrorUserInfoMultiFactorResolverKey]; NSMutableString *displayNameString = [NSMutableString string]; for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) { [displayNameString appendString:tmpFactorInfo.displayName]; [displayNameString appendString:@" "]; } [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Select factor to sign in\n%@", displayNameString] completionBlock:^(BOOL userPressedOK, NSString *_Nullable displayName) { FIRPhoneMultiFactorInfo* selectedHint; for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) { if ([displayName isEqualToString:tmpFactorInfo.displayName]) { selectedHint = (FIRPhoneMultiFactorInfo *)tmpFactorInfo; } } [FIRPhoneAuthProvider.provider verifyPhoneNumberWithMultiFactorInfo:selectedHint UIDelegate:nil multiFactorSession:resolver.session completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) { if (error) { [self showMessagePrompt:error.localizedDescription]; } else { [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Verification code for %@", selectedHint.displayName] completionBlock:^(BOOL userPressedOK, NSString *_Nullable verificationCode) { FIRPhoneAuthCredential *credential = [[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationID verificationCode:verificationCode]; FIRMultiFactorAssertion *assertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential]; [resolver resolveSignInWithAssertion:assertion completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (error) { [self showMessagePrompt:error.localizedDescription]; } else { NSLog(@"Multi factor finanlize sign in succeeded."); } }]; }]; } }]; }]; } else if (error) { // ... return; } // User successfully signed in. Get user data from the FIRUser object if (authResult == nil) { return; } FIRUser *user = authResult.user; // ... }];
ขั้นตอนถัดไป
หลังจากผู้ใช้ลงชื่อเข้าใช้เป็นครั้งแรก ระบบจะสร้างบัญชีผู้ใช้ใหม่และลิงก์กับข้อมูลเข้าสู่ระบบ ซึ่งก็คือชื่อผู้ใช้และรหัสผ่าน หมายเลขโทรศัพท์ หรือข้อมูลของผู้ให้บริการการตรวจสอบสิทธิ์ ซึ่งผู้ใช้ที่ลงชื่อเข้าใช้ด้วย ระบบจะจัดเก็บบัญชีใหม่นี้เป็นส่วนหนึ่งของโปรเจ็กต์ Firebase และสามารถใช้เพื่อระบุผู้ใช้ในทุกแอปในโปรเจ็กต์ได้ ไม่ว่าผู้ใช้จะลงชื่อเข้าใช้ด้วยวิธีใด
-
คุณดูข้อมูลโปรไฟล์พื้นฐานของผู้ใช้จากออบเจ็กต์
User
ในแอปได้ โปรดดูหัวข้อจัดการผู้ใช้ ในกฎความปลอดภัยของ Firebase Realtime Database และ Cloud Storage คุณจะรับรหัสผู้ใช้ที่ไม่ซ้ำของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร
auth
ได้ และใช้รหัสดังกล่าวเพื่อควบคุมข้อมูลที่ผู้ใช้เข้าถึงได้
คุณอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปโดยใช้ผู้ให้บริการการตรวจสอบสิทธิ์หลายรายได้โดยลิงก์ข้อมูลเข้าสู่ระบบของผู้ให้บริการการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่
หากต้องการนำผู้ใช้ออกจากระบบ โปรดโทรหา
signOut:
Swift
let firebaseAuth = Auth.auth() do { try firebaseAuth.signOut() } catch let signOutError as NSError { print("Error signing out: %@", signOutError) }
Objective-C
NSError *signOutError; BOOL status = [[FIRAuth auth] signOut:&signOutError]; if (!status) { NSLog(@"Error signing out: %@", signOutError); return; }
คุณอาจต้องเพิ่มโค้ดการจัดการข้อผิดพลาดสำหรับข้อผิดพลาดในการตรวจสอบสิทธิ์ทั้งหมดด้วย โปรดดูหัวข้อจัดการข้อผิดพลาด