打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
非AppStore应用检查更新教程(附Demo)

非AppStore应用更新检查的教程

鉴于近来发现很多人对于应用更新这一方面的了解比较少,所以就发个教程供大家参考。

App很简单,运行以后,只有一个Check按钮:


按下按钮,这个App会从服务器上检查更新数据,并且显示出新版本和新特性,且指引用户跳转到下载界面


修改版本号到最新版本,再次运行,就会看到最新版本的提示



看起来很简单的App,先大致说一下思路帮助理解:
按钮点击以后,程序会获取当前版本并向服务器发送数据,得到数据以后进行处理,判断是否有新版本并展示给用户,从而实现检查更新

按照上面的思路,先要搭建好服务器端
★首先,如果没有域名空间的童鞋请先去http://us.5944.net开通一个免费的域名空间(本人开通的是http://hi.223366.info
★接下来,在Win上面用DreamWeaver建一个ASP VBScript,取名为“CheckUpdate.asp”输入下面的代码(我会解释的):
复制代码
  1. <html>
  2. <%@LANGUAGE="VBSCRIPT"CODEPAGE="65001"%>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

  4. <head>
  5. <title>版本检查</title>
  6. </head>

  7. <body>
  8. <%
  9. set fs=server.createobject("scripting.filesystemobject")
  10. file=server.mappath("Update.txt")
  11. set txt=fs.opentextfile(file,1,true)

  12. requestStr=request("type")

  13. txt.skipLine()
  14. line=txt.readline
  15. if line=requestStr then
  16.     resp*****e.write "Newest"
  17. else
  18.     resp*****e.write line
  19.     line=txt.readAll
  20.     resp*****e.write line
  21. end if
  22. %>
  23. </body>
  24. </html>


★接着建立一个Update.txt,在里面输入:
复制代码
  1. [Update]
  2. 1.0
  3. [New]
  4. ★新功能1
  5. ★新功能2
  6. ★新功能3
  7. ★新功能4
  8. ★新功能5


★把它和这个ASP放在一起,进行本地测试(也可以直接传到空间上测试)
输入网址:http://你的域名/CheckUpdate.asp?type=应用版本
对于我就输入:http://hi.223366.info/CheckUpdate.asp?type=1.0
就会看到”Newest“的提示,如果把版本换成0.9就可以看到
”1.0[New] ★新功能1 ★新功能2 ★新功能3 ★新功能4 ★新功能5“的提示

★这样就实现了一个使用服务器检查更新的功能,对上面的代码简单的解释以下

★上面代码会根据url中的"type"参数(也就是传过去的当前版本号)与txt中的最新版本号(这里是1.0)进行比对,如果相同,就显示”Newest“
如果不相同,就显示”新版本号 + [new] + 新功能"

★更新检查服务器的搭建大致讲到这里,当然也可以用PHP来实现,不过鉴于我对PHP一窍不通,就用ASP来实现


★接下来要做ios端的实现,添加一个类用于检查更新:

复制代码

  1. //★DFUpdateChecker.h

  2. #import <Foundation/Foundation.h>

  3. @protocol DFUpdateCheckerDelegate <NSObject>

  4. -(void)checkFinishedWithNewVersion:(NSString*)theNewVersion NewThing:(NSString*)theNewThing;

  5. @end

  6. @interface DFUpdateChecker:NSObject<NSURLConnectionDelegate>{
  7.     NSMutableData  *receivedData;
  8.     NSURLConnection *theConncetion;
  9.     
  10.     id<DFUpdateCheckerDelegate> delegate;
  11.     NSString *newVersion;
  12.     NSString *newThings;
  13.     
  14. }
  15. @property(retain,nonatomic)id<DFUpdateCheckerDelegate> delegate;
  16. -(void)cancelDownload;
  17. -(void)startCheckWithURLString:(NSString*)theURL;
  18. -(void)checkNew;
  19. -(id)init;
  20. @end


复制代码

  1. //★DFUpdateChecker.m

  2. #import "DFUpdateChecker.h"

  3. @implementation DFUpdateChecker
  4. @synthesize delegate;
  5. NSString *result;

  6. -(void)connection:(NSURLConnection*)connection didReceiveData:(NSData *)data{
  7.     [receivedData appendData:data];
  8. }

  9. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
  10.     [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
  11.     [connection release];
  12.     [receivedData release];
  13.     NSLog(@"Error");
  14. }

  15. -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
  16.     [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
  17.     
  18.     result=[result initWithData:receivedData encoding:NSUTF8StringEncoding];
  19.     result=(NSString*)[[result componentsSeparatedByString:@"<body>"] objectAtIndex:1];
  20.     result=[[result componentsSeparatedByString:@"</body>"] objectAtIndex:0];
  21.     result=[result stringByTrimmingCharactersInSet: [NSCharacterSet newlineCharacterSet]];

  22.     [connection release];
  23.     [receivedData release];
  24.     
  25.     if([result isEqualToString:@"Newest"]==YES){
  26.       newVersion=@"Newest";
  27.     }else{
  28.         newVersion=[newVersion initWithString:[[result componentsSeparatedByString:@"[New]"] objectAtIndex:0]];
  29.         newThings=[newThings initWithString:[[result componentsSeparatedByString:@"[New]"] objectAtIndex:1]];
  30.         newThings=[newThings stringByTrimmingCharactersInSet: [NSCharacterSet newlineCharacterSet]];
  31.     }
  32.     [delegate checkFinishedWithNewVersion:newVersion NewThing:newThings];
  33. }

  34. -(void)dealloc{
  35.     [super dealloc];
  36. }

  37. -(void)cancelDownload
  38. {
  39.     [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
  40.     [theConncetion cancel];
  41.     theConncetion = nil;
  42.     receivedData = nil;
  43. }

  44. -(NSString*)getNowVersion{
  45.     NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  46.     NSString *result_=[infoDictionary objectForKey:@"CFBundleShortVersi*****tring"];
  47.     return result_;
  48. }

  49. -(void)checkNew{
  50.     [self startCheckWithURLString:[NSString stringWithFormat:@"http://hi.223366.info/CheckUpdate.asp?type=%@",[self getNowVersion]]];
  51. }

  52. -(id)init{
  53.     if(self=[super init]){
  54.         result=[NSString alloc];
  55.         newVersion=[NSString alloc];
  56.         newThings=[NSString alloc];
  57.     }
  58.     return self;
  59. }

  60. -(void)startCheckWithURLString:(NSString*)theURL{
  61.     NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:theURL] cachePolicy:NSURLRequestUseProtocolCachePolicy  timeoutInterval:60.0];  
  62.     theConncetion=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];  
  63.     if(theConncetion){
  64.         receivedData=[[NSMutableData data] retain];
  65.         [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
  66.     }else{
  67.         NSLog(@"Can't start the connection!");
  68.     }
  69. }

  70. @end



★这个类用于异步获取更新数据,然后通过委托传递出去。注意把里面的检查url换成你自己的


★然后,在视图控制器上面加上这些实现代码:

复制代码

  1. -(void)checkFinishedWithNewVersion:(NSString *)theNewVersion NewThing:(NSString *)theNewThing{
  2.     [button setTitle:@"Check" forState:UIControlStateNormal];
  3.     [button setEnabled:YES];
  4.     UIAlertView *alert;
  5.     if(theNewVersion==@"Newest"){
  6.         alert=[[UIAlertView alloc]initWithTitle:@"软件更新" message:@"您的应用是最新版" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
  7.         [alert show];
  8.     }else{
  9.         NSString *text=[NSString stringWithFormat:@"有新的版本(%@)可供使用\n此版本有如下新特性:\n%@",theNewVersion,theNewThing];
  10.         alert=[[UIAlertView alloc]initWithTitle:@"软件更新" message:text delegate:self cancelButtonTitle:@"以后再说" otherButtonTitles:@"立即下载",nil];
  11.         [alert show];
  12.     }
  13.     [alert release];
  14. }

  15. -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
  16.     if(buttonIndex==0){
  17.         return;
  18.     }else if(buttonIndex==1){
  19.         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://bill123.5gbfree.com"]];
  20.     }
  21. }

  22. -(IBAction)checkButtonClicked:(id)sender{
  23.     [button setTitle:@"Checking" forState:UIControlStateNormal];
  24.     [button setEnabled:NO];
  25.     DFUpdateChecker *checker=[[DFUpdateChecker alloc]init];
  26.     checker.delegate=self;
  27.     [checker checkNew];
  28.     [checker release];
  29. }



★这样就OK了,运行结果请看最前面的图

Deam:http://download.csdn.net/detail/slinloss/4487682

  1.   

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
iOS 各种传值方式
UITextView的使用详解
NSXMLParser具体解析xml的应用详解
IOS开发教程自定义CheckBox控件 | 开发资源分享区
<objc/rumtime.h>
MPMoviePlayerController 电影播放器
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服