- この記事は、Instance IDに関する記事を和訳したものです。
- 原文: iOS Implementation
- 元記事のライセンスは CC-BYで、この和訳記事のライセンスは CC-BYです。
- 自己責任でご利用ください。
- 和訳した時期は 2017年8月ころです。
次の例は、iOSクライアントにて Instance IDを実装するのに役立つでしょう。 これらの例では、GCMスコープを使用していることに注意してください、それは、Google Cloud Messaging用の iOSクライアントにてトークンを管理するために使用します。
あなたの CocoaPods依存関係をセットアップする
Instance IDは、インストールおよび依存関係を管理するために CocoaPodsを使用します。 ターミナルウィンドウを開き、あなたのアプリケーション用の Xcodeプロジェクトの位置に移動します。 もしまだあなたのアプリケーション用の Podfileを作成していなければ、それを今すぐ作成します:
pod init
あなたのアプリケーションのために作成された Podfileを開き、以下を追加します:
pod 'Google/GGLInstanceID'
ファイルを保存し、次を実行します:
pod install
これは、あなたのアプリケーションのための .xcworkspace
ファイルを作成します。
あなたのアプリケーション上の今後のすべての開発のために、このファイルを使用してください。
Instance IDを取得する
次のコードは Instance IDを返します:
__block NSString *iid;
GGLInstanceIDHandler handler = ^void(NSString *identity, NSError *error) {
if (error) {
// failed to get the identity for the app
// handle error
} else {
iid = identity;
// handle InstanceID for the app
}
};
[[GGLInstanceID sharedInstance] getIDWithHandler:handler];
トークンを生成する
トークンを生成するには、Google Developers Consoleによって生成された Project IDを必要とします。
// Project ID from Google Developer Console
NSString *authorizedEntity = PROJECT_ID;
String *scope = kGGLInstanceIDScopeGCM; // Scope “GCM” in GGLInstanceID.h
NSDictionary *options = @{
kGGLInstanceIDRegisterAPNSOption : <APNS Token data>,
// 1 if APNS sandbox token else 0
kGGLInstanceIDAPNSServerTypeSandboxOption : @(1),
};
GGLInstanceIDTokenHandler handler = ^void(NSString *token, NSError *error) {
if (error) {
// check the error and try again after an exponential backoff
} else {
// got the token successfully
}
};
// Start GGLInstanceID (do it once only)
GGLInstanceID *config = [GGLInstanceIDConfig defaultConfig];
[[GGLInstanceID sharedInstance] startWithConfig:config];
GGLInstanceID *iidInstance = [GGLInstanceID sharedInstance];
NSString *token = [iidInstance tokenWithAuthorizedEntity:authorizedEntity
scope:scope
options:options
handler:handler];
トークンおよび Instance IDを管理する
Instance IDは、あなたにトークンを削除およびリフレッシュさせます。
トークンおよび Instance IDを削除する
NSString *authorizedEntity = PROJECT_ID; // Project ID from Google Developer Console
String *scope = kGGLInstanceIDScopeGCM; // Scope “GCM” in GMPInstanceID.h
GGLInstanceIDDeleteTokenHandler handler = ^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.
}
};
[[GGLInstanceID sharedInstance]
deleteTokenWithAuthorizedEntity:authorizedEntity
scope:scope
handler:handler];
また、Instance ID自身を削除することもできます、この場合、次に getInstance()
を呼んだとき、新しい Instance IDを取得するでしょう:
GGLInstanceID *iid = [GGLInstanceID sharedInstance];
GGLInstanceIDDeleteHandler deleteHandler = ^void(NSError *error) {
if (error) {
// failed to delete the identity for the app
// do an exponential backoff and retry again.
} else {
// try to get a new ID
dispatch_async(dispatch_get_main_queue(), ^{
GGLInstanceIDHandler handler =
^void(NSString *identity, NSError *error) {
if (error) {
// failed to get the identity for the app
// handle error
} else {
NSString *instanceID = identity;
// handle InstanceID for the app
}
}
[iid getIDWithHandler:handler];
});
}
}
[iid deleteIDWithHandler:deleteHandler];
トークンをリフレッシュする
Instance IDサービスは、定期的にコールバックを開始します(例えば、6ヶ月ごとに)、それは、あなたのアプリがそのトークンをリフレッシュするすることをリクエストします。 また、次の場合に、コールバックを開始するかもしれません:
- セキュリティの問題がある; 例えば、SSLあるいはプラットフォームの問題。
- 端末情報がもはや有効ではない; 例えば、バックアップおよびリストア。
- Instance IDサービスが、それ以外に影響を受けた。
あなたの iOSアプリにてこれらの Instance IDサービスのコールバックをリッスンするには、InstanceID
デリゲートプロトコルを実装します:
@interface MyAppInstanceIDDelegate : NSObject <GGLInstanceIDDelegate>
@end
@implementation MyAppInstanceIDDelegate
-(void)onTokenRefresh {
// assuming you have defined TokenList as
// some generalized store for your tokens
NSArray *tokenList = [MyAppTokensList allTokens];
GGLInstanceID iid = [GGLInstanceID sharedInstance];
for(MyAppTokenItem *tokenItem in tokenList) {
tokenItem.token =
[iid tokenWithAuthorizedEntity:tokenItem.authEntity
scope:tokenItem.scope
options:tokenItem.options
handler:tokenItem.handler];
// send this tokenItem.token to your server
}
}
@end