2011-12-07 4 views
7

문서 폴더에있는 plist 파일에 데이터를 유지하는 relativley 간단한 응용 프로그램이 있습니다. 시작할 때 데이터가 UITableView로로드됩니다. 그런 다음 사용자는 레코드를 편집, 삭제 또는 추가 할 수 있으며 변경 사항은 plist 파일에 다시 저장됩니다.iCloud를 사용하여 plist 파일 공유

이제이 데이터 (plist 파일)를 iCloud를 사용하여 여러 기기에서 공유하고 싶습니다. 설명서를 살펴본 결과, plist 파일을 "관리"하는 데 UIDocument를 만들어야한다는 것을 이해했습니다.

몇 가지 iCloud 튜토리얼을 살펴 보았지만 모두 plist와 같은 전체 파일이 아니라 UIDocument 클래스의 속성 내에 간단한 문자열을 저장합니다.

UIDocument 개체를 사용하여 plist 파일 (또는 그와 관련하여 다른 파일)을 iCloud에 어떻게 공유합니까?

plist 파일 내용을 NSData로 변환 한 다음 UIDocument의 속성에 저장합니까? 대신 NsFileWrapper를 사용해야합니까?

UIDocument/iCloud 배열에서 머리를 감싸는 데 어려움을 겪고있는 것처럼 보입니다. 나는 아마 이것을 더 복잡하게 만든다. 정말로 그렇다.

+0

답변을 찾으셨습니까? 나는 똑같은 일을 할려고하고 있는데 내 앱은 너와 아주 비슷하게 들린다. 좋은 가이드를 찾았 으면 알려주세요. – Jackson

+0

나는 또한 이것을 성취하려고 노력하고있다.NSString을 사용하여 자습서를 수정했지만 두 번째 장치에서 데이터를 볼 수 없습니다. – zambono

+0

. +1 to poster – autodidakto

답변

1

UIDocument와 함께 plist를 사용하려면 UIDocument를 서브 클래스 화하고 NSMutableDictionary로 선언 된 self.myDictionary (귀하의 plist)로 다음 두 메소드를 대체 할 수 있습니다.

- (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 또는 binary 출력을 선택할 수 있습니다. – malhal

+0

또한 보관 용 plists는 Mac과 iOS간에 호환되지 않습니다. – malhal

7

아직 해결 방법이 필요한 사람이 있는지는 확실하지 않지만 제대로 작동하려면 좋은 방법을 찾았습니다.

UIDocument는 Data를 NSData 또는 NSFilewrapper로만 받아들이므로 NSData에서 NSDictionary를 반환하는 NSDictionary 클래스에 대한 Category를 처음 만들었습니다.

있는 NSDictionary + DictFromData.h :

#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)

당신이 만약 지금 수입 여기에 범주에 대한 두 파일을입니다 UIDocument Subclass의이 카테고리를 사용하면 plist 파일을 iCloud 컨테이너에 쉽게로드하고 저장할 수 있습니다.

가 iCloud에 당신의 UIDocument 서브 클래스에이 추가에서 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; 
} 

이 추가 다시 아이 클라우드에 데이터를 저장하는 경우 :

- (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 초가 걸립니다.