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-10 UTC。"],[[["\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."]]