博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Beginning iCloud in iOS 5 Tutorial Part 2(转载)
阅读量:6584 次
发布时间:2019-06-24

本文共 4268 字,大约阅读时间需要 14 分钟。

原文地址:http://www.raywenderlich.com/6031/beginning-icloud-in-ios-5-tutorial-part-2

 

Setting Up the User Interface

The Xcode project template we chose already set up an empty view controller for us. We will extend it by adding the current document and a UITextView to display the content of our note.

Start by modifying ViewController.h to look like the following:

We have also marked the view controller as implementing UITextViewDelegate so that we can receive events from the text view.

Next, open up ViewController_iPhone.xib and make the following changes:

  • Drag a Text View into the View, and make it fill the entire area.
  • Control-click the File’s Owner, and drag a line from the noteView outlet to the Text View.
  • Control-click the Text View, and drag a line from the delegate to the File’s Owner.

At this point your screen should look like this:

When you are done, repeat these steps for ViewController_iPad.xib as well.

Next, open up ViewController.m and synchronize your new properties as follows:

@synthesize doc; @synthesize noteView;

Then modify viewDidLoad to register for the notification our code will send when our document changes (we’ll add the code to send this notification later):

Next, implement the method that gets called when the notification is received as follows:

This simply stores the current document and updates the text view according to the new content received.

In general substituting the old content with the new one is NOT a good practice. When we receive a notification of change from iCloud we should have a conflict resolution policy to enable the user to accept/refuse/merge the differences between the local version and the iCloud one. We’ll discuss more about conflict resolution later, but for now to keep things simple we’ll just overwrite each time.

Next, implement textViewDidChange to notify iCloud when the document changes, and modify the app to refresh the data in viewWillAppear as well:

This simply stores the current document and updates the text view according to the new content received.

In general substituting the old content with the new one is NOT a good practice. When we receive a notification of change from iCloud we should have a conflict resolution policy to enable the user to accept/refuse/merge the differences between the local version and the iCloud one. We’ll discuss more about conflict resolution later, but for now to keep things simple we’ll just overwrite each time.

Next, implement textViewDidChange to notify iCloud when the document changes, and modify the app to refresh the data in viewWillAppear as well:

As above this is not a great practice, because we are going to notify each iCloud about every single change (i.e. each time a character is added or deleted). For efficiency, it would be better to just tell iCloud every so often, or when the user has finished a batch of edits.

There’s just one last step remaining – we need to add the code to send the “noteModified” notification we registered for in viewDidLoad. The best place in this case is the Note class’s loadFromContents:ofType:error, method which is called whenever data are read from the cloud.

So open up Note.m and add this line of code to the bottom of loadFromContents:ofType:error (before the return YES):

Now we are really ready! The best way to test this application is the following: install it on two devices and run it on both. You should be able to edit on one and see the changes periodically propagated to the other.

The propagation of changes is not immediate and might depend on your connectivity. In general, for our examples, it should take 5-30 seconds. Another way to check the correctness it to browse the list of files in your iCloud.

It is a bit hidden in the menu. Here is the sequence:

Settings -> iCloud -> Storage and Backup -> Manage Storage -> Documents & Data -> Unknown

If the application works correctly you should see the note we created in our app:

The ‘unknown’ label comes from the fact that the application has not been uploaded and approved on the Apple Store yet.

Also note that users can delete files from iCloud from this screen at-will (without having to go through your app). So keep this in mind as you’re developing.

Congrats – you have built your first iCloud-aware application!

转载地址:http://boxno.baihongyu.com/

你可能感兴趣的文章
前端面试题
查看>>
人事管理系统——考勤系统需求分析
查看>>
P2522 [HAOI2011]Problem b
查看>>
java简繁转换(区分港台)
查看>>
maven简单使用
查看>>
Python中的函数与变量
查看>>
简单描述PHP发展历程
查看>>
LICS O(n*m)+前驱路径
查看>>
一种用指针方式实现队列的方式
查看>>
关于“Return empty arrays or collections, not nulls”的思考
查看>>
18 os/os.path模块中关于文件/目录常用的函数使用方法 (转)
查看>>
尝去哪些论坛
查看>>
mysql if示例
查看>>
IP地址根据子网掩码划分子网
查看>>
LAMP环境搭建博客
查看>>
WPF自定义控件
查看>>
Activiti学习(一) 环境搭建
查看>>
尼姆博弈(二)
查看>>
angular-cli 安装
查看>>
css高斯模糊背景,使用filter!
查看>>