2011-12-07 10 views
7

私は、relativleyのシンプルなアプリケーションを使用して、データをドキュメントフォルダ内のplistファイルに保存します。起動時にデータがUITableViewに読み込まれます。ユーザーは、レコードを編集、削除、または追加することができ、変更はplistファイルに保存されます。iCloudを使用してplistファイルを共有する

ここでは、このデータ(plistファイル)をiCloudを使用してデバイス間で共有したいと考えています。私はドキュメンテーションを見てきましたが、私はplistファイルを "管理"するためにUIDocumentを作成する必要があることを理解しています。

私はいくつかのiCloudチュートリアルを見てきましたが、それらはすべてファイル全体(plistのようなもの)ではなく、UIDocumentクラスのプロパティ内に単純な文字列を格納しています。

UIDocumentオブジェクトを使用して、plistファイル(またはその他のファイル)をiCloudにどのように共有できますか?

plistファイルの内容をNSDataに変換して、UIDocumentのプロパティに保存しますか?代わりにNsFileWrapperを使用する必要がありますか?

私はUIDocument/iCloudアレンジの周りに頭を巻きつけるのが難しいようです。私はおそらくこれをもっと複雑にしています。

+0

この回答は見つかりましたか?私は同じことをやっていると私のアプリはあなたのものに非常によく似ています。あなたが良いチュートリアルを見つけたら教えてください。 – Jackson

+0

私もこれを達成しようとしています。NSStringを使ってチュートリアルを修正していますが、データを見るために2番目のデバイスを取得できません。 – zambono

+0

。 +1 toポスター – autodidakto

答えて

1

plistをUIDocumentで使用するには、UIDocumentをサブクラス化し、NSMutableDictionaryとして宣言されたself.myDictionary(plist)で次の2つのメソッドをオーバーライドできます。

- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError **)outError 
{  
    if ([contents length] > 0) 
    { 
     NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:(NSData *)contents]; 
     NSMutableDictionary *dataDictionary = [unarchiver decodeObjectForKey:@"data"]; 

     self.myDictionary = dataDictionary; 
     [unarchiver finishDecoding]; 
     [unarchiver release]; 
    } 
    else 
    { 
     self.myDictionary = [NSMutableDictionary dictionary]; 
    } 

    return YES;  
} 

- (id)contentsForType:(NSString *)typeName error:(NSError **)outError 
{ 
    NSMutableData *data = [[[NSMutableData alloc] init] autorelease]; 
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; 
    if(!self.myDictionary) 
    { 
     self.myDictionary = [NSMutableDictionary dictionary]; 
    } 
    [archiver encodeObject:self.myDictionary forKey:@"data"]; 

    [archiver finishEncoding]; 
    [archiver release]; 
    return data; 
} 
+0

これは、アーカイブplistを作成するが十分に近い辞書plistを作成しません。実際のplistを作成するには、NSPropertyListSerializationを使用し、xml出力またはバイナリ出力を選択できます。 – malhal

+0

また、アーカイブplistsはMacとiOSでは互換性がありません。 – malhal

7

解決策がまだ必要な人がいるかどうかはわかりませんが、これをうまく利用するには良い方法があります。

UIDocumentはDataをNSDataまたはNSFilewrapperとしてのみ受け入れるので、NSDataからNSDictionaryを返すNSDictionaryクラスのカテゴリを最初に作成しました。あなたは今インポートした場合

#import <Foundation/Foundation.h> 

@interface NSDictionary (DictFromData) 
+ (id)dictionaryWithData:(NSData *)data; 
- (id)initWithData:(NSData *)data; 
@end 

とNSDictionaryの+ DictFromData.m

#import "NSDictionary+DictFromData.h" 
@implementation NSDictionary (DictFromData) 

+ (id)dictionaryWithData:(NSData *)data { 
    return [[[NSDictionary alloc] initWithData:data] autorelease]; 
} 

- (id)initWithData:(NSData *)data { 
    NSString *tmp = nil; 

    self = (NSDictionary *)[NSPropertyListSerialization 
          propertyListFromData:data 
          mutabilityOption:NSPropertyListImmutable 
          format:NULL 
          errorDescription:&tmp]; 

    NSAssert1(tmp == nil,@"Error in plist: %@",tmp); 
    return [self retain]; 
} 
@end 

source

NSDictionaryの+ DictFromData.h:ここではカテゴリーの2つのファイルがありますUIDocumentサブクラスのこのカテゴリを使用すると、plistファイルを簡単にロードしてiCloudコンテナに保存できます。あなたUIDocumentのサブクラスにこれを追加のiCloudからあなたのplistをロードするために

(プロパティの内容はNSDictionaryのです):

- (BOOL)loadFromContents:(id)contents 
        ofType:(NSString *) 
     typeName error:(NSError **)outError { 

    if ([contents length] > 0){ 
     self.contents = [NSDictionary dictionaryWithData:contents]; 
    } else { 
     self.contents = nil; 
    } 

    // call some Methods to handle the incoming NSDictionary 
    // maybe overwrite the old Plist file with the new NSDictionary 

    return YES; 
} 

バックのiCloudにあなたのデータを保存するため、これを追加します。

- (id)contentsForType:(NSString *)typeName error:(NSError **)outError { 
    NSData * plistData = [[[NSData alloc]initWithContentsOfFile:YOUR_PLIST_FILE]autorelease]; 
    return plistData; 
} 

を今すぐ電話を受ける場合:

[myUIDocument updateChangeCount:UIDocumentChangeDone]; 

YOUR_PLIST_FILEが同期しています。 iCloudコンテナのアップデートには約10-15秒かかります。

関連する問題