LoginSignup
13
13

More than 5 years have passed since last update.

NSUbiquitousKeyValueStore でiCloud同期ができない場合の対処

Posted at

はじめに

ドハマリしたので備忘録的に残しておきます。

NSUbiquitousKeyValueStore は、iOSが提供してくれるクラウド上のKVSストレージです。
詳しい説明はこちら。

NSUbiquitousKeyValueStore を利用するための通常の流れ

  1. Apple DeveloperサイトでApp IDを確認する(はじめてなら作成)
  2. 対象のApp IDののiCloud利用を有効にする
  3. プロビジョニングプロファイルを作成する(すでに作っていたら再作成)
  4. Xcodeプロジェクトでtargetを指定し、Capabilitiesの設定でiCoudを有効にする
  5. 作成されたentitlementsファイルのiCloud Key-Value Storeの値を \$(TeamIdentifierPrefix)\$(CFBundleIdentifier) に設定する
  6. アプリをインストールしたデバイスのiCloud Drive設定をオンにする

基本的には、これで利用できるようになります。

発生した問題

NSUbiquitousKeyValueStore* ukvs = [NSUbiquitousKeyValueStore defaultStore];
[ukvs setString: @"value" forKey: @"key"];

NSString* str = [ukvs stringForKey: @"key"];

という実装で、値が取れるデバイスと取れないデバイスがいた。

原因

同じApp ID (Bundle Identifier) のアプリをiCloud設定をする前にすでにインストールしている場合、後からiCloud設定を有効にしたアプリをインストールしても、そのデバイスからはiCloudが使えなくなるというもの(iOS設定画面のiCloud設定でオンになっていたとしても)。
↓のStackoverflowの回答見るまで全然分からなかった。

I had the same problem. Logging off and back on my iCloud account on the device solved it for me, too.

This problem seems to appear when you have installed the app with the same bundle identifier on the device before you enabled iCloud entitlements.
I had this problem on 2 testing devices. A third device on which i installed the app the first time with iCloud enabled, made no problems.

http://stackoverflow.com/questions/26428083/nsubiquitouskeyvaluestore-not-syncing-with-icloud

解決策は、こちらに書いてある通り、iCloudのアカウントを一度サインアウトしてサインインし直せばOK。

気になること

本番リリースしてるアプリで、途中からiCloud同期を有効にした場合とかこの現象が発生しそう。
ユーザに強いるわけにもいかないし、他の回避策がありそうだけどどなたかご存じですか?(;´Д`)

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13