गेम सेंटर का इस्तेमाल करके पुष्टि करें

Firebase पर बने Apple प्लैटफ़ॉर्म गेम में खिलाड़ियों को साइन इन करने के लिए, Game Center का इस्तेमाल किया जा सकता है. Firebase के साथ गेम सेंटर में साइन इन करने के लिए, सबसे पहले पक्का करें कि लोकल प्लेयर ने Game Center से साइन इन किया हो. इसके बाद, GameCenterAuthProvider ऑब्जेक्ट का इस्तेमाल करके Firebase क्रेडेंशियल जनरेट करें. इसका इस्तेमाल Firebase से पुष्टि करने के लिए किया जा सकता है.

वेब कंटेनर इंस्टॉल करने से पहले

Firebase डिपेंडेंसी इंस्टॉल और मैनेज करने के लिए, Swift पैकेज मैनेजर का इस्तेमाल करें.

  1. Xcode में, अपना ऐप्लिकेशन प्रोजेक्ट खोलने के लिए, फ़ाइल > पैकेज जोड़ें पर जाएं.
  2. जब कहा जाए, तब Firebase Apple प्लैटफ़ॉर्म SDK टूल का रिपॉज़िटरी जोड़ें:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. Firebase से पुष्टि करने की लाइब्रेरी चुनें.
  5. अपने टारगेट की बिल्ड सेटिंग के अन्य लिंकर फ़्लैग सेक्शन में -ObjC फ़्लैग जोड़ें.
  6. यह काम पूरा होने के बाद, Xcode बैकग्राउंड में आपकी डिपेंडेंसी को अपने-आप रिज़ॉल्व और डाउनलोड करना शुरू कर देगा.

इसके बाद, कॉन्फ़िगरेशन के कुछ चरण पूरे करें:

  1. पक्का करें कि आपने Apple ऐप्लिकेशन को Firebase के साथ रजिस्टर किया हो. इसका मतलब है कि रजिस्ट्रेशन सेक्शन में अपने ऐप्लिकेशन के बंडल आईडी के साथ-साथ, अन्य वैकल्पिक जानकारी भी डालनी होगी. जैसे, App Store आईडी और टीम आईडी वगैरह. ऐसा करना इसलिए ज़रूरी है, ताकि साइन-इन करने से पहले, उपयोगकर्ता के गेम सेंटर क्रेडेंशियल की ऑडियंस की सुरक्षित तरीके से पुष्टि की जा सके.
  2. अपने Firebase प्रोजेक्ट के लिए, गेम सेंटर को साइन इन करने की सेवा देने वाली कंपनी के तौर पर चालू करें:
    1. Firebase कंसोल में, पुष्टि करने की सुविधा वाला सेक्शन खोलें.
    2. साइन इन करने का तरीका टैब पर, गेम सेंटर में साइन इन करने की सेवा देने वाली कंपनी को चालू करें.

अपने गेम में गेम सेंटर साइन-इन को इंटिग्रेट करें

अगर आपके गेम में पहले से ही Game Center का इस्तेमाल नहीं किया जा रहा है, तो सबसे पहले अपने गेम में गेम सेंटर को शामिल करने और Apple की डेवलपर साइट पर डिवाइस पर किसी लोकल प्लेयर की पुष्टि करने में दिए गए निर्देशों का पालन करें.

पक्का करें कि iTunes Connect को आपने जो बंडल आईडी दिया है वह ऐप्लिकेशन को Firebase प्रोजेक्ट से कनेक्ट करते समय इस्तेमाल किए गए बंडल आईडी से मेल खाता हो.

अपने गेम सेंटर के इंटिग्रेशन में, आप पुष्टि करने वाला ऐसा हैंडलर तय करते हैं जिसे Game Center की पुष्टि करने की प्रोसेस में कई पॉइंट पर कॉल किया जाता है. इस हैंडलर में, देखें कि खिलाड़ी ने Game Center में साइन इन किया है या नहीं. अगर ऐसा है, तो Firebase में साइन इन करना जारी रखा जा सकता है.

Swift

let localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = { (gcAuthViewController?, error) in
  if let gcAuthViewController = gcAuthViewController {
    // Pause any activities that require user interaction, then present the
    // gcAuthViewController to the player.
  } else if localPlayer.isAuthenticated {
    // Player is signed in to Game Center. Get Firebase credentials from the
    // player's Game Center credentials (see below).
  } else {
    // Error
  }
}

Objective-C

__weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *gcAuthViewController,
                                    NSError *error) {
  if (gcAuthViewController != nil) {
    // Pause any activities that require user interaction, then present the
    // gcAuthViewController to the player.
  } else if (localPlayer.isAuthenticated) {
    // Player is signed in to Game Center. Get Firebase credentials from the
    // player's Game Center credentials (see below).
  } else {
    // Error
  }
};

Firebase की मदद से पुष्टि करें

यह तय करने के बाद कि स्थानीय खिलाड़ी ने Game Center से साइन इन किया है, GameCenterAuthProvider.getCredential() की मदद से AuthCredential ऑब्जेक्ट बनाकर प्लेयर में साइन इन करें और उस ऑब्जेक्ट को signIn(with:) को दें:

Swift

// Get Firebase credentials from the player's Game Center credentials
GameCenterAuthProvider.getCredential() { (credential, error) in
  if let error = error {
    return
  }
  // The credential can be used to sign in, or re-auth, or link or unlink.
  Auth.auth().signIn(with:credential) { (user, error) in
    if let error = error {
      return
    }
    // Player is signed in!
  }

Objective-C

// Get Firebase credentials from the player's Game Center credentials
[FIRGameCenterAuthProvider getCredentialWithCompletion:^(FIRAuthCredential *credential,
                                                         NSError *error) {
  // The credential can be used to sign in, or re-auth, or link or unlink.
  if (error == nil) {
    [[FIRAuth auth] signInWithCredential:credential
                              completion:^(FIRUser *user, NSError *error) {
      // If error is nil, player is signed in.
    }];
  }
}];

अगले चरण

जब कोई उपयोगकर्ता पहली बार साइन इन करता है, तो एक नया उपयोगकर्ता खाता बन जाता है और उनके गेम सेंटर के आईडी से लिंक हो जाता है. यह नया खाता आपके Firebase प्रोजेक्ट के हिस्से के तौर पर सेव किया जाता है. इसका इस्तेमाल, प्रोजेक्ट के हर ऐप्लिकेशन में उपयोगकर्ता की पहचान करने के लिए किया जा सकता है.

अपने गेम में, User ऑब्जेक्ट से उपयोगकर्ता का Firebase यूआईडी पाया जा सकता है:

Swift

let user = Auth.auth().currentUser
if let user = user {
  let playerName = user.displayName

  // The user's ID, unique to the Firebase project.
  // Do NOT use this value to authenticate with your backend server,
  // if you have one. Use getToken(with:) instead.
  let uid = user.uid
}

Objective-C

FIRUser *user = [FIRAuth auth].currentUser;
if (user) {
  NSString *playerName = user.displayName;

  // The user's ID, unique to the Firebase project.
  // Do NOT use this value to authenticate with your backend server,
  // if you have one. Use getTokenWithCompletion:completion: instead.
  NSString *uid = user.uid;
}

अपने Firebase रीयल टाइम डेटाबेस और Cloud Storage के सुरक्षा नियमों में, आपको auth वैरिएबल से साइन-इन किए हुए उपयोगकर्ता का यूनीक यूज़र आईडी मिल सकता है. साथ ही, इसका इस्तेमाल यह कंट्रोल करने के लिए किया जा सकता है कि उपयोगकर्ता कौनसा डेटा ऐक्सेस कर सकता है.

उपयोगकर्ता के गेम सेंटर के प्लेयर की जानकारी पाने या गेम सेंटर की सेवाएं ऐक्सेस करने के लिए, Game Kit से मिले एपीआई का इस्तेमाल करें.

किसी उपयोगकर्ता को Firebase से साइन आउट करने के लिए, Auth.signOut() पर कॉल करें:

Swift

let firebaseAuth = Auth.auth()
do {
  try firebaseAuth.signOut()
} catch let signOutError as NSError {
  print ("Error signing out: %@", signOutError)
}

Objective-C

NSError *signOutError;
BOOL status = [[FIRAuth auth] signOut:&signOutError];
if (!status) {
  NSLog(@"Error signing out: %@", signOutError);
  return;
}