1.代码的实现
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
:(NSDictionary *)launchOptions
{
if ([self checkVersion])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"有新版本请更新" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil];
[alert show];
[alert release];
}
return YES;
}
//检查版本号 判断是否需要更新
- (BOOL)checkVersion
{
//当前的版本号
NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
// 获取服务器的版本号
NSString *serverUrl = @"https://网址/文件名.plist";
NSURL * url = [NSURL URLWithString:serverUrl];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
if (data != nil)
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
//存储的路径 真机测试下 在document问价后面加/如下/TCal.plist
NSString *pathPath = [[documentPaths objectAtIndex:0] stringByAppendingString:@"/文件的名字.plist"];
if ([data writeToFile:pathPath atomically:YES])
{
NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:pathPath];
if (dic)
{
NSArray *array = [dic objectForKey:@"items"];
NSDictionary *metadata = [array[0] objectForKey:@"metadata"];
//服务器版本号
NSString *serverVersion = [metadata objectForKey:@"bundle-version"];
//比较版本号 判断是否需要更新
if (currentVersion.floatValue < serverVersion.floatValue)
{
return YES;
}
else
{
return NO;
}
}
else
{
NSLog(@"数据为空");
}
}
else
{
NSLog(@"save fail");
}
}
else
{
NSLog(@"error : %@",error);
}
}
#pragma mark --- 更新的弹出视图的代理方法
//警告框
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1 )
{
//下载地址 可以是plist文件
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"itms-services://?action=download-manifest&url=https://网址/文件名.plist"]];
}
}
2.打包
3.放置到服务器上
将plist文件和spa包放到服务器上