アプリにNSDataがあり、そのデータをiCloudに保存したいと思います。NSUserDefaultsをiCloudと同期したくないので、「iCloudを使用してNSUserDefaultsplistファイルを同期できますか」のクローンはありません。それは可能ですか?どうやってやるの?保存したデータを取得するにはどうすればよいですか?
5353 次
4 に答える
3
はい、可能です。私は以前にそれを行いました、そして私の答えはzipをiCloudと同期することです、そこで私はzipを作成してそれをiCloudに変換してNSData
同期します、後で私はNSDataを受け取りそして再びそれをzipに変換してコンテンツを解凍します。ここでの主なニーズはNSDataの同期であるため、回避する必要があるのはすべてですNSData
。
1)のサブクラスを作成しますUIDocument
#import <UIKit/UIKit.h>
@interface MyDocument : UIDocument
@property (strong) NSData *dataContent;
@end
2)MyDocument.m
#import "MyDocument.h"
@implementation MyDocument
@synthesize dataContent;
// Called whenever the application reads data from the file system
- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError **)outError
{
self.dataContent = [[NSData alloc] initWithBytes:[contents bytes] length:[contents length]];
[[NSNotificationCenter defaultCenter] postNotificationName:@"noteModified" object:self];
return YES;
}
// Called whenever the application (auto)saves the content of a note
- (id)contentsForType:(NSString *)typeName error:(NSError **)outError
{
return self.dataContent;
}
@end
3)iCloudとの同期(必要に応じて行うことができます)
-(IBAction) iCloudSyncing:(id)sender
{
NSURL* ubiq = [[NSFileManager defaultManager]URLForUbiquityContainerIdentifier:nil];
NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:@"iCloudPictures.zip"];
MyDocument *mydoc = [[MyDocument alloc] initWithFileURL:ubiquitousPackage];
NSData *data = << YOUR NSDATA >>;
mydoc.dataContent = data;
[mydoc saveToURL:[mydoc fileURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success)
{
if (success)
{
NSLog(@"Synced with icloud");
}
else
NSLog(@"Syncing FAILED with icloud");
}];
}
お役に立てれば..
于 2013-03-04T10:01:13.433 に答える
0
ここでは、データをicloudに保存するソリューションがあります。または、そのデータをファイルに書き込み、ファイルパスを指定してそのファイルをicloudに直接保存できます
于 2013-03-04T10:05:49.260 に答える
0
カスタム オブジェクト用注: iCloud ユーザーごとに 1 MB に制限されています!
+(void)write{
//Decode using
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[HistoryFile files]];
//Save Data To NSUserDefault
NSUbiquitousKeyValueStore *iCloud = [NSUbiquitousKeyValueStore defaultStore];
//let ios know we want to save the data
[iCloud setObject:data forKey:@"app_data"];
//iOS will save the data when it is ready.
[iCloud synchronize];
}
+(NSMutableArray*)read{
//Read Settings Value From NSUserDefault
//get the NSUserDefaults object
NSUbiquitousKeyValueStore *iCloud = [NSUbiquitousKeyValueStore defaultStore];
//read value back from the settings
NSData *data = [iCloud objectForKey:@"app_data"];
NSMutableArray *data_array = (NSMutableArray*)[NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"data_array %@",data_array);
return data_array;
}
于 2017-08-08T07:11:02.383 に答える