icloud document storage programming is more complex relative to key value data stores, involving customizing document classes, obtaining icloud document catalogs, locating documents in ubiquity containers, saving documents, and resolving document conflicts.
Example: icloud document storage Programming design
Example describes the icloud document storage programming process, the screen has a text box and a button control, in the device 1 input, click the "Save Data" button to save the data icloud server. The right image is a device 2 screen, and after a few seconds the icloud server-side data is read on device 2 and displayed in a text box.
Configure Xcode Engineering
Writing icloud document storage application also requires some configuration in the Xcode project to select Tagets→mynotes→summary→entitlements
In the diagram ubiquity Contrainers adds Com.51work6.mynotes, which is the Ubiquity container identifier, which can have multiple ubiquity container identities that represent different directories. The Com.51work6.mynotes representative directory is as follows:
/var/mobile/library/mobile documents/98z3r5xu29~com~51work6~mynotes/
The 98z3r5xu29 was generated when the iOS developer configured the portal site Creation app ID, which is called the team ID.
If the application does not use the icloud key value data store Key-value store can not be configured.
Custom document Classes
We need to encapsulate a document class, which inherits the abstract class Uidocument, and Uidocument implements the Nsfilepresenter protocol. A class that implements the Nsfilepresenter protocol, which represents files and directories that can be viewed and edited, which are used in conjunction with the file Coordinator class nsfilecoordinator to coordinate the management of files or directories.
This class is named Myclouddocument, and the Myclouddocument code is as follows:
#import <UIKit/UIKit.h>
@interface Myclouddocument:uidocument①
@property (Strong, nonatomic) nsstring *contents;
@end
#import "MyCloudDocument.h"
@implementation myclouddocument
//Loading Data
-(BOOL) loadfromcontents :(ID) Contents OfType: (NSString *) TypeName Error: (nserror * *) Outerror②
{
if ([contents length] > 0)
{ C11/>self.contents = [[NSString alloc] initwithdata:contents encoding:nsutf8stringencoding]; ③
} return
YES;
Save Data
-(ID) Contentsfortype: (NSString *) TypeName Error: (nserror * *) Outerror④
{return
[ Self.contents datausingencoding:nsutf8stringencoding]; ⑤
}
@end