您的位置:首页 > 移动开发

如何改进iOS App的离线使用体验

2014-11-13 10:58 483 查看
AppStore中的App分析

App已经与我们形影不离了,不管在地铁上、公交上还是在会场你总能看到很多人拿出来手机,刷一刷微博,看看新闻。

据不完全统计有近一半的用户在非Wifi环境打开App,以下为一个典型iPhone和AndroidApp(50W+用户)的友盟后台数据


如何做?

打开过的文章、下载过的音频、查看过的图片我们都希望Cache到本地,下次不用再向服务器请求。

首先,我们为了最快让用户看到内容,会在ViewDidLoad加载Cache数据,如:

?
1

2

3

4
-
(
void
)viewDidLoad{


[selfgetArticleList:0
length:SECTION_LENGTHuseCacheFirst:YES];


}


然后在viewDidAppear中向服务器请求最新数据,如

?
1

2

3

4

5

6

7

8
-
(
void
)viewDidAppear:(
BOOL
)animated{




[superviewDidAppear:animated];


//...


[selfgetArticleList:0
length:SECTION_LENGTHuseCacheFirst:NO]


}


当然这里的getArticleList接口有useCacheFirst参数,我们需要网络请求模块能够支持这一点,下面就介绍这些库和工具。(借助一些工具很容易能做到这些,而不用自己造轮子。遵循“凡事都应该最简单,而不过于简陋”的原则,这里整理一下,方便项目中使用)。


1.NSMutableURLRequest

Sample(参考麒麟的文章《iOS开发之缓存(一):内存缓存》来使用NSURLCache):

?
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32
NSString
*paramURLAsString=@
"http://www.baidu.com/"
;


if
([paramURLAsStringlength]
==0){


NSLog(@
"Nilor
emptyURLisgiven"
);


return
;


}


NSURLCache
*urlCache=[NSURLCachesharedURLCache];


[urlCache
setMemoryCapacity:1*1024*1024];


//创建一个nsurl


NSURL
*url=[NSURLURLWithString:paramURLAsString];


//创建一个请求


NSMutableURLRequest
*request=


[NSMutableURLRequest


requestWithURL:url


cachePolicy:NSURLRequestUseProtocolCachePolicy


timeoutInterval:60.0f];


//从请求中获取缓存输出


NSCachedURLResponse
*response=


[urlCache
cachedResponseForRequest:request];


//判断是否有缓存


if
(response!=
nil){


NSLog(@
"如果有缓存输出,从缓存中获取数据"
);


[requestsetCachePolicy:NSURLRequestReturnCacheDataDontLoad];


}


self.connection
=nil;


NSURLConnection
*newConnection=


[[NSURLConnection
alloc]initWithRequest:request


delegate:self


startImmediately:YES];


self.connection
=newConnection;


[newConnection
release];


但是NSMutableURLRequest使用起来不够简便,在实际项目中我很少用它,而基本使用ASIHTTPRequest来代替。


2.ASIHTTPRequest

你可以从这里找到它的介绍:http://allseeing-i.com/ASIHTTPRequest/,在5.0/4.0及之前iOS版本,ASIHTTPRequest基本是主力的HTTPrequestslibrary,它本身也是Github中的开源项目,但是从iOS
5.0之后逐渐停止维护了。未来的项目可以使用AFNetworking或者MKNetworkKit代替ASIHTTPRequest。

ASIHTTPRequest的简介如下:

ASIHTTPRequestisaneasytousewrapperaroundtheCFNetworkAPIthatmakessomeofthemoretediousaspectsofcommunicatingwithwebserverseasier.ItiswritteninObjective-CandworksinbothMacOSXandiPhoneapplications.

ItissuitableperformingbasicHTTPrequestsandinteractingwithREST-basedservices(GET/POST/PUT/DELETE).TheincludedASIFormDataRequestsubclassmakesiteasytosubmitPOSTdataandfilesusingmultipart/form-data.

ASIHTTPRequest库API设计的简单易用,并且支持block、queue、gzip等丰富的功能,这是该开源项目如此受欢迎的主要原因。

ASIHTTPRequest库中提供了ASIWebPageRequest组件用于请求网页,并且能把网页中的外部资源一并请求下来,但是我在实际项目中使用后发现有严重Bug,所以不建议使用。

ASIHTTPRequest库的介绍中也提到了它可以支持REST-basedservice,但是与RestfullAPI打交道我们往往使用下面介绍的的RestKit。

Sample:

?
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40
NSMutableString
*requestedUrl=[[NSMutableStringalloc]initWithString:self.url];


//如果优先使用本地数据


ASICachePolicy
policy=_useCacheFirst?ASIOnlyLoadIfNotCachedCachePolicy


:(ASIAskServerIfModifiedCachePolicy
|ASIFallbackToCacheIfLoadFailsCachePolicy);


asiRequest
=[ASIHTTPRequestrequestWithURL:


[NSURLURLWithString:[requestedUrlstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];


[asiRequest
setDownloadCache:[ASIDownloadCachesharedCache]];


[asiRequest
setCachePolicy:policy];


[asiRequestsetCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];


//
Connection


if
(_connectionType==
ConnectionTypeAsynchronously){




[asiRequestsetDelegate:self];


[asiRequeststartAsynchronous];




//Tell
we'rereceiving.


if
(!_canceled&&
[_delegaterespondsToSelector:@selector(downloaderDidStart:)])


[_delegatedownloaderDidStart:self];


}


else


{


[asiRequeststartSynchronous];




NSError*error
=[asiRequesterror];




if
(!error)


{


[selfrequestFinished:asiRequest];


}


else


{


[selfrequestFailed:asiRequest];


}


}


[requestedUrl
release];


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: