IOS network programming-cloud document storage programming instance

Source: Internet
Author: User

Programming of iCloud document storage is more complex than key-value data storage, involving custom document classes, obtaining the iCloud document directory, searching for documents in the ubiquity container, saving documents, and resolving document conflicts.

 

Example: Programming Design of iCloud document storage

This example describes how to save the data to the iCloud file storage program. There is a text box and a button control in the screen. Enter the content in device 1 and click "save data" to save the data to the iCloud server. The picture on the right shows device 2. After a few seconds, device 2 reads data from the iCloud server and displays the data in the text box.

Configure the xcode Project

You also need to configure the compilation of the iCloud document storage and editing application in the xcode project. Select tagets> mynotes> summary> entitlements.

In the figure, add com.51work6. mynotes to ubiquity contrainers. This is the ubiquity container ID, which can have multiple ubiquity container IDs, which represent different directories. Com.51work6. mynotes indicates the directory as follows:

/Var/mobile/library/mobile documents/98z3r5xu29 ~ Com ~ 51work6 ~ Mynotes/

98z3r5xu29 is generated when the IOS developer configures the portal website to create an app ID, which is called the team ID.

You do not need to configure the key-value store if the cloud key-value data is not used in the application.

Custom document class

We need to encapsulate a document class by ourselves, which inherits the abstract class uidocument, while the uidocument implements the nsfilepresenter protocol. Classes that implement the nsfilepresenter protocol. The files and directories represented by nsfilepresenter can be viewed and edited. These nsfilepresenter implementation classes are used in conjunction with the nsfilecoordinator class of the file Coordinator class to coordinate file or directory management.

This class is named myclouddocument. The code for myclouddocument is as follows:

# Import <uikit/uikit. h> @ interface myclouddocument: uidocument ① @ property (strong, nonatomic) nsstring * Contents; @ end # import "myclouddocument. h "@ implementation myclouddocument // load data-(bool) loadfromcontents :( ID) contents oftype :( nsstring *) typename error :( nserror **) outerror ② {If ([contents length]> 0) {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

 

Obtain the iCloud document directory

The iCloud document directory is the document directory under the ubiquity container. Therefore, you can get the root directory of the ubiquity container to obtain the iCloud document directory.

The ubiquitousdocumentsurl method in viewcontroller. m can be used in the iCloud document directory:

// Request the local ubiquity container to obtain the URL-(nsurl *) ubiquitousdocumentsurl {nsfilemanager * filemanager = [nsfilemanager defaultmanager] from the container; ① nsurl * containerurl = [container: @ "98z3r5xu29.com. 51work6. mynotes "]; ② containerurl = [containerurl urlbyappendingpathcomponent: @" events "]; ③ return containerurl ;}

 

 

Search for documents in the ubiquity container

After obtaining the iCloud document directory, we need to find the files in the container. To Query files in a container, you must register two broadcast notifications:

Nsmetadataquerydidfinishgatheringnotification: notification of query termination;

The nsmetadataquerydidupdatenotification query is complete and a notification is sent when the update starts;

The registration and release notification code in viewcontroller. m is as follows:

-(Void) viewdidload {[Super viewdidload]; // to query changes to the iCloud file, register the notification [[nsicationicationcenter defacenter center] addobserver: selfselector: @ selector (updateubiquitousdocuments :) name: jsonobject: Nil]; [[nsnotificationcenter defacenter center] addobserver: selfselector: @ selector (updateubiquitousdocuments :) name: nsmetadataquerydidupdatenotification object: Nil]; // query the changes to the iCloud file [self searchfilesonicloud];}-(void) didreceivememorywarning {[Super didreceivemorywarning]; [[nsicationicationcenter defacenter center] removeobserver: Self];}

 

Save document

Saving the document is very simple. It is done in saveclick: Method in viewcontroller. M. saveclick: triggered when you click the button:

- (IBAction)saveClick:(id)sender {_myCloudDocument.contents = _txtContent.text;[_myCloudDocument updateChangeCount:UIDocumentChangeDone];[_txtContent resignFirstResponder];}

 

After the storage is successful, you can view the abc.txt document in icloud. If you are using a Mac OS X system computer, you can press "system preference settings" to create a volume file.

If you want to view the information on iOS devices such as iPhone, iPod Touch, and iPad, you can start the setting application or go to bucket management to view the information on my iPod Touch.

From IOS network programming and cloud application best practices: Guan Dongsheng @ Tony _ Guan Dongsheng

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.