Saturday, November 12, 2011

iCloud implementation

Step by step method to implement icloud
Make sure your device is running iOS 5.
Turn on iCloud.
When you turn on a new iOS device or after you’ve completed the update to iOS 5, follow the onscreen instructions to activate your device and set up iCloud.
If you skipped the setup process or want to change your iCloud settings, tap the Settings icon on the Home screen and select iCloud.
Customize your settings.
Tap the Settings icon and select iCloud. Tap the On/Off switches to enable individual iCloud services, including Photo Stream, Documents, Find My iPhone, and more.
To enable Backup, tap Storage & Backup, then switch on iCloud Backup.
Now go to the Provisioning Portal to enable iCloud storage for your iOSapplication. Enabling this feature requires that you have an updated provisioning profile on your development systems. Xcode 4 handles this step for you automatically.
Add a new Entitlements file to your application and use it to configure the iCloud features for your application uses. Set the keys and values like this:

           •Now we have to do Entitlements  settings. Go to your target and select Summary tab and do the settings so           that it will look like this:

           •Now we have to do some setup for bundle settings. For this add a new setting bundle file under
           •Resource option in your project , it will have extension .bundle. Now this file contain a Root.plist file.
           •In Root.plist you will get 2 key, one is Preference Items with 4 items and another is Strings Filename. Delete all the items under Preference Items and setup it like this:

Now come on code, Go to your appDelegate.m file and write these line in didFinishLaunchingWithOptions method :
   [[NSUserDefaults standardUserDefaults] setValue:@"YES"   forKey:@"enableiCloud"];
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSURL *iCloudURL = [fileManager                   URLForUbiquityContainerIdentifier:@""];
  BOOL check = [fileManager isUbiquitousItemAtURL:iCloudURL];
  NSUbiquitousKeyValueStore *cloudStore = [NSUbiquitousKeyValueStore   defaultStore];
   [cloudStore setString:[iCloudURL absoluteString] forKey:@"iCloudURL"];
   [cloudStore synchronize]; // Important as it stores the values you set before on iCloud

// Get the path og file/document to be send on iCloud from document directory.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"saved.png"];
BOOL existFile = [fileManager fileExistsAtPath:writableDBPath];
// If the file exist then send it on iCloud.
 if (existFile { 
   NSError *errorOut=nil;
     BOOL success =[fileManager setUbiquitous:YES itemAtURL:[NSURL   URLWithString:writableDBPath]   destinationURL:iCloudURL error: &errorOut];
      NSLog(@"Error: %@", errorOut);
// Fire Query to check whether the perticular file is present on iCloud or not.
query = [[NSMetadataQuery alloc] init];
 [query setSearchScopes:[NSArray   arrayWithObjects:NSMetadataQueryUbiquitousDataScope,   NSMetadataQueryUbiquitousDocumentsScope, nil]];
 [query setPredicate:[NSPredicate predicateWithFormat:@"kMDItemFSName LIKE '*.png'"]];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryHandler:) name:NSMetadataQueryDidFinishGatheringNotification object:query]; 
 [query startQuery];

- (void)queryHandler: (NSNotification *) inNotification{
   NSLog(@"The number of results: %i", (int)[query resultCount])