इस पेज पर, iOS या macOS ऐप्लिकेशन में 'Google साइन-इन' को इंटिग्रेट करने का तरीका बताया गया है. हो सकता है कि आपको अपने ऐप्लिकेशन के लाइफ़साइकल या यूज़र इंटरफ़ेस (यूआई) मॉडल के हिसाब से, इन निर्देशों में बदलाव करना पड़े.
शुरू करने से पहले
डिपेंडेंसी डाउनलोड करें, अपना Xcode प्रोजेक्ट कॉन्फ़िगर करें, और अपना क्लाइंट आईडी सेट करें.
1. पुष्टि करने के लिए रीडायरेक्ट करने वाले यूआरएल को मैनेज करना
Google से पुष्टि करने के बाद, पुष्टि करने की प्रोसेस आपके ऐप्लिकेशन पर वापस रीडायरेक्ट हो जाती है. इसमें एक यूआरएल होता है. इस यूआरएल में पुष्टि करने का जवाब शामिल होता है. इसमें उपयोगकर्ता का आईडी टोकन भी शामिल होता है. इस यूआरएल को GIDSignIn पर पास करने से, SDK टूल जवाब को पार्स कर पाता है और उपयोगकर्ता के क्रेडेंशियल आपके ऐप्लिकेशन को वापस भेज पाता है.
iOS: UIApplicationDelegate
अपने AppDelegate के application:openURL:options तरीके में, GIDSignIn के handleURL: तरीके को कॉल करें:
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 में,
applicationDidFinishLaunchingमेंkAEGetURLइवेंट के लिए एक हैंडलर रजिस्टर करें: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]; }इन इवेंट के लिए हैंडलर तय करें, जो
GIDSignInकेhandleURLको कॉल करता है: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
अपने ऐप्लिकेशन की विंडो या सीन में, यूआरएल पाने और GIDSignIn के handleURL को कॉल करने के लिए एक हैंडलर रजिस्टर करें:
Swift
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
// ...
.onOpenURL { url in
GIDSignIn.sharedInstance.handle(url)
}
}
}
}
2. उपयोगकर्ता के साइन-इन की स्थिति को वापस लाने की कोशिश करना
जब आपका ऐप्लिकेशन शुरू होता है, तो restorePreviousSignInWithCallback को कॉल करें. इससे, Google का इस्तेमाल करके पहले से साइन इन कर चुके उपयोगकर्ताओं के साइन-इन की स्थिति को वापस लाने की कोशिश की जा सकती है. ऐसा करने से, उपयोगकर्ताओं को हर बार आपका ऐप्लिकेशन खोलने पर साइन इन नहीं करना पड़ता. हालांकि, अगर उन्होंने साइन आउट किया है, तो उन्हें साइन इन करना होगा.
iOS ऐप्लिकेशन अक्सर ऐसा UIApplicationDelegate के application:didFinishLaunchingWithOptions: तरीके में करते हैं. वहीं, macOS ऐप्लिकेशन, NSApplicationDelegate के applicationDidFinishLaunching: तरीके में ऐसा करते हैं. उपयोगकर्ता को कौनसी व्यू दिखानी है, यह तय करने के लिए नतीजे का इस्तेमाल करें. उदाहरण के लिए:
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 का इस्तेमाल किया जा रहा है, तो अपनी शुरुआती व्यू के लिए onAppear में restorePreviousSignIn को कॉल करें:
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 साइन-इन' बटन जोड़ना
अपने साइन-इन व्यू में, "Google से साइन इन करें" बटन जोड़ें. SwiftUI और UIKit के लिए कॉम्पोनेंट उपलब्ध हैं. ये कॉम्पोनेंट, Google के ब्रैंडिंग से जुड़े दिशा-निर्देशों के मुताबिक बटन अपने-आप जनरेट करते हैं.
SwiftUI का इस्तेमाल करना
पक्का करें कि आपने अपने प्रोजेक्ट में, SwiftUI के "Google से साइन इन करें" बटन के लिए डिपेंडेंसी जोड़ी हो.
जिस फ़ाइल में आपको SwiftUI बटन जोड़ना है, उसके सबसे ऊपर ज़रूरी इंपोर्ट जोड़ें:
import GoogleSignInSwiftप्रज़ेंटेशन के लिए, चालू रूट व्यू कंट्रोलर को वापस पाने के लिए,
UIApplicationएक्सटेंशन जोड़ें:extension UIApplication { // Minimal implementation to retrieve the active root view // controller for presentation. Apps presenting sign-in from deeper // within an existing view hierarchy should ensure they select the // appropriate view controller. var rootViewController: UIViewController? { let windowScene = connectedScenes .compactMap { scene in scene as? UIWindowScene } .first { scene in scene.activationState == .foregroundActive } return windowScene?.windows.first(where: { window in window.isKeyWindow })?.rootViewController } }अपनी व्यू में "Google से साइन इन करें" बटन जोड़ें और वह कार्रवाई तय करें जो बटन दबाने पर कॉल की जाएगी:
GoogleSignInButton(action: handleSignInButton)अपनी कार्रवाई में,
GIDSignInकेsignIn(withPresenting:completion:)तरीके को कॉल करके, बटन दबाने पर साइन इन की प्रोसेस ट्रिगर करें:func handleSignInButton() { guard let rootViewController = UIApplication.shared.rootViewController else { // Handle error return } 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 बनाना होगा. इसके बाद, GoogleSignInButton(viewModel: yourViewModel, action:
yourAction) का इस्तेमाल करके, इसे बटन के
इनिशियलाइज़र में viewModel के तौर पर सेट करना होगा. ज़्यादा जानकारी के लिए,
GoogleSignInButtonViewModel सोर्स कोड
देखें.
UIKit का इस्तेमाल करना
अपने साइन-इन व्यू में, "Google से साइन इन करें" बटन जोड़ें.
GIDSignInButtonक्लास का इस्तेमाल करके, Google ब्रैंडिंग वाला बटन अपने-आप जनरेट किया जा सकता है. हमारा सुझाव है कि आप ऐसा करें. इसके अलावा, पसंद के मुताबिक स्टाइलिंग वाला अपना बटन भी बनाया जा सकता है.स्टोरीबोर्ड या XIB फ़ाइल में
GIDSignInButtonजोड़ने के लिए, एक व्यू जोड़ें और उसकी कस्टम क्लास कोGIDSignInButtonपर सेट करें. ध्यान दें कि जब स्टोरीबोर्ड मेंGIDSignInButtonव्यू जोड़ा जाता है, तो इंटरफ़ेस बिल्डर में साइन-इन बटन रेंडर नहीं होता. साइन-इन बटन देखने के लिए, ऐप्लिकेशन चलाएं.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. साइन-आउट बटन जोड़ना
अपने ऐप्लिकेशन में साइन-आउट बटन जोड़ें. यह बटन, साइन इन किए हुए उपयोगकर्ताओं को दिखना चाहिए. यह बटन, GIDSignIn के signOut तरीके को कॉल करता है.
signOut को कॉल करने से, GIDSignIn में सेव की गई साइन-इन की स्थिति मिट जाती है. साथ ही, Keychain से आपके ऐप्लिकेशन के लिए उपयोगकर्ता के क्रेडेंशियल हट जाते हैं. अपने ऐप्लिकेशन की स्थिति और यूज़र इंटरफ़ेस (यूआई) को अपडेट करने की ज़िम्मेदारी आपकी है. साइन आउट करने की सुविधा सिर्फ़ आपके ऐप्लिकेशन पर लागू होती है. इससे उपयोगकर्ता, अन्य ऐप्लिकेशन या सेवाओं से साइन आउट नहीं होता. साथ ही, इससे आपके ऐप्लिकेशन को दी गई अनुमतियां भी वापस नहीं ली जाती हैं.
SwiftUI का इस्तेमाल करना
SwiftUI में, एक Button जोड़ें जो GIDSignIn.sharedInstance.signOut() को कॉल करता है:
Button("Sign Out") {
GIDSignIn.sharedInstance.signOut()
// Calling signOut() may not automatically trigger UI updates.
// Update your app's state as needed.
}
UIKit का इस्तेमाल करना
बटन को अपने ViewController में मौजूद उस तरीके से कनेक्ट करें जो signOut: को कॉल करता है. उदाहरण के लिए, IBAction का इस्तेमाल करें:
Swift
@IBAction func signOut(sender: Any) {
GIDSignIn.sharedInstance.signOut()
}
Objective-C
- (IBAction)signOut:(id)sender {
[GIDSignIn.sharedInstance signOut];
}
अगले चरण
अब उपयोगकर्ता अपने Google खातों का इस्तेमाल करके, आपके ऐप्लिकेशन में साइन इन कर सकते हैं. अब यह जानें कि: