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){// ...}];
管理權杖和執行個體 ID
執行個體 ID 可讓您刪除及更新權杖。
刪除權杖和執行個體 ID
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];
您也可以刪除執行個體 ID 本身,這樣下次呼叫 getInstance() 時,就會取得新的執行個體 ID:
[[["容易理解","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."]]