หน้านี้แสดงวิธีผสานรวม Google Sign-In เข้ากับแอป iOS หรือ macOS คุณอาจต้องปรับวิธีการเหล่านี้ตามวงจรการใช้งานหรือโมเดล UI ของแอป
ก่อนเริ่มต้น
ดาวน์โหลดทรัพยากร Dependency กำหนดค่าโปรเจ็กต์ 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
ตรวจสอบว่าคุณได้เพิ่มการขึ้นต่อกันสำหรับปุ่ม "ลงชื่อเข้าใช้ด้วย Google" ของ SwiftUI ในโปรเจ็กต์แล้ว
ในไฟล์ที่คุณต้องการเพิ่มปุ่ม SwiftUI ให้เพิ่มการนำเข้าที่จำเป็นที่ด้านบนของไฟล์ ดังนี้
import GoogleSignInSwift
เพิ่มปุ่ม "ลงชื่อเข้าใช้ด้วย Google" ลงใน View และระบุการทำงานที่จะเรียกเมื่อกดปุ่ม ดังนี้
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
ลงในสตอรีบอร์ด ปุ่มลงชื่อเข้าใช้จะไม่แสดงผลในเครื่องมือสร้างอินเทอร์เฟซ เรียกใช้แอปเพื่อดูปุ่มลงชื่อเข้าใช้คุณปรับแต่งรูปลักษณ์ของ
GIDSignInButton
ได้ด้วยการตั้งค่าพร็อพเพอร์ตี้colorScheme
และstyle
ดังนี้คุณสมบัติของรูปแบบปุ่มลงชื่อเข้าใช้ GID colorScheme
kGIDSignInButtonColorSchemeLight
kGIDSignInButtonColorSchemeDark
style
kGIDSignInButtonStyleStandard
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 ID ของผู้ใช้
- เรียกใช้ Google API ในนามของผู้ใช้