تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يمكنك استخدام Firebase Authentication للسماح للمستخدمين بتسجيل الدخول إلى تطبيقك باستخدام طريقة واحدة أو أكثر من طرق تسجيل الدخول، بما في ذلك تسجيل الدخول باستخدام عنوان البريد الإلكتروني وكلمة المرور، وموفّري خدمات الهوية الموحّدة، مثل "تسجيل الدخول باستخدام حساب Google" و"تسجيل الدخول باستخدام حساب Facebook". يساعدك هذا البرنامج التعليمي في بدء استخدام Firebase Authentication من خلال توضيح كيفية إضافة ميزة تسجيل الدخول باستخدام عنوان البريد الإلكتروني وكلمة المرور إلى تطبيقك.
في وحدة تحكّم Firebase، أضِف تطبيقك إلى مشروعك على Firebase.
إضافة Firebase Authentication إلى تطبيقك
استخدِم أداة Swift Package Manager لتثبيت الملحقات التابعة لـ Firebase وإدارتها.
في Xcode، بعد فتح مشروع تطبيقك، انتقِل إلى ملف (File) > إضافة حِزم (Add Packages).
عندما يُطلب منك ذلك، أضِف مستودع حزمة تطوير البرامج (SDK) لمنصة Firebase على أجهزة Apple:
https://github.com/firebase/firebase-ios-sdk.git
اختَر مكتبة Firebase Authentication.
أضِف العلامة -ObjC إلى قسم علامات الرابط الأخرى في إعدادات الإصدار الخاص بالكائن المستهدف.
بعد الانتهاء، سيبدأ Xcode تلقائيًا في حلّ التبعيات وتنزيلها في الخلفية.
(اختياري) إنشاء نموذج أوّلي واختباره باستخدام Firebase Local Emulator Suite
قبل التحدّث عن كيفية مصادقة تطبيقك للمستخدمين، دعنا نقدّم مجموعة من الأدوات التي يمكنك استخدامها لإنشاء نماذج أولية واختبار وظيفة Authentication:
Firebase Local Emulator Suite. إذا كنت بصدد الاختيار بين تقنيات وموفّري خدمات المصادقة، أو تجربة نماذج بيانات مختلفة باستخدام بيانات عامة وخاصة باستخدام Authentication وFirebase Security Rules، أو إنشاء نماذج أولية لتصاميم واجهة مستخدم تسجيل الدخول، قد يكون من المفيد أن تتمكّن من العمل محليًا بدون نشر الخدمات المباشرة.
يُعدّ محاكي Authentication جزءًا من Local Emulator Suite، ما يتيح لتطبيقك التفاعل مع المحتوى والإعدادات المحاكية لقاعدة البيانات، بالإضافة إلى موارد مشروعك المحاكية (الدوال وقواعد البيانات الأخرى وقواعد الأمان) بشكل اختياري.
لا يتطلّب استخدام محاكي Authentication سوى بضع خطوات:
إضافة سطر من الرمز البرمجي إلى إعدادات الاختبار في تطبيقك للاتصال بالمحاكي
من جذر دليل مشروعك المحلي، شغِّل firebase emulators:start.
استخدام واجهة مستخدم Local Emulator Suite لإنشاء نماذج أولية تفاعلية، أو استخدام واجهة برمجة تطبيقات REST لمحاكي Authentication لإجراء اختبارات غير تفاعلية
لكل طريقة عرض في تطبيقك تحتاج إلى معلومات عن المستخدم الذي سجّل الدخول،
أضِف أداة معالجة إلى العنصر FIRAuth. يتم استدعاء أداة معالجة الأحداث هذه كلما تغيّرت حالة تسجيل الدخول للمستخدم.
أرفِق أداة معالجة الأحداث بطريقة viewWillAppear في وحدة التحكّم في العرض:
أنشئ نموذجًا يتيح للمستخدمين الجدد التسجيل في تطبيقك باستخدام عنوان بريدهم الإلكتروني وكلمة مرور. عندما يكمل المستخدم النموذج، تحقَّق من صحة عنوان البريد الإلكتروني وكلمة المرور اللذين قدّمهما، ثم مرِّرهما إلى الطريقة createUser:
بعد أن يسجّل المستخدم الدخول بنجاح، يمكنك الحصول على معلومات عنه. على سبيل المثال، في برنامج معالجة حالة المصادقة:
Swift
ifletuser=user{// 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.letuid=user.uidletemail=user.emailletphotoURL=user.photoURLvarmultiFactorString="MultiFactor: "forinfoinuser.multiFactor.enrolledFactors{multiFactorString+=info.displayName??"[DispayName]"multiFactorString+=" "}// ...}
if(user){// 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*email=user.email;NSString*uid=user.uid;NSMutableString*multiFactorString=[NSMutableStringstringWithFormat:@"MultiFactor: "];for(FIRMultiFactorInfo*infoinuser.multiFactor.enrolledFactors){[multiFactorStringappendString:info.displayName];[multiFactorStringappendString:@" "];}NSURL*photoURL=user.photoURL;// ...}
تاريخ التعديل الأخير: 2025-09-04 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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-09-04 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["You can use Firebase Authentication to allow users to sign in to your app using one or\nmore sign-in methods, including email address and password sign-in, and\nfederated identity providers such as Google Sign-in and Facebook Login. This\ntutorial gets you started with Firebase Authentication by showing you how to add\nemail address and password sign-in to your app.\n\nConnect your app to Firebase \n\n1. [Install the Firebase SDK](/docs/ios/setup).\n2. In the [Firebase console](//console.firebase.google.com/), add your app to your Firebase project.\n\nAdd Firebase Authentication to your app\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| **Important:** For Mac apps, enable the Keychain Sharing capability so the SDK has permission to store user entries in the keychain. For more information, see the [FAQ guide](/docs/ios/troubleshooting-faq#macos-keychain-sharing).\n\n(Optional) Prototype and test with Firebase Local Emulator Suite\n\nBefore talking about how your app authenticates users, let's introduce a set of\ntools you can use to prototype and test Authentication functionality:\nFirebase Local Emulator Suite. If you're deciding among authentication techniques\nand providers, trying out different data models with public and private data\nusing Authentication and Firebase Security Rules, or prototyping sign-in UI designs, being able to\nwork locally without deploying live services can be a great idea.\n\nAn Authentication emulator is part of the Local Emulator Suite, which\nenables your app to interact with emulated database content and config, as\nwell as optionally your emulated project resources (functions, other databases,\nand security rules).\n\nUsing the Authentication emulator involves just a few steps:\n\n1. Adding a line of code to your app's test config to connect to the emulator.\n2. From the root of your local project directory, running `firebase emulators:start`.\n3. Using the Local Emulator Suite UI for interactive prototyping, or the Authentication emulator REST API for non-interactive testing.\n\nA detailed guide is available at [Connect your app to the Authentication emulator](/docs/emulator-suite/connect_auth).\nFor more information, see the [Local Emulator Suite introduction](/docs/emulator-suite).\n\nNow let's continue with how to authenticate users.\n\nInitialize the Firebase SDK\n\nIn your app delegate, first import the Firebase SDK: \n\nSwift \n\n import FirebaseCore \n https://github.com/firebase/quickstart-ios/blob/6e483be884a13ffe8d6258f1a626662fa9e7d837/authentication/LegacyAuthQuickstart/AuthenticationExampleSwift/AppDelegate.swift#L20-L20\n\nObjective-C \n\n @import FirebaseCore; \n https://github.com/firebase/quickstart-ios/blob/6e483be884a13ffe8d6258f1a626662fa9e7d837/authentication/LegacyAuthQuickstart/AuthenticationExample/AppDelegate.m#L21-L21\n\nThen, in the `application:didFinishLaunchingWithOptions:` method, initialize the\n`FirebaseApp` object: \n\nSwift \n\n // Use Firebase library to configure APIs\n FirebaseApp.configure() \n https://github.com/firebase/quickstart-ios/blob/6e483be884a13ffe8d6258f1a626662fa9e7d837/authentication/LegacyAuthQuickstart/AuthenticationExampleSwift/AppDelegate.swift#L40-L41\n\nObjective-C \n\n // Use Firebase library to configure APIs\n [FIRApp configure]; \n https://github.com/firebase/quickstart-ios/blob/6e483be884a13ffe8d6258f1a626662fa9e7d837/authentication/LegacyAuthQuickstart/AuthenticationExample/AppDelegate.m#L34-L35\n\nListen for authentication state\n\nFor each of your app's views that need information about the signed-in user,\nattach a listener to the `FIRAuth` object. This listener gets called whenever\nthe user's sign-in state changes.\n\nAttach the listener in the view controller's `viewWillAppear` method: \n\nSwift \n\n handle = Auth.auth().addStateDidChangeListener { auth, user in\n // ...\n } \n https://github.com/firebase/quickstart-ios/blob/6e483be884a13ffe8d6258f1a626662fa9e7d837/authentication/LegacyAuthQuickstart/AuthenticationExampleSwift/MainViewController.swift#L505-L510\n\nObjective-C \n\n self.handle = [[FIRAuth auth]\n addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth, FIRUser *_Nullable user) {\n // ...\n }]; \n https://github.com/firebase/quickstart-ios/blob/6e483be884a13ffe8d6258f1a626662fa9e7d837/authentication/LegacyAuthQuickstart/AuthenticationExample/MainViewController.m#L575-L581\n\nAnd detach the listener in the view controller's `viewWillDisappear` method: \n\nSwift \n\n Auth.auth().removeStateDidChangeListener(handle!) \n https://github.com/firebase/quickstart-ios/blob/6e483be884a13ffe8d6258f1a626662fa9e7d837/authentication/LegacyAuthQuickstart/AuthenticationExampleSwift/MainViewController.swift#L523-L523\n\nObjective-C \n\n [[FIRAuth auth] removeAuthStateDidChangeListener:_handle]; \n https://github.com/firebase/quickstart-ios/blob/6e483be884a13ffe8d6258f1a626662fa9e7d837/authentication/LegacyAuthQuickstart/AuthenticationExample/MainViewController.m#L604-L604\n\nSign up new users\n\nCreate a form that allows new users to register with your app using their email\naddress and a password. When a user completes the form, validate the email\naddress and password provided by the user, then pass them to the `createUser`\nmethod: \n\nSwift \n\n Auth.auth().createUser(withEmail: email, password: password) { authResult, error in\n // ...\n } \n https://github.com/firebase/quickstart-ios/blob/6e483be884a13ffe8d6258f1a626662fa9e7d837/authentication/LegacyAuthQuickstart/AuthenticationExampleSwift/EmailViewController.swift#L173-L184\n\nObjective-C \n\n [[FIRAuth auth] createUserWithEmail:email\n password:password\n completion:^(FIRAuthDataResult * _Nullable authResult,\n NSError * _Nullable error) {\n // ...\n }]; \n https://github.com/firebase/quickstart-ios/blob/6e483be884a13ffe8d6258f1a626662fa9e7d837/authentication/LegacyAuthQuickstart/AuthenticationExample/EmailViewController.m#L170-L184\n\nSign in existing users\n\nCreate a form that allows existing users to sign in using their email address\nand password. When a user completes the form, call the `signIn` method: \n\nSwift \n\n Auth.auth().signIn(withEmail: email, password: password) { [weak self] authResult, error in\n guard let strongSelf = self else { return }\n // ...\n } \n https://github.com/firebase/quickstart-ios/blob/6e483be884a13ffe8d6258f1a626662fa9e7d837/authentication/LegacyAuthQuickstart/AuthenticationExampleSwift/EmailViewController.swift#L37-L99\n\nObjective-C \n\n [[FIRAuth auth] signInWithEmail:self-\u003e_emailField.text\n password:self-\u003e_passwordField.text\n completion:^(FIRAuthDataResult * _Nullable authResult,\n NSError * _Nullable error) {\n // ...\n }]; \n https://github.com/firebase/quickstart-ios/blob/6e483be884a13ffe8d6258f1a626662fa9e7d837/authentication/LegacyAuthQuickstart/AuthenticationExample/EmailViewController.m#L36-L89\n\nGet user information\n\nAfter a user signs in successfully, you can get information about the user. For\nexample, in your [authentication state listener](#listen_for_authentication_state): \n\nSwift \n\n if let user = user {\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 let uid = user.uid\n let email = user.email\n let photoURL = user.photoURL\n var multiFactorString = \"MultiFactor: \"\n for info in user.multiFactor.enrolledFactors {\n multiFactorString += info.displayName ?? \"[DispayName]\"\n multiFactorString += \" \"\n }\n // ...\n } \n https://github.com/firebase/quickstart-ios/blob/6e483be884a13ffe8d6258f1a626662fa9e7d837/authentication/LegacyAuthQuickstart/AuthenticationExampleSwift/MainViewController.swift#L590-L636\n\nObjective-C \n\n if (user) {\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 *email = user.email;\n NSString *uid = user.uid;\n NSMutableString *multiFactorString = [NSMutableString stringWithFormat:@\"MultiFactor: \"];\n for (FIRMultiFactorInfo *info in user.multiFactor.enrolledFactors) {\n [multiFactorString appendString:info.displayName];\n [multiFactorString appendString:@\" \"];\n }\n NSURL *photoURL = user.photoURL;\n // ...\n } \n https://github.com/firebase/quickstart-ios/blob/6e483be884a13ffe8d6258f1a626662fa9e7d837/authentication/LegacyAuthQuickstart/AuthenticationExample/MainViewController.m#L647-L688\n\nNext steps\n\nLearn how to add support for other identity providers and anonymous guest\naccounts:\n\n- [Google Sign-in](/docs/auth/ios/google-signin)\n- [Facebook Login](/docs/auth/ios/facebook-login)\n- [Twitter Login](/docs/auth/ios/twitter-login)\n- [GitHub Login](/docs/auth/ios/github-auth)\n- [Anonymous sign-in](/docs/auth/ios/anonymous-auth)"]]