[ObjC] – iCloud class helper

A simple iCloud file managment system, to read / write / load files across devices.

What you need:

  • A valid Provisioning Profile ( with iCloud access )
  • An iCloud account
  • iOS 5.1+
  • ARC / Non ARC
  • iOS device
  • Security.framework

How to use?

Import #import “APCloudCore.h” in your project.

Next, set your delegate, and your file prefix.

File prefix, is very important if you want to separate files, because iCloud don’t permit to create and read folders.

Every classes provides a singleton access.

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”][[APCloudCore sharedCloud] setDelegate:self];
[[APCloudCore sharedCloud] setFilePrefix:@"ap"];[/code]

Main screen

Main screen

Methods:
Check for cloud enabled

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”][[APCloudCore sharedCloud] isCloudEnabled];[/code]

Save a file

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”][[APCloudCore sharedCloud] saveData:yourData withName:yourFileName];[/code]

Saved delegate

Saved delegate

Load a file

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”][[APCloudCore sharedCloud] loadDataWithName:yourFileName];[/code]

Delete a file

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”][[APCloudCore sharedCloud] deleteFileWithName:yourFileName];[/code]

Load all cloud files

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”][[APCloudCore sharedCloud] loadAllFilesWithPrefix:yourPrefix];[/code]

iCloud file list

iCloud file list

APCloudCoreDelegate

If you’re implemented the optionals delegate, you should implements the delegates (finish and fail):

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]- (void) apCloudCoreDidFinishUpdate;
– (void) apCloudCoreDidFailUpdate;- (void) apCloudCoreDidFinishLoadFiles:(NSMutableArray*)file;
– (void) apCloudCoreDidFailLoadFiles;

– (void) apCloudCoreDidFinishSaveData:(id)data;
– (void) apCloudCoreDidFailSaveData:(id)data;

– (void) apCloudCoreDidFinishLoadData:(id)data;
– (void) apCloudCoreDidFailLoadData:(id)data;

– (void) apCloudCoreDidFinishDeleteData:(id)data;
– (void) apCloudCoreDidFailDeleteData:(id)data;[/code]

Notes on SAVE

Save method, accept an id as file. This id is your custom class, that must implements NSCoding protocol.

Example of test store class:

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]@interface APTestStore : NSObject <nscoding>

@property (nonatomic, strong) NSString *aTitle;
@property (nonatomic, strong) NSString *aSubTitle;
@property (nonatomic, strong) NSString *aDescription;

@end

@implementation APTestStore
@end[/code]

This class is saved (encrypted) and loaded (decrypted) to/from cloud as NSData.

Cloud files

File are stored in your Cloud directory, that is accessibile from web, at url http://developer.icloud.com or in your Mac local folder, ~/Library/Mobile Documents/.

Files are stored using this tree:

[code lang=”bash” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]~/Library/Mobile Documents/XXXXXXX~it~albertopasca~dev-app/Documents/

ap_test-file-1.dat
ap_test-file-2.dat
ap_test-file-3.dat
xx-afile-1.dat
xx-afile-2.dat
[…][/code]

“ap” or “xx” are the prefixes of your files!

You can find full project on githubhttps://github.com/elpsk/iCloud-Helper
Feel free to edit/optimize/make money.

Rounded buttons (APRoundedButton) are explained in the last post: http://www.albertopasca.it/whiletrue/2014/04/objc-uibutton-rounded-corner/

Have fun.

ref: albertopasca.it

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.