หน้านี้จะแสดงวิธีผสานรวม Google Sign-In เข้ากับแอป iOS หรือ macOS คุณอาจต้องปรับคำแนะนำเหล่านี้ให้เหมาะกับวงจรการใช้งานหรือโมเดล UI ของแอป
ก่อนเริ่มต้น
ดาวน์โหลดการพึ่งพา กำหนดค่าโปรเจ็กต์ Xcode และตั้งค่ารหัสไคลเอ็นต์
ลองใช้แอปตัวอย่าง iOS และ macOS เพื่อดูวิธีการทำงานของการลงชื่อเข้าใช้
1. จัดการ URL เปลี่ยนเส้นทางสำหรับการตรวจสอบสิทธิ์
iOS: UIApplicationDelegate
ในเมธอด application:openURL:options ของ AppDelegate ให้เรียกใช้เมธอด handleURL: ของ GIDSignIn ดังนี้
Swift
func application(
_ app: UIApplication,
open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]
) -> Bool {
var handled: Bool
handled = GIDSignIn.sharedInstance.handle(url)
if handled {
return true
}
// Handle other custom URL types.
// If not handled by this app, return false.
return false
}
Objective-C
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
BOOL handled;
handled = [GIDSignIn.sharedInstance handleURL:url];
if (handled) {
return YES;
}
// Handle other custom URL types.
// If not handled by this app, return NO.
return NO;
}
macOS: NSApplicationDelegate
ใน AppDelegate ของแอป ให้ลงทะเบียนตัวแฮนเดิลสำหรับเหตุการณ์
kAEGetURLในapplicationDidFinishLaunchingดังนี้Swift
func applicationDidFinishLaunching(_ notification: Notification) { // Register for GetURL events. let appleEventManager = NSAppleEventManager.shared() appleEventManager.setEventHandler( self, andSelector: "handleGetURLEvent:replyEvent:", forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL) ) }Objective-C
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Register for GetURL events. NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager]; [appleEventManager setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL]; }กำหนดตัวแฮนเดิลสำหรับเหตุการณ์เหล่านี้ที่เรียกใช้
handleURLของGIDSignInดังนี้Swift
func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) { if let urlString = event?.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue{ let url = NSURL(string: urlString) GIDSignIn.sharedInstance.handle(url) } }Objective-C
- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent { NSString *URLString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; NSURL *URL = [NSURL URLWithString:URLString]; [GIDSignIn.sharedInstance handleURL:url]; }
SwiftUI
ในหน้าต่างหรือฉากของแอป ให้ลงทะเบียนตัวแฮนเดิลเพื่อรับ URL และเรียกใช้ handleURL ของ GIDSignIn ดังนี้
Swift
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
// ...
.onOpenURL { url in
GIDSignIn.sharedInstance.handle(url)
}
}
}
}
2. พยายามกู้คืนสถานะการลงชื่อเข้าใช้ของผู้ใช้
เมื่อแอปเริ่มต้น ให้เรียกใช้ restorePreviousSignInWithCallback เพื่อพยายามกู้คืนสถานะการลงชื่อเข้าใช้ของผู้ใช้ที่ลงชื่อเข้าใช้ด้วยบัญชี Google แล้ว การดำเนินการนี้จะช่วยให้ผู้ใช้ไม่ต้องลงชื่อเข้าใช้ทุกครั้งที่เปิดแอป (เว้นแต่จะออกจากระบบ)
แอป iOS มักจะดำเนินการนี้ในเมธอด application:didFinishLaunchingWithOptions: ของ UIApplicationDelegate และ applicationDidFinishLaunching: ของ NSApplicationDelegate สำหรับแอป macOS ใช้ผลลัพธ์เพื่อกำหนดมุมมองที่จะแสดงต่อผู้ใช้ เช่น
Swift
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in
if error != nil || user == nil {
// Show the app's signed-out state.
} else {
// Show the app's signed-in state.
}
}
return true
}
Objective-C
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GIDSignIn.sharedInstance restorePreviousSignInWithCompletion:^(GIDGoogleUser * _Nullable user,
NSError * _Nullable error) {
if (error) {
// Show the app's signed-out state.
} else {
// Show the app's signed-in state.
}
}];
return YES;
}
SwiftUI
หากใช้ SwiftUI ให้เพิ่มการเรียกใช้ restorePreviousSignIn ใน onAppear สำหรับมุมมองเริ่มต้น ดังนี้
Swift
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
// ...
.onAppear {
GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in
// Check if `user` exists; otherwise, do something with `error`
}
}
}
}
}
3. เพิ่มปุ่ม Google Sign-In
เพิ่มปุ่ม "ลงชื่อเข้าใช้ด้วย Google" ลงในมุมมองการลงชื่อเข้าใช้ คอมโพเนนต์พร้อมใช้งานสำหรับ SwiftUI และ UIKit ซึ่งจะสร้างปุ่มที่มีการสร้างแบรนด์ของ Google โดยอัตโนมัติ และขอแนะนำให้ใช้
การใช้ SwiftUI
ในไฟล์ที่ต้องการเพิ่มปุ่ม SwiftUI ให้เพิ่มการนำเข้าที่จำเป็นที่ด้านบนของไฟล์ ดังนี้
import GoogleSignInSwiftเพิ่มปุ่ม "ลงชื่อเข้าใช้ด้วย Google" ลงในมุมมองและระบุการดำเนินการที่จะเรียกใช้เมื่อกดปุ่ม ดังนี้
GoogleSignInButton(action: handleSignInButton)ทริกเกอร์กระบวนการลงชื่อเข้าใช้เมื่อกดปุ่มโดยเพิ่มการเรียกใช้เมธอด
signIn(presentingViewController:completion:)ของGIDSignInในการดำเนินการ ดังนี้func handleSignInButton() { GIDSignIn.sharedInstance.signIn( withPresenting: rootViewController) { signInResult, error in guard let result = signInResult else { // Inspect error return } // If sign in succeeded, display the app's main content View. } ) }
ซึ่งจะใช้โมเดลมุมมองเริ่มต้นที่ให้ข้อมูลการจัดรูปแบบมาตรฐานสำหรับปุ่ม หากต้องการควบคุมลักษณะที่ปรากฏของปุ่ม คุณต้องสร้าง
GoogleSignInButtonViewModel ที่กำหนดเองและตั้งค่าเป็น viewModel ใน
ตัวเริ่มต้นของปุ่มโดยใช้ GoogleSignInButton(viewModel: yourViewModel, action:
yourAction) ดูข้อมูลเพิ่มเติมได้ที่
ซอร์สโค้ด GoogleSignInButtonViewModel
การใช้ UIKit
เพิ่มปุ่ม "ลงชื่อเข้าใช้ด้วย Google" ลงในมุมมองการลงชื่อเข้าใช้ คุณสามารถใช้คลาส
GIDSignInButtonเพื่อสร้างปุ่มที่มีการสร้างแบรนด์ของ Google โดยอัตโนมัติ (แนะนำ) หรือสร้างปุ่มของคุณเองด้วยการจัดรูปแบบที่กำหนดเองหากต้องการเพิ่ม
GIDSignInButtonลงในสตอรีบอร์ด หรือไฟล์ XIB ให้เพิ่มมุมมองและตั้งค่าคลาสที่กำหนดเองเป็นGIDSignInButtonโปรดทราบว่าเมื่อคุณเพิ่มมุมมองGIDSignInButtonลงใน Storyboard ปุ่มลงชื่อเข้าใช้จะไม่แสดงใน Interface Builder ให้เรียกใช้แอปเพื่อดูปุ่มลงชื่อเข้าใช้คุณสามารถปรับแต่งลักษณะที่ปรากฏของ
GIDSignInButtonได้โดยการตั้งค่าพร็อพเพอร์ตี้colorSchemeและstyleดังนี้พร็อพเพอร์ตี้สไตล์ของ GIDSignInButton colorSchemekGIDSignInButtonColorSchemeLight
kGIDSignInButtonColorSchemeDarkstylekGIDSignInButtonStyleStandard
kGIDSignInButtonStyleWide
kGIDSignInButtonStyleIconOnlyเชื่อมต่อปุ่มกับเมธอดใน ViewController ที่เรียกใช้
signIn:เช่น ใช้IBActionดังนี้Swift
@IBAction func signIn(sender: Any) { GIDSignIn.sharedInstance.signIn(withPresenting: self) { signInResult, error in guard error == nil else { return } // If sign in succeeded, display the app's main content View. } }Objective-C
- (IBAction)signIn:(id)sender { [GIDSignIn.sharedInstance signInWithPresentingViewController:self completion:^(GIDSignInResult * _Nullable signInResult, NSError * _Nullable error) { if (error) { return; } // If sign in succeeded, display the app's main content View. }]; }
4. เพิ่มปุ่มออกจากระบบ
เพิ่มปุ่มออกจากระบบลงในแอป ซึ่งผู้ใช้ที่ลงชื่อเข้าใช้จะมองเห็น
เชื่อมต่อปุ่มกับเมธอดใน ViewController ที่เรียกใช้
signOut:เช่น ใช้IBActionดังนี้Swift
@IBAction func signOut(sender: Any) { GIDSignIn.sharedInstance.signOut() }Objective-C
- (IBAction)signOut:(id)sender { [GIDSignIn.sharedInstance signOut]; }
ขั้นตอนถัดไป
เมื่อผู้ใช้ลงชื่อเข้าใช้แอปด้วยบัญชี Google ได้แล้ว ให้ดูวิธีดำเนินการต่อไปนี้
- รับข้อมูลโปรไฟล์บัญชี Google ของผู้ใช้
- ตรวจสอบสิทธิ์กับแบ็กเอนด์โดยใช้รหัส Google ของผู้ใช้ โทเค็น
- เรียกใช้ Google API ในนามของผู้ใช้