تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
ستساعدك الأمثلة التالية في تنفيذ Instance ID في جهاز iOS.
يُرجى العِلم أنّ هذه الأمثلة تستخدم نطاق GCM الذي يمكنك استخدامه لإدارة الرموز المميّزة لعميل iOS في المراسلة عبر السحابة الإلكترونية من Firebase.
إعداد ملحقات CocoaPods
يستخدم معرّف المثيل CocoaPods لتثبيت الملحقات وإدارتها. افتح نافذة أوامر طرفية وانتقِل إلى موقع مشروع Xcode لتطبيقك. إذا لم يسبق لك إنشاء Podfile لتطبيقك، أنشئ واحدًا الآن:
pod init
افتح ملف Podfile الذي تم إنشاؤه لتطبيقك وأضِف ما يلي:
pod 'FirebaseInstanceId'
احفظ الملف وشغِّل:
pod install
يؤدي هذا إلى إنشاء ملف .xcworkspace لتطبيقك. استخدِم هذا الملف لكل عمليات التطوير المستقبلية في تطبيقك.
NSString*authorizedEntity=PROJECT_ID;String*scope=kFIRInstanceIDScopeFirebaseMessaging;NSDictionary*options=@{@"apns_token":<APNSTokendata>,// 1 if APNS sandbox token else 0@"apns_sandbox":@(1),};[[FIRInstanceIDinstanceID]tokenWithAuthorizedEntity:authorizedEntityscope:scopeoptions:optionshandler:^(NSString*_Nullabletoken,NSError*_Nullableerror){// ...}];
إدارة الرموز المميزة وأرقام تعريف المثيل
يتيح لك معرّف المثيل حذف الرموز المميزة وإعادة تحميلها.
حذف الرموز المميزة وأرقام تعريف المثيل
NSString*authorizedEntity=PROJECT_ID;// Project IDString*scope=kFIRInstanceIDScopeFirebaseMessaging;FIRInstanceIDDeleteTokenHandlerhandler=^void(NSError*error){if(error){// Failed to delete the token. Check error and do an exponential// backoff to retry again.}else{// Successfully deleted the token.}};[[FIRInstanceIDinstanceID]deleteTokenWithAuthorizedEntity:authorizedEntityscope:scopehandler:handler];
يمكنك أيضًا حذف رقم تعريف المثيل نفسه، وفي هذه الحالة، ستحصل على رقم تعريف مثيل جديد في المرة التالية التي تتصل فيها بـ
getInstance():
قد تنشئ خدمة Instance ID رموزًا مميّزة أو تعيد إنشائها. وعند حدوث ذلك، سيتم إرسال إشعار. يمكنك الاستماع إلى هذا الإشعار من خلال إضافة مراقب للإشعارات باسم kFIRInstanceIDTokenRefreshNotification.
يجب إنشاء هذا المراقب قبل إنشاء الرمز المميّز، مثلاً
قبل طلب [FIRApp configure]. يمكن استرداد الرمز المميز الأخير من خلال استدعاء [[FIRInstanceID instanceID] token].
يُرجى العِلم أنّه لمراقبة عملية إنشاء الرموز المميزة لخدمة "المراسلة عبر السحابة الإلكترونية"، يتوفّر مفوّض محدّد.
تاريخ التعديل الأخير: 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 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003eThis documentation provides steps to implement Instance ID in an iOS client using Firebase Cloud Messaging for token management.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers need to set up CocoaPods dependencies by adding \u003ccode\u003epod 'FirebaseInstanceId'\u003c/code\u003e to their Podfile and running \u003ccode\u003epod install\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eGenerating an Instance ID token requires a Project ID and involves specifying the authorized entity, scope, and options like APNS token.\u003c/p\u003e\n"],["\u003cp\u003eInstance ID enables managing tokens by allowing deletion of tokens and Instance IDs and providing a mechanism to refresh them when needed.\u003c/p\u003e\n"],["\u003cp\u003eTo listen for token refresh notifications, add an observer for \u003ccode\u003ekFIRInstanceIDTokenRefreshNotification\u003c/code\u003e before the token is created.\u003c/p\u003e\n"]]],[],null,["# iOS Implementation\n\nThe following examples will help you implement Instance ID in an iOS client.\nNote that these examples use the GCM scope, which you would use to\nmanage tokens for an iOS client for\n[Firebase Cloud Messaging](//firebase.google.com/docs/cloud-messaging/ios/client).\n\nSet up your CocoaPods dependencies\n----------------------------------\n\nInstance ID uses [CocoaPods](https://cocoapods.org/) to install and\nmanage dependencies. Open a terminal window and navigate to the location of the\nXcode project for your application. If you have not already created a Podfile\nfor your application, create one now: \n\n```\npod init\n```\n\nOpen the Podfile created for your application and add the following: \n\n```\npod 'FirebaseInstanceId'\n```\n\nSave the file and run: \n\n```\npod install\n```\n\nThis creates an `.xcworkspace` file for your application. Use this file for all\nfuture development on your application.\n\nGenerate a token\n----------------\n\nGenerating tokens requires a Project ID generated by the [Google Developers Console](https://console.developers.google.com/project). \n\n NSString *authorizedEntity = PROJECT_ID;\n String *scope = kFIRInstanceIDScopeFirebaseMessaging;\n NSDictionary *options = @{\n @\"apns_token\" : \u003cAPNS Token data\u003e,\n // 1 if APNS sandbox token else 0\n @\"apns_sandbox\" : @(1),\n };\n [[FIRInstanceID instanceID] tokenWithAuthorizedEntity:authorizedEntity\n scope:scope\n options:options\n handler:\n ^(NSString * _Nullable token, NSError * _Nullable error) {\n // ...\n }];\n\nManage tokens and Instance IDs\n------------------------------\n\nInstance ID lets you delete and refresh tokens.\n\n### Delete tokens and Instance IDs\n\n NSString *authorizedEntity = PROJECT_ID; // Project ID\n String *scope = kFIRInstanceIDScopeFirebaseMessaging;\n FIRInstanceIDDeleteTokenHandler handler = ^void(NSError *error) {\n if (error) {\n // Failed to delete the token. Check error and do an exponential\n // backoff to retry again.\n } else {\n // Successfully deleted the token.\n }\n };\n [[FIRInstanceID instanceID]\n deleteTokenWithAuthorizedEntity:authorizedEntity\n scope:scope\n handler:handler];\n\nYou can also delete the Instance ID itself, in which case next time you call\n`getInstance()` you will get a new Instance ID: \n\n [FIRInstanceID instanceID] deleteIDWithHandler:^(NSError *error) {\n if error != nil {\n NSLog(@\"Error deleting instance ID: %@\", error);\n }\n }];\n\n### Refresh tokens\n\nThe Instance ID service may create or regenerate tokens. When this occurs, a\nnotification will be sent. You can listen to this notification by adding an\nobserver for notifications named `kFIRInstanceIDTokenRefreshNotification`. \n\n [[NSNotificationCenter defaultCenter] addObserver:self\n selector:@selector(tokenRefreshNotification:) \n name:kFIRInstanceIDTokenRefreshNotification object:nil];\n\nThis observer must be created before the token is created, for example\nprior to calling `[FIRApp configure]`. The latest token can be retrieved by\ncalling `[[FIRInstanceID instanceID] token]`.\n\nNote that for observing generation of tokens for Cloud Messaging, there is a\nspecific [delegate](https://firebase.google.com/docs/cloud-messaging/ios/client#monitor-token-generation) available."]]