非AppStore应用更新检查的教程

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

< %@LANGUAGE="VBSCRIPT"CODEPAGE="65001"%>


版本检查


< %set fs=server.createobject("scripting.filesystemobject")file=server.mappath("Update.txt")set txt=fs.opentextfile(file,1,true)requestStr=request("type")txt.skipLine()line=txt.readlineif line=requestStr then response.write "Newest"else response.write line line=txt.readAll response.write lineend if%>


★接着建立一个Update.txt,在里面输入:
[Update]
1.0
[New]
★新功能1
★新功能2
★新功能3
★新功能4
★新功能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端的实现,添加一个类用于检查更新:
//★DFUpdateChecker.h
#import
@protocol DFUpdateCheckerDelegate
-(void)checkFinishedWithNewVersion:(NSString*)theNewVersion NewThing:(NSString*)theNewThing;
@end
@interface DFUpdateChecker:NSObject{
NSMutableData *receivedData;
NSURLConnection *theConncetion;
id delegate;
NSString *newVersion;
NSString *newThings;
}
@property(retain,nonatomic)id
delegate;
-(void)cancelDownload;
-(void)startCheckWithURLString:(NSString*)theURL;
-(void)checkNew;
-(id)init;
@end
//★DFUpdateChecker.m
#import "DFUpdateChecker.h"
@implementation DFUpdateChecker
@synthesize delegate;
NSString *result;
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData *)data{
[receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
[connection release];
[receivedData release];
NSLog(@"Error");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
result=[result initWithData:receivedData encoding:NSUTF8StringEncoding];
result=(NSString*)[[result componentsSeparatedByString:@""] objectAtIndex:1];
result=[[result componentsSeparatedByString:@""] objectAtIndex:0];
result=[result stringByTrimmingCharactersInSet: [NSCharacterSet newlineCharacterSet]];
[connection release];
[receivedData release];
if([result isEqualToString:@"Newest"]==YES){
newVersion=@"Newest";
}else{
newVersion=[newVersion initWithString:[[result componentsSeparatedByString:@"[New]"] objectAtIndex:0]];
newThings=[newThings initWithString:[[result componentsSeparatedByString:@"[New]"] objectAtIndex:1]];
newThings=[newThings stringByTrimmingCharactersInSet: [NSCharacterSet newlineCharacterSet]];
}
[delegate checkFinishedWithNewVersion:newVersion NewThing:newThings];
}
-(void)dealloc{
[super dealloc];
}
-(void)cancelDownload
{
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
[theConncetion cancel];
theConncetion = nil;
receivedData = nil;
}
-(NSString*)getNowVersion{
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *result_=[infoDictionary objectForKey:@"CFBundleShortVersionString"];
return result_;
}
-(void)checkNew{
[self startCheckWithURLString:[NSString stringWithFormat:@"http://hi.223366.info/CheckUpdate.asp?type=%@",[self getNowVersion]]];
}
-(id)init{
if(self=[super init]){
result=[NSString alloc];
newVersion=[NSString alloc];
newThings=[NSString alloc];
}
return self;
}
-(void)startCheckWithURLString:(NSString*)theURL{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:theURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
theConncetion=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConncetion){
receivedData=[[NSMutableData data] retain];
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
}else{
NSLog(@"Can't start the connection!");
}
}
@end

★这个类用于异步获取更新数据,然后通过委托传递出去。注意把里面的检查url换成你自己的
★然后,在视图控制器上面加上这些实现代码:
-(void)checkFinishedWithNewVersion:(NSString *)theNewVersion NewThing:(NSString *)theNewThing{
[button setTitle:@"Check" forState:UIControlStateNormal];
[button setEnabled:YES];
UIAlertView *alert;
if(theNewVersion==@"Newest"){
alert=[[UIAlertView alloc]initWithTitle:@"软件更新" message:@"您的应用是最新版" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
;
}else{
NSString *text=[NSString stringWithFormat:@"有新的版本(%@)可供使用\n此版本有如下新特性:\n%@",theNewVersion,theNewThing];
alert=[[UIAlertView alloc]initWithTitle:@"软件更新" message:text delegate:self cancelButtonTitle:@"以后再说" otherButtonTitles:@"立即下载",nil];
;
}
;
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if(buttonIndex==0){
return;
}else if(buttonIndex==1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://bill123.5gbfree.com"]];
}
}
-(IBAction)checkButtonClicked:(id)sender{
[button setTitle:@"Checking" forState:UIControlStateNormal];
[button setEnabled:NO];
DFUpdateChecker *checker=[[DFUpdateChecker alloc]init];
checker.delegate=self;
[checker checkNew];
[checker release];
}

★这样就OK了,运行结果请看最前面的图
★因为编写有些匆忙,并且本人仅仅只有初一,所以部分地方如果有问题可以回复告诉我,我会尽早学习改正,谢谢。
[dl href=”http://vdisk.weibo.com/s/8p9pc”]点此下载非AppStore应用更新检查源码[/dl]