इस पेज पर, iOS या macOS ऐप्लिकेशन में Google साइन-इन को इंटिग्रेट करने का तरीका बताया गया है. आपको अपने ऐप्लिकेशन के लाइफ़साइकल या यूज़र इंटरफ़ेस (यूआई) मॉडल के हिसाब से इन निर्देशों में बदलाव करना पड़ सकता है.
शुरू करने से पहले
डिपेंडेंसी डाउनलोड करें, 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
अपने ऐप्लिकेशन के 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
अपने ऐप्लिकेशन की विंडो या सीन में, यूआरएल पाने और 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's
application:didFinishLaunchingWithOptions: तरीके में करते हैं. वहीं, macOS ऐप्लिकेशन के लिए NSApplicationDelegate's 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अपने व्यू में "Google से साइन इन करें" बटन जोड़ें. साथ ही, यह तय करें कि बटन दबाने पर कौनसी कार्रवाई की जाएगी:
GoogleSignInButton(action: handleSignInButton)जब उपयोगकर्ता बटन दबाए, तब साइन इन करने की प्रोसेस ट्रिगर करें. इसके लिए, अपनी कार्रवाई में
GIDSignIn'ssignIn(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 का इस्तेमाल करना
साइन-इन व्यू में "Google से साइन इन करें" बटन जोड़ें. Google की ब्रैंडिंग वाला बटन अपने-आप जनरेट करने के लिए,
GIDSignInButtonक्लास का इस्तेमाल करें (सुझाया गया). इसके अलावा, कस्टम स्टाइल वाला बटन भी बनाया जा सकता है.किसी स्टोरीबोर्ड या XIB फ़ाइल में
GIDSignInButtonजोड़ने के लिए, एक व्यू जोड़ें और उसके कस्टम क्लास कोGIDSignInButtonपर सेट करें. ध्यान दें कि स्टोरीबोर्ड मेंGIDSignInButtonव्यू जोड़ने पर, साइन इन बटन इंटरफ़ेस बिल्डर में रेंडर नहीं होता. साइन-इन बटन देखने के लिए, ऐप्लिकेशन चलाएं.colorSchemeऔरstyleप्रॉपर्टी सेट करके,GIDSignInButtonके दिखने के तरीके को पसंद के मुताबिक बनाया जा सकता है: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. साइन आउट करने का बटन जोड़ना
अपने ऐप्लिकेशन में साइन आउट करने का बटन जोड़ें, जो साइन इन किए हुए उपयोगकर्ताओं को दिखे.
बटन को अपने ViewController में मौजूद उस तरीके से कनेक्ट करें जो
signOut:को कॉल करता है. उदाहरण के लिए,IBActionका इस्तेमाल करें:Swift
@IBAction func signOut(sender: Any) { GIDSignIn.sharedInstance.signOut() }Objective-C
- (IBAction)signOut:(id)sender { [GIDSignIn.sharedInstance signOut]; }
अगले चरण
अब उपयोगकर्ता अपने Google खातों का इस्तेमाल करके, आपके ऐप्लिकेशन में साइन इन कर सकते हैं. इसलिए, यहां दिए गए काम करने का तरीका जानें:
- लोगों के Google खाते की प्रोफ़ाइल की जानकारी पाना.
- उपयोगकर्ता के Google आईडी टोकन का इस्तेमाल करके, अपने बैकएंड से पुष्टि करें.
- उपयोगकर्ता की ओर से Google API को कॉल करें.