博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
非AppStore应用检查更新教程(附Demo)
阅读量:4110 次
发布时间:2019-05-25

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

非AppStore应用更新检查的教程

鉴于近来发现很多人对于应用更新这一方面的了解比较少,所以就发个教程供大家参考。
App很简单,运行以后,只有一个Check按钮:
按下按钮,这个App会从服务器上检查更新数据,并且显示出新版本和新特性,且指引用户跳转到下载界面
修改版本号到最新版本,再次运行,就会看到最新版本的提示
看起来很简单的App,先大致说一下思路帮助理解:
按钮点击以后,程序会获取当前版本并向服务器发送数据,得到数据以后进行处理,判断是否有新版本并展示给用户,从而实现检查更新
按照上面的思路,先要搭建好服务器端
★首先,如果没有域名空间的童鞋请先去开通一个免费的域名空间(本人开通的是)
★接下来,在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=应用版本
对于我就输入:
就会看到”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://dnosi.baihongyu.com/

你可能感兴趣的文章
单链表的C实现
查看>>
_attribute_机制(转)
查看>>
LINUX 内核源文件介绍以及头文件介绍(转)
查看>>
ubuntu下sudo apt-get update Sources 404 Not Found [IP: 91.189.92.200 80]解决办法
查看>>
一步一步写万能makefile
查看>>
类的三大特性(封装,继承,多态)
查看>>
类的三大特性(封装,继承,多态)
查看>>
深入理解linux i节点(inode)
查看>>
基于java的延号排序方法,使用权重
查看>>
寒假读完《深入理解Java虚拟机》的总结
查看>>
一文总结Synchronized的各种用法并代码实现
查看>>
Spring的单例模式源码小窥
查看>>
后台服务的变慢排查思路(轻量级应用服务器中测试)
查看>>
MySQL中InnoDB事务的默认隔离级别测试
查看>>
MySQL中用replace批量替换数据
查看>>
tomcat整体架构与组成
查看>>
微服务的注册与发现
查看>>
采用jmeter进行抢购商品的压力测试
查看>>
贪心算法思路总结及其java实现
查看>>
广度优先搜索(BFS)总结及其Java实现
查看>>