I have NSData in my App and i want to save that data in iCloud. I DONT want to synchronize my NSUserDefaults with iCloud, so its no clone of "Can I use iCloud to sync the NSUserDefaults plist file". Is that possible? How can i do that? How can I retrieve that saved data?
Yes it is possible. I have done that before and my answer is at syncing zip with iCloud,in which I am creating zip and converting it into NSData
and syncing with iCloud, later I am receiving NSData and again converting back it into zip and unzipping content. Here your main need is syncing of NSData so All you to have work around for NSData
.
1) Create subclass of 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) Syncing with iCloud (You can do as per you need)
-(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");
}];
}
Hope this helps..