หน้านี้แสดงวิธีผสานรวม Google Sign-In เข้ากับแอป iOS หรือ macOS คุณอาจต้องปรับวิธีการเหล่านี้ตามอายุการใช้งานหรือโมเดล UI ของแอป
ก่อนเริ่มต้น
ดาวน์โหลดทรัพยากร Dependency กำหนดค่าโปรเจ็กต์ Xcode และตั้งค่ารหัสไคลเอ็นต์ของคุณ
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 และเรียก GIDSignIn
handleURL
:
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" ลงใน "มุมมอง" และระบุการทำงานที่จะเรียกเมื่อกดปุ่ม
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
ดังนี้คุณสมบัติรูปแบบ GIDSignInButton 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 APIs ในนามของผู้ใช้