如何改进iOS App的离线使用体验
2014-02-26 10:07
483 查看
App Store中的App分析
App已经与我们形影不离了,不管在地铁上、公交上还是在会场你总能看到很多人拿出来手机,刷一刷微博,看看新闻。据不完全统计有近一半的用户在非Wifi环境打开App,以下为一个典型iPhone和Android App(50W+用户)的友盟后台数据:
3G、2G的数据连接往往不稳定(特别在公交或者地铁上),这时打开一些App就会像这样:
当然也会有一些体验很好的App,在离线状态下也能顺畅使用:
甚至提供了离线阅读功能:
如何做?
打开过的文章、下载过的音频、查看过的图片我们都希望Cache到本地,下次不用再向服务器请求。首先,我们为了最快让用户看到内容,会在ViewDidLoad加载Cache数据,如:
1.NSMutableURLRequest
Sample(参考麒麟的文章《iOS开发之缓存(一):内存缓存》来使用NSURLCache):2.ASIHTTPRequest
你可以从这里找到它的介绍:http://allseeing-i.com/ASIHTTPRequest/,在5.0/4.0及之前iOS版本,ASIHTTPRequest基本是主力的HTTP requests library,它本身也是Github中的开源项目,但是从iOS 5.0之后逐渐停止维护了。未来的项目可以使用AFNetworking或者MKNetworkKit代替ASIHTTPRequest。
ASIHTTPRequest的简介如下:
ASIHTTPRequest is an easy to use wrapper around the CFNetwork API that makes some of the more tedious aspects of communicating with web servers easier. It is written in Objective-C and works in both Mac OS X and
iPhone applications.
It is suitable performing basic HTTP requests and interacting with REST-based services (GET / POST / PUT / DELETE). The included ASIFormDataRequest subclass makes it easy to submit POST data and files usingmultipart/form-data.
ASIHTTPRequest库API设计的简单易用,并且支持block、queue、gzip等丰富的功能,这是该开源项目如此受欢迎的主要原因。
ASIHTTPRequest库中提供了ASIWebPageRequest组件用于请求网页,并且能把网页中的外部资源一并请求下来,但是我在实际项目中使用后发现有严重Bug,所以不建议使用。
ASIHTTPRequest库的介绍中也提到了它可以支持REST-based service,但是与Restfull API打交道我们往往使用下面介绍的的RestKit。
Sample:
3.RestKit
官方网站:http://restkit.org/,Github开源项目,与 RestfullAPI 的 Web服务打交道,这个库非常便捷,它也提供了很完整的Cache机制。
Sample:
data model到Core Data中:
Core Data support. Building on top of the object mapping layer, RestKit provides integration with Apple's Core Data framework. This support allows RestKit to persist remotely loaded objects directly back into a local
store, either as a fast local cache or a primary data store that is periodically synced with the cloud. RestKit can populate Core Data associations for you, allowing natural property based traversal of your data model. It also provides a nice API on top of
the Core Data primitives that simplifies configuration and querying use cases through an implementation of the Active Record access pattern.
但实际上RKRequestCachePolicy已经解决了大部分Cache需求。
4.SDWebImage
SDWebImage是Github开源项目:https://github.com/rs/SDWebImage,它用于方便的请求、Cache网络图片,并且请求完毕后交由UIImageView显示。Asynchronous image downloader with cache support with an UIImageView category.
SDWebImage作为UIImageView的一个Category提供的,所以使用起来非常简单:
5.UIWebView中的图片Cache
如果你使用UIWebView来展示内容,在离线情况下如果也想能显示的话需要实现2点:Cache Html页面
Cache 图片等元素
使用上面介绍的网络组件来Cache Html页面比较便捷,之后使用webView loadHTMLString即可加载本地Html页面,而Cache图片需要更换NSURLCache公共实例为自定义的NSURLCache(UIWebView使用的即是+[NSURLCache sharedURLCache]):
Reader开源项目这里参考的是:http://cocoawithlove.com/2010/09/substituting-local-data-for-remote.html
NewsReader中的介绍
《iOS News Reader开源项目》这篇文章介绍到的开源项目改进了离线使用体验:
在没有网络的情况下使用已经Cache过的所有数据:文章、图片、音频等等,用到的主要方案已经在上面介绍了,详细的请看源码:https://github.com/cubewang/NewsReader。
NewsReader项目因为历史演进的原因已经有些庞大了,需要进一步重构,在之后的项目中我们的客户端结构更精简。
另外欢迎加QQ群讨论:161561752
作者: 王克伟
出处: http://wangkewei.cnblogs.com/
版权声明: 本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任的权利。
您可以从这里更方便的找到我的文章。
相关文章推荐
- iphone开发教程
- Ipad开发课程系列目录--很好的教程,推荐给大家
- 【iOS-Cocos2d游戏开发】系列(总结了多篇文章,可以好好学习
- Objective-C语法的那些事儿(十篇文章,大家一起来学习)
- 分享15本iphone开发书籍,需要的来下载
- ASIHTTPRequest ASIWebThumbnail
- Xcode 不同版本的链接地址
- iphone开发 加速计
- ios 仿facebook 滑动导航菜单实现
- Ios 仿ibooks 翻页效果
- IPHONE开发 VIEW之间的相互切换
- IPHONE开发 文本框回车 虚拟键盘消失
- IPHONE开发 在VIEW之间传递参数
- IPHONE开发 @synthesize 变量下划线
- IOS 开发笔记
- iphone开发 运行模拟器出现"ARC forbids explicit message send of 'retain' 错误信息
- IPHONE开发 在VIEW中点击按钮如何转入TAB BAR VIEW
- iphone开发 二维码和条形码的识别
- Xcode4.5运行IOS6以前版本测试
- 下拉刷新列表