原文地址: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!

转载于:https://www.cnblogs.com/pengyingh/articles/2339507.html

Beginning iCloud in iOS 5 Tutorial Part 2(转载)相关推荐

  1. Introduction to In-App Purchases in iOS 6 Tutorial

    From: http://www.raywenderlich.com/21081/introduction-to-in-app-purchases-in-ios-6-tutorial Note fro ...

  2. UIKit Particle Systems in iOS 5 Tutorial ( 附雨的粒子效果 )

    原文地址:http://www.raywenderlich.com/6063/uikit-particle-systems-in-ios-5-tutorial?utm_source=feedburne ...

  3. iOS Newsstand Tutorial(IOS报刊亭教程)

    原文地址: http://blog.5dscape.cn/post/2012-09-06/40038111026 一种新的方式来分发杂志 由于iPad的时代的开始,在平板电脑中最欣赏的功能之一就是看杂 ...

  4. iCloud配置问题(iOS 支持iCloud)

    关于iOS支持iCloud Drive配置问题 首先解释下:iCloud是什么?有何作用? iCloud是苹果公司所提供的云端服务,使用者可以免费储存5GB的资料. 可以备份存放照片.音乐.通讯录.短 ...

  5. iOS Newsstand Tutorial

    http://stackoverflow.com/questions/8539310/ios-5-newsstand-application-icon http://www.viggiosoft.co ...

  6. ICLOUD储存空间要升级吗_有人像我一样需要恢复苹果手机icloud空间ios备份时 微信卡住不动了吗(已解决)...

    童鞋们 想告诉你们一个振奋人心的消息-我的微信刚刚恢复成功(o^^o)三天前我一度以为保存n年的微信记录要离我远去了- 我找到了一个关键环节就是 恢复备份必须是在一定条件下才能进行 譬如说连接电源等等 ...

  7. iOS -- 学习资料总结(转载)

    先著名转载地址:https://www.jianshu.com/p/c47c24ab1e76/ 关于iOS学习进阶的必读一些博客总结 经过一周的思考还是决定重组一下优秀的博客,首次整理这些博客比较乱, ...

  8. iOS 发布证书申请 (转载)

    概述 由于苹果的机制,在非越狱机器上安装应用必须通过官方的Appstore,开发者开发好应用后上传Appstore,也需要通过审核等环节.AppCan不仅能实现跨平台开发,也支持上传ipa包至Apps ...

  9. iOS RAC 的使用总结 (转载)

    ReactiveCocoa(简称RAC),是GitHub上开源的一个应用于iOS和OS X开发的一个新框架.RAC具有函数式编程和响应者编程的特性. ReactiveCocoa解决的问题: 1.传统i ...

最新文章

  1. electron.js_在使用Electron.js之前我希望知道的事情
  2. java 二维码生成和解析
  3. 人民币升值与美元贬值
  4. Mc神秘·音乐-舞曲网
  5. 递归算法小结(数的阶乘、斐波那契和汉诺塔问题)
  6. POJ2301+水~~~~~~
  7. 10大申请攻略+套磁技巧助你完胜美研申请
  8. 计算机模块考试注意事项,常考计算机模块考试操作注意事项.doc
  9. MacBook(macOS) 如何安装 Homebrew Cask(作废)
  10. IP地址专题六:计算相关地址
  11. Springboot 统一maper 和逆向工程
  12. 001-JavaScript简介
  13. 周立功arm linux教程,极速搭建周立功IMX287A ARM Linux开发环境
  14. Win10如何设置护眼模式?
  15. ERP是属于系统还是软件?
  16. 机器学习算法工程师领域现状
  17. RK3399 修改android桌面图标默认大小
  18. Jedis分析一-Pipeline is currently not supported for JedisClusterConnection.
  19. android 调出键盘表情_Android 显示输入法中的emoji表情以及String字符
  20. Linux 显示文件内行号显示

热门文章

  1. 几何画板手机版_运用几何画板解决动点最值问题(二)
  2. 挖掘城市ip_不断挖掘IP价值,紧抓乐园经济新机遇!
  3. mysql 事务 引擎_mysql引擎和事务
  4. java http请求原理_浅谈Spring Cloud zuul http请求转发原理
  5. 计算机概论在线阅读,计算器概论 or 计算机概论
  6. Oracle rowid和rownum的区别
  7. SSM 整合 4:Spring IoC 容器基于的两个重要接口 BeanFactory 和 ApplicationContext
  8. 回放导出数据_王者荣耀本地视频怎么导出MP4格式视频文件?答案在这里
  9. poj 2777(线段树的节点更新策略)
  10. 计算机视觉领域常见期刊和会议,计算机视觉领域常见期刊和会议