IOS network programming-cloud key-value data storage programming instance

Source: Internet
Author: User

Cloud key-value data storage design

A programming instance of the iCloud key-value data storage. There are two controls on and off the screen. The picture on the left is device 1. Click the "set iCloud data" button to save the control status to the iCloud server. The picture on the right shows device 2. After a few seconds, device 2 receives a change notification.

 

 

Configure the xcode Project

Create an iOS project using xcode. After the project is created, select tagets → mynotes → summary → entitlements. Here we can configure the authorization information.

Then we also need to apply settingsCodeSignature ID. Select the configuration profile for the code signature ID. Select tagets → mynotes → code signing identity

After setting, you can start encoding.

Code Implementation

First, you need to register nsubiquitouskeyvaluestoredidchangeexternallynotification and synchronize data. For the code, refer to viewdidload method of viewcontroller. M:

-( Void  ) Viewdidload {[Super viewdidload]; nsubiquitouskeyvaluestore * Store =[Nsubiquitouskeyvaluestore ultultstore]; ① [[nsicationicationcenter defacenter center] ② addobserverforname: nsubiquitouskeyvaluestoredidchangeexternallynotification  Object  : Storequeue: nilusingblock: ^ (Nsnotification * Note) {③  //  Update control status  [_ Switchsound Seton: [store boolforkey: ubiquitoussoundkey]; ④ [_ switchmusic Seton: [store boolforkey: ubiquitousmusickey]; ⑤ uialertview * Alert = [[Uialertview alloc] initwithtitle: @ "iCloud change notification" message: @ "your iCloud storage data has changed"  Delegate : Nilcancelbuttontitle: @ "OK" otherbuttontitles: nil, nil]; [alert show] ;}]; [store synchronize]; ⑥  //  Initialize control status  [_ Switchsound Seton: [store boolforkey: ubiquitoussoundkey]; 7 [_ switchmusic Seton: [store boolforkey: ubiquitousmusickey]; Response} 

 

Save the data to the iCloud storage. The setdata code of viewcontroller. M: method:

-(Ibaction) setdata :(ID) Sender {nsubiquitouskeyvaluestore* Store =[Nsubiquitouskeyvaluestore defaultstore];//Store to iCloud[Store setbool: _ switchsound. ISON forkey: ubiquitoussoundkey]; [store setbool: _ switchmusic. ISON forkey: ubiquitousmusickey]; [store synchronize];

 

}

Because it is a bool value, the storage method is setbool: forkey :. Finally, do not forget to cancel the registration notification. In View Controller, release notification can be completed in the didreceivememorywarning method:

 
-(Void) Didreceivememorywarning {[Super didreceivememorywarning]; [nsicationicationcenter defaultcenter] removeobserver: Self];}

 

 

Because there is only one notification in this application, you can use the [nsnotificationcenter defacenter center] removeobserver: Self] statement to release all notifications without affecting other notifications, if there are other notifications, we should use this statement with caution.

We can test the code after programming. The test of this application is very troublesome and requires two real devices instead of simulators. Run the two devices and click "set iCloud data" for one of the devices. After a few seconds, the other device will receive a change notification.

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

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.