تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يمكنك استخدام Game Center لتسجيل دخول اللاعبين إلى لعبة على منصات Apple تم إنشاؤها باستخدام Firebase. لاستخدام ميزة "تسجيل الدخول باستخدام حساب Game Center" مع Firebase، تأكَّد أولاً من تسجيل دخول اللاعب المحلي باستخدام Game Center، ثم استخدِم العنصر GameCenterAuthProvider لإنشاء بيانات اعتماد Firebase، والتي يمكنك استخدامها لمصادقة حسابك على Firebase.
قبل البدء
استخدِم Swift Package Manager لتثبيت تبعيات Firebase وإدارتها.
في Xcode، مع فتح مشروع تطبيقك، انتقِل إلى ملف > إضافة حِزم.
أضِف مستودع حزمة تطوير البرامج (SDK) لمنصّات Apple من Firebase عندما يُطلب منك ذلك:
أضِف العلامة -ObjC إلى قسم رموز ربط أخرى في إعدادات الإنشاء الخاصة بالهدف.
عند الانتهاء، سيبدأ Xcode تلقائيًا في حلّ ملفاتك المضمّنة وتنزيلها
في الخلفية.
بعد ذلك، عليك تنفيذ بعض خطوات الضبط:
تأكَّد من تسجيل تطبيقك على Apple باستخدام Firebase. ويعني ذلك إدخال
معرّف حِزمة تطبيقك في قسم التسجيل مع معلومات إضافية اختيارية
مثل معرّف متجر التطبيقات ومعرّف الفريق وما إلى ذلك. وسيكون ذلك مطلوبًا لتأكيد هوية جمهور بيانات اعتماد المستخدم في Game Center بشكل آمن قبل
إكمال عملية تسجيل الدخول.
فعِّل Game Center كمقدّم خدمة تسجيل الدخول لمشروعك على Firebase:
تأكَّد من أنّ معرّف الحِزمة الذي تقدّمه إلى iTunes Connect يتطابق مع معرّف الحِزمة الذي
استخدَمته عند ربط تطبيقك بمشروعك على Firebase.
كجزء من عملية دمج Game Center، يمكنك تحديد معالِج مصادقة
يتم استدعاؤه في نقاط متعدّدة من عملية مصادقة Game Center. في
هذا المعالِج، تحقَّق مما إذا كان اللاعب قد سجّل الدخول باستخدام Game Center. إذا كان الأمر كذلك، يمكنك
مواصلة تسجيل الدخول إلى Firebase.
Swift
letlocalPlayer=GKLocalPlayer.localPlayer()localPlayer.authenticateHandler={(gcAuthViewController?,error)inifletgcAuthViewController=gcAuthViewController{// Pause any activities that require user interaction, then present the// gcAuthViewController to the player.}elseiflocalPlayer.isAuthenticated{// Player is signed in to Game Center. Get Firebase credentials from the// player's Game Center credentials (see below).}else{// Error}}
Objective-C
__weakGKLocalPlayer*localPlayer=[GKLocalPlayerlocalPlayer];localPlayer.authenticateHandler=^(UIViewController*gcAuthViewController,NSError*error){if(gcAuthViewController!=nil){// Pause any activities that require user interaction, then present the// gcAuthViewController to the player.}elseif(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،
سجِّل دخول اللاعب إلى لعبتك من خلال إنشاء عنصر AuthCredential باستخدام
GameCenterAuthProvider.getCredential() ونقله إلى
signIn(with:):
Swift
// Get Firebase credentials from the player's Game Center credentialsGameCenterAuthProvider.getCredential(){(credential,error)inifleterror=error{return}// The credential can be used to sign in, or re-auth, or link or unlink.Auth.auth().signIn(with:credential){(user,error)inifleterror=error{return}// Player is signed in!}
Objective-C
// Get Firebase credentials from the player's Game Center credentials[FIRGameCenterAuthProvidergetCredentialWithCompletion:^(FIRAuthCredential*credential,NSError*error){// The credential can be used to sign in, or re-auth, or link or unlink.if(error==nil){[[FIRAuthauth]signInWithCredential:credentialcompletion:^(FIRUser*user,NSError*error){// If error is nil, player is signed in.}];}}];
الخطوات التالية
بعد أن يسجِّل المستخدم الدخول لأول مرة، يتم إنشاء حساب مستخدم جديد وربطه
بمعرّف Game Center. يتم تخزين هذا الحساب الجديد كجزء من
مشروعك على Firebase، ويمكن استخدامه لتحديد مستخدم في كل تطبيق في
مشروعك.
في لعبتك، يمكنك الحصول على معرّف Firebase للمستخدم من عنصر User:
Swift
letuser=Auth.auth().currentUserifletuser=user{letplayerName=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.letuid=user.uid}
Objective-C
FIRUser*user=[FIRAuthauth].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 في الوقت الفعلي" و"مساحة تخزين السحابة الإلكترونية"، يمكنك الحصول على
معرّف المستخدم الفريد للمستخدم الذي سجّل الدخول من المتغيّر auth واستخدامه للتحكّم في البيانات التي يمكن للمستخدم الوصول إليها.
للحصول على معلومات لاعب Game Center الخاصة بالمستخدم أو للوصول إلى خدمات Game Center، استخدِم واجهات برمجة التطبيقات التي يوفّرها Game Kit.
لإلغاء تسجيل مستخدم من Firebase، يمكنك الاتصال Auth.signOut():
تاريخ التعديل الأخير: 2025-08-16 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-08-16 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["You can use Game Center to sign players in to an Apple platforms game built on Firebase. To\nuse Game Center Sign-in with Firebase, first make sure the local player is\nsigned in with Game Center, and then use the `GameCenterAuthProvider` object to\ngenerate a Firebase credential, which you can use to authenticate with Firebase.\n\nBefore you begin\n\nUse Swift Package Manager to install and manage Firebase dependencies.\n| Visit [our installation guide](/docs/ios/installation-methods) to learn about the different ways you can add Firebase SDKs to your Apple project, including importing frameworks directly and using CocoaPods.\n\n1. In Xcode, with your app project open, navigate to **File \\\u003e Add Packages**.\n2. When prompted, add the Firebase Apple platforms SDK repository: \n\n```text\n https://github.com/firebase/firebase-ios-sdk.git\n```\n| **Note:** New projects should use the default (latest) SDK version, but you can choose an older version if needed.\n3. Choose the Firebase Authentication library.\n4. Add the `-ObjC` flag to the *Other Linker Flags* section of your target's build settings.\n5. When finished, Xcode will automatically begin resolving and downloading your dependencies in the background.\n\n| **Deprecated:** Apple has [deprecated the `playerID` field](https://developer.apple.com/documentation/gamekit/gkplayer/1521127-playerid/). The Firebase Apple platforms SDK will use `gamePlayerID` and `teamPlayerID` from version 10.5.0 and onwards. Upgrading to SDK version 10.5.0 or later updates existing integrations that use `playerID` to instead use `gamePlayerID` and `teamPlayerID`. Upgrading existing Game Center Sign-in integrations to SDK version 10.5.0 or later is irreversible. For more details, see [Method: accounts.signInWithGameCenter](https://cloud.google.com/identity-platform/docs/reference/rest/v1/accounts/signInWithGameCenter).\n\nNext, perform some configuration steps:\n\n1. Make sure you register your Apple app with Firebase. This means entering your app's bundle ID in the registration section along with additional optional information such as App Store ID and Team ID, etc. This will be required for securely verifying the audience of the user's Game Center credential before completing sign-in.\n2. Enable Game Center as a sign-in provider for your Firebase project:\n 1. In the [Firebase console](//console.firebase.google.com/), open the **Authentication** section.\n 2. On the **Sign in method** tab, enable the **Game Center** sign-in provider.\n\nIntegrate Game Center Sign-in into your game\n\nFirst, if your game doesn't already use Game Center, follow the instructions in\n[Incorporating Game Center into Your Game](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/GameKit_Guide/GameCenterOverview/GameCenterOverview.html#//apple_ref/doc/uid/TP40008304-CH5-SW22) and\n[Authenticating a Local Player on the Device](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Users/Users.html#//apple_ref/doc/uid/TP40008304-CH8-SW19) on the Apple\ndeveloper site.\n\nBe sure the bundle ID you provide to iTunes Connect matches the bundle ID you\nused when you connected your app to your Firebase project.\n\nAs part of your Game Center integration, you define an authentication handler\nthat is called at multiple points in the Game Center authentication process. In\nthis handler, check if the player is signed in with Game Center. If so, you can\ncontinue to sign in to Firebase. \n\nSwift \n\n```swift\nlet localPlayer = GKLocalPlayer.localPlayer()\nlocalPlayer.authenticateHandler = { (gcAuthViewController?, error) in\n if let gcAuthViewController = gcAuthViewController {\n // Pause any activities that require user interaction, then present the\n // gcAuthViewController to the player.\n } else if localPlayer.isAuthenticated {\n // Player is signed in to Game Center. Get Firebase credentials from the\n // player's Game Center credentials (see below).\n } else {\n // Error\n }\n}\n```\n\nObjective-C \n\n```objective-c\n__weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];\nlocalPlayer.authenticateHandler = ^(UIViewController *gcAuthViewController,\n NSError *error) {\n if (gcAuthViewController != nil) {\n // Pause any activities that require user interaction, then present the\n // gcAuthViewController to the player.\n } else if (localPlayer.isAuthenticated) {\n // Player is signed in to Game Center. Get Firebase credentials from the\n // player's Game Center credentials (see below).\n } else {\n // Error\n }\n};\n```\n\nAuthenticate with Firebase\n\nAfter you determine that the local player has signed in with Game Center,\nsign the player in to your game by creating an `AuthCredential` object with\n`GameCenterAuthProvider.getCredential()` and passing that object to\n`signIn(with:)`: \n\nSwift \n\n```swift\n// Get Firebase credentials from the player's Game Center credentials\nGameCenterAuthProvider.getCredential() { (credential, error) in\n if let error = error {\n return\n }\n // The credential can be used to sign in, or re-auth, or link or unlink.\n Auth.auth().signIn(with:credential) { (user, error) in\n if let error = error {\n return\n }\n // Player is signed in!\n }\n```\n\nObjective-C \n\n```objective-c\n// Get Firebase credentials from the player's Game Center credentials\n[FIRGameCenterAuthProvider getCredentialWithCompletion:^(FIRAuthCredential *credential,\n NSError *error) {\n // The credential can be used to sign in, or re-auth, or link or unlink.\n if (error == nil) {\n [[FIRAuth auth] signInWithCredential:credential\n completion:^(FIRUser *user, NSError *error) {\n // If error is nil, player is signed in.\n }];\n }\n}];\n```\n\nNext steps\n\nAfter a user signs in for the first time, a new user account is created and\nlinked to their Game Center ID. This new account is stored as part of your\nFirebase project, and can be used to identify a user across every app in your\nproject.\n\nIn your game, you can get the user's Firebase UID from the `User` object: \n\nSwift \n\n```swift\nlet user = Auth.auth().currentUser\nif let user = user {\n let playerName = user.displayName\n\n // The user's ID, unique to the Firebase project.\n // Do NOT use this value to authenticate with your backend server,\n // if you have one. Use getToken(with:) instead.\n let uid = user.uid\n}\n```\n\nObjective-C \n\n```objective-c\nFIRUser *user = [FIRAuth auth].currentUser;\nif (user) {\n NSString *playerName = user.displayName;\n\n // The user's ID, unique to the Firebase project.\n // Do NOT use this value to authenticate with your backend server,\n // if you have one. Use getTokenWithCompletion:completion: instead.\n NSString *uid = user.uid;\n}\n```\n\nIn your Firebase Realtime Database and Cloud Storage Security Rules, you can get\nthe signed-in user's unique user ID from the `auth` variable, and use it to\ncontrol what data a user can access.\n\nTo get a user's Game Center player information or to access Game Center\nservices, use the APIs provided by [Game Kit](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Introduction/Introduction.html).\n\nTo sign a user out of Firebase, call `Auth.signOut()`: \n\nSwift \n\n```swift\nlet firebaseAuth = Auth.auth()\ndo {\n try firebaseAuth.signOut()\n} catch let signOutError as NSError {\n print (\"Error signing out: %@\", signOutError)\n}\n```\n\nObjective-C \n\n```objective-c\nNSError *signOutError;\nBOOL status = [[FIRAuth auth] signOut:&signOutError];\nif (!status) {\n NSLog(@\"Error signing out: %@\", signOutError);\n return;\n}\n```"]]