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

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

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

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

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];
    }
    

स्विफ़्टयूज़र इंटरफ़ेस (यूआई)

अपने ऐप्लिकेशन की विंडो या सीन में, यूआरएल पाने और 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: तरीके और NSApplicationDelegate की applicationDidFinishLaunching: 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 का इस्तेमाल कर रहे हैं, तो शुरुआती व्यू के लिए 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. पक्का करें कि आपने अपने प्रोजेक्ट में स्विफ़्टयूआई "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 क्लास का इस्तेमाल किया जा सकता है. इसके अलावा, अपनी पसंद के मुताबिक स्टाइल बनाकर, अपने हिसाब से बटन भी बनाया जा सकता है.

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

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

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