icloud-intro

Have you been wondering how to actually integrate the iCloud storage in your Application ? Here is quick, short yet effective tutorial for the new bee.

Why Do we need iCloud Storage in my app :

  • Do you need to restore the data of the user even if user uninstalls your app ?
  • Do you want the user to logged into the application through different devices yet the data should be sync without web service ?
  • Do you want to store small bunch of data without creating web services ?

If the answers to above question is affirmative than feel free to integrate iCloud and follow this sort yet effective tutorial

Pre-requisite (A big hurdle) :

To integrate iCloud into your application you will need

  1. Apple Developer Account (Paid Account :P)
  2. Developer Certificate
  3. Bundle ID with iCloud Enabled
  4. Provisioning Profile with Above Certificate & Bundle ID
  5. Actual device to Test the APP

Notes : I would not prefer to use Wild Card Bundle ID & provisioning Profile to integrate iCloud/

Steps :

  1. Creating the Bundle Identifier :

    Screen Shot 2015-07-28 at 2.43.13 pm

    Screen Shot 2015-07-28 at 2.43.23 pm

    You will need to enable the iCloud in Your bundle identifier that will be needed.

  2. Setting up the basic application

    So you have set up the bundle ID, you are having the certificates and you have the provisioning profile with above certificates and bundle id. That means we can get our hand dirty into iCloud Application coding. Lets start with the basic setup. Follow the below screenshot to setup the basic app.

    1) Select the target-> general and check if cloudKit.framework is included as per below screenshot :

    Screen Shot 2015-07-28 at 4.55.32 pm

    2) Select target-> Category and enable iCloud -> KeyValue Storage And check if All 4 steps are tick if not like below screenshot click on Fix issue. (You will need team to be added in order to fix this issues)

    Screen Shot 2015-07-28 at 4.55.49 pm

    3) Check if Entitlement File is created and setup which will look like below :

    Screen Shot 2015-07-28 at 4.56.39 pm

  3. Writing the actual Code

    I hope you have followed me till now. Now lets dive deep into coding part. And its not complicated :P. I hope you are aware of NSUserDefaults. This syntax is similar to that and you will enjoy coding for iCloud.

    Lets look at the functions below :

    @property (strong, nonatomic) NSMutableArray *notes;
    
    - (NSArray *)notes
    {
        if (_notes) {
            return _notes;
        } 
        _notes = [[[NSUbiquitousKeyValueStore defaultStore] arrayForKey:@"AVAILABLE_NOTES"] mutableCopy];
        if (!_notes) _notes = [NSMutableArray array];
        return _notes;
    }
    
    
    -(IBAction)writeToiCloud:(id)sender
    
    {
        _notes = [[[NSUbiquitousKeyValueStore defaultStore] arrayForKey:@"AVAILABLE_NOTES"] mutableCopy];
        if(!_notes)
        {
            _notes=[[NSMutableArray alloc] init];
        }
        
        [_notes addObject:@"test1"];
        [[NSUbiquitousKeyValueStore defaultStore] setArray:self.notes forKey:@"AVAILABLE_NOTES"];
    }
    -(IBAction)readFromiCloud:(id)sender
    
    {
        _notes=[[NSMutableArray alloc] init];
        [[NSUbiquitousKeyValueStore defaultStore] synchronize];
       _notes = [[[NSUbiquitousKeyValueStore defaultStore] arrayForKey:@"AVAILABLE_NOTES"] mutableCopy];
        NSLog(@"%@",[self.notes description]);
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Array data" message:[NSString stringWithFormat:@"%@",_notes] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
    }

    Here NSUbiquitousKeyValueStore actually is used to perform iCloud Read/Write Operations. Do not forget to bind this to touchUpInside event of Button.

  4. Testing Your App.

    Install into your device click on write button, read button Close App. UnInstall the app. Re-install the app and than perform Read operation if you are able to read you are iCloud Master 😛

iCloud Tutorial

Aadil Keshwani

People call me geek i call myself Coder. I like to explore & learn new things, R&D and Innovation motivates me. By profession i am iPhone Developer but i have sound knowledge of PhoneGap, HTML, CSS3, WordPress, Photoshop, PHP, CodeIgniter, OpenCart. Feel free to contact me for any queries or help in above technologies, i would be more than happy to help you out.

Category: Uncategorized
777 views