How to Use Icloud to Sync the Nsuserdefaults Plist File

Can I use iCloud to sync the NSUserDefaults plist file

There is a library available to do this with one line of code

https://github.com/MugunthKumar/MKiCloudSync

How can i save NSData in iCloud

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..

Save user defaults to iCloud

You misunderstand what that is saying.

When a user backup's up their devices using iTunes or iCloud, only certain parts of the app's sandbox is backed up. So there are two important aspects here:

  1. Only backup data that can't be replaced.
  2. Be sure to backup irreplaceable data.

NSUserDefaults is one of the things that will be backed up for you. You don't need to do anything special.

You don't need iCloud support unless your app has a specific need to use it.

Backing up NSUserDefaults and synching iPhone

Yes. NSUserDefaults uses a PLIST file as a backing store, which is backed up on each sync. See http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/UserDefaults/Concepts/DefaultsDomains.html for more information.

If you wanted to see for yourself, you could check out ~/Library/Application Support/MobileSync/Backup/. Create an unencrypted backup of a device with just your app on it, and view the files in the PLIST editor.



Related Topics



Leave a reply



Submit