با احراز هویت Firebase در پلتفرم های اپل شروع کنید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
میتوانید از Firebase Authentication استفاده کنید تا به کاربران اجازه دهید با استفاده از یک یا چند روش ورود به سیستم وارد برنامه شما شوند، از جمله ورود به سیستم آدرس ایمیل و رمز عبور، و ارائهدهندگان هویت فدرال مانند Google Sign-in و Facebook Login. این آموزش با نشان دادن نحوه افزودن آدرس ایمیل و ورود رمز عبور به برنامه خود، شما را با Firebase Authentication شروع می کند.
در کنسول Firebase ، برنامه خود را به پروژه Firebase خود اضافه کنید.
Firebase Authentication به برنامه خود اضافه کنید
برای نصب و مدیریت وابستگی های Firebase از Swift Package Manager استفاده کنید.
در Xcode، با باز بودن پروژه برنامه، به File > Add Packages بروید.
هنگامی که از شما خواسته شد، مخزن SDK پلتفرم های Apple Firebase را اضافه کنید:
https://github.com/firebase/firebase-ios-sdk.git
کتابخانه Firebase Authentication انتخاب کنید.
پرچم -ObjC را به بخش Other Linker Flags تنظیمات ساخت هدف خود اضافه کنید.
پس از اتمام، Xcode به طور خودکار شروع به حل و دانلود وابستگی های شما در پس زمینه می کند.
(اختیاری) نمونه اولیه و آزمایش با Firebase Local Emulator Suite
قبل از صحبت در مورد اینکه برنامه شما چگونه کاربران را احراز هویت میکند، بیایید مجموعهای از ابزارها را معرفی کنیم که میتوانید از آنها برای نمونهسازی اولیه و آزمایش عملکرد Authentication استفاده کنید: Firebase Local Emulator Suite . اگر از میان تکنیکها و ارائهدهندگان احراز هویت تصمیم میگیرید، آزمایش مدلهای مختلف داده با دادههای عمومی و خصوصی با استفاده از Authentication و Firebase Security Rules یا نمونهسازی طرحهای UI ورود به سیستم، امکان کار به صورت محلی بدون استقرار سرویسهای زنده میتواند ایده خوبی باشد.
یک شبیهساز Authentication بخشی از Local Emulator Suite است که به برنامه شما امکان میدهد با محتوای پایگاه داده شبیهسازیشده و پیکربندی، و همچنین بهصورت اختیاری منابع پروژه شبیهسازی شده شما (توابع، سایر پایگاههای داده، و قوانین امنیتی) تعامل داشته باشد.
استفاده از شبیه ساز Authentication فقط شامل چند مرحله است:
افزودن یک خط کد به پیکربندی آزمایشی برنامه برای اتصال به شبیه ساز.
از ریشه دایرکتوری پروژه محلی خود، firebase emulators:start اجرا کنید.
استفاده از Local Emulator Suite UI برای نمونه سازی تعاملی، یا Authentication emulator REST API برای تست غیرتعاملی.
برای هر یک از نماهای برنامه شما که نیاز به اطلاعاتی درباره کاربر وارد شده به سیستم دارند، یک شنونده را به شی FIRAuth متصل کنید. هر زمان که وضعیت ورود به سیستم کاربر تغییر کند، این شنونده تماس می گیرد.
شنونده را در روش viewWillAppear کنترلر view ضمیمه کنید:
فرمی ایجاد کنید که به کاربران جدید امکان می دهد با استفاده از آدرس ایمیل و رمز عبور در برنامه شما ثبت نام کنند. هنگامی که کاربر فرم را تکمیل کرد، آدرس ایمیل و رمز عبور ارائه شده توسط کاربر را تأیید کنید، سپس آنها را به روش createUser ارسال کنید:
فرمی ایجاد کنید که به کاربران موجود اجازه دهد با استفاده از آدرس ایمیل و رمز عبور خود وارد سیستم شوند. وقتی کاربر فرم را تکمیل کرد، روش signIn فراخوانی کنید:
پس از اینکه کاربر با موفقیت وارد سیستم شد، می توانید اطلاعاتی در مورد کاربر دریافت کنید. به عنوان مثال، در شنونده وضعیت احراز هویت :
سویفت
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-03 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-03 بهوقت ساعت هماهنگ جهانی."],[],[],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)"]]