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

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

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

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

1. पुष्टि करने वाले रीडायरेक्ट यूआरएल को मैनेज करना

iOS: UIApplication Relief

अपने App Relief के 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: NSApplication प्रतिशत

  1. अपने ऐप्लिकेशन के Appडेल से, 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 और यूज़र इंटरफ़ेस (यूआई) के लिए कॉम्पोनेंट उपलब्ध हैं. ये कॉम्पोनेंट 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 सोर्स कोड देखें.

UIKit का इस्तेमाल करना

  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 खातों का इस्तेमाल करके आपके ऐप्लिकेशन में साइन इन कर सकते हैं, तो यह जानें: