'Google साइन इन' को अपने iOS या macOS ऐप्लिकेशन में इंटिग्रेट करना

इस पेज पर, iOS या macOS ऐप्लिकेशन में 'Google साइन इन' को इंटिग्रेट करने का तरीका बताया गया है. आपको अपने ऐप्लिकेशन की लाइफ़साइकल या यूज़र इंटरफ़ेस (यूआई) मॉडल के हिसाब से, इन निर्देशों में बदलाव करना पड़ सकता है.

शुरू करने से पहले

डिपेंडेंसी डाउनलोड करें, अपना Xcode प्रोजेक्ट कॉन्फ़िगर करें, और अपना क्लाइंट आईडी सेट करें.

साइन-इन कैसे काम करता है, यह देखने के लिए हमारा iOS और macOS सैंपल ऐप्लिकेशन आज़माएं.

1. पुष्टि करने वाले दूसरे वेबलिंक को मैनेज करना

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

  1. आपके ऐप्लिकेशन के 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];
    }
    
  2. 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

अपने ऐप्लिकेशन की विंडो या सीन में, यूआरएल पाने के लिए एक हैंडलर रजिस्टर करें और GIDSignIns 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 का इस्तेमाल करना

  1. पक्का करें कि आपने अपने प्रोजेक्ट में, SwiftUI के "Google से साइन इन करें" बटन की डिपेंडेंसी जोड़ी हो.

  2. जिस फ़ाइल में आपको SwiftUI बटन जोड़ना है, उसमें ज़रूरी इंपोर्ट को फ़ाइल में सबसे ऊपर जोड़ें:

    import GoogleSignInSwift
    
  3. अपने व्यू में "Google से साइन इन करें" बटन जोड़ें और उस कार्रवाई को चुनें जिसे बटन दबाए जाने पर कॉल किया जाएगा:

    GoogleSignInButton(action: handleSignInButton)
    
  4. बटन दबाने पर, साइन इन करने की प्रोसेस को ट्रिगर किया जा सकता है. इसके लिए, GIDSignIn के signIn(presentingViewController:completion:) वाले तरीके का इस्तेमाल करके कॉल जोड़ें:

    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 बनाना होगा. साथ ही, GoogleSignInButton(viewModel: yourViewModel, action: yourAction) का इस्तेमाल करके, बटन के शुरू करने वाले टूल में इसे viewModel के तौर पर सेट करना होगा. ज़्यादा जानकारी के लिए, GoogleSignInButtonViewModel सोर्स कोड देखें.

यूज़र इंटरफ़ेस (यूआई) का इस्तेमाल करना

  1. अपने साइन-इन व्यू में, "Google से साइन इन करें" बटन जोड़ें. Google ब्रैंडिंग (इसका सुझाव दिया जाता है) की मदद से बटन अपने-आप जनरेट करने के लिए, GIDSignInButton क्लास का इस्तेमाल किया जा सकता है या पसंद के मुताबिक स्टाइलिंग वाला अपना बटन बनाया जा सकता है.

    GIDSignInButton को स्टोरीबोर्ड या XIB फ़ाइल में जोड़ने के लिए, व्यू जोड़ें और उसकी कस्टम क्लास को GIDSignInButton पर सेट करें. ध्यान दें कि जब स्टोरीबोर्ड में GIDSignInButton व्यू जोड़ा जाता है, तो इंटरफ़ेस बिल्डर में 'साइन-इन करें' बटन नहीं दिखता. 'साइन इन करें' बटन देखने के लिए ऐप्लिकेशन चलाएं.

    GIDSignInButton की colorScheme और style प्रॉपर्टी को सेट करके, GIDSignInButton की लुक को पसंद के मुताबिक बनाया जा सकता है:

    GIDSignInButton स्टाइल प्रॉपर्टी
    colorScheme kGIDSignInButtonColorSchemeLight
    kGIDSignInButtonColorSchemeDark
    style kGIDSignInButtonStyleStandard
    kGIDSignInButtonStyleWide
    kGIDSignInButtonStyleIconOnly
  2. बटन को अपने 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. 'साइन-आउट करें' बटन जोड़ें

  1. अपने ऐप्लिकेशन में 'साइन-आउट करें' बटन जोड़ें. यह बटन, साइन इन किए हुए लोगों को दिखता है.

  2. बटन को अपने ViewController के उस तरीके से कनेक्ट करें जो signOut: को कॉल करता हो. उदाहरण के लिए, किसी IBAction का इस्तेमाल करें:

    Swift

    @IBAction func signOut(sender: Any) {
      GIDSignIn.sharedInstance.signOut()
    }
    

    Objective-C

    - (IBAction)signOut:(id)sender {
      [GIDSignIn.sharedInstance signOut];
    }
    

अगले चरण

अब जब उपयोगकर्ता अपने Google खातों से आपके ऐप्लिकेशन में साइन इन कर सकते हैं, तो यह करने का तरीका जानें: