cocos2d-x 项目中需要在游戏里提示版本有更新
代码如下,在Oc的主函数中新增(本人的为AppController.mm)
//app强制更新
//定义的app的地址
NSString *urld = [NSString stringWithFormat:@" http://itunes.apple.com/cn/lookup?id=%@
",@"你的appleID"]; //这里注意:链接不要动,这是苹果官方给出的接口,返回Json格式的版本信息
//网络请求app的信息,主要是取得我说需要的Version
NSURL *url = [NSURL URLWithString:urld];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init];
if (data) {
//[NSThread sleepForTimeInterval:1.0];
//data是有关于App所有的信息
NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
if ([[receiveDic valueForKey:@"resultCount"] intValue]>0) {
[receiveStatusDic setValue:@"1" forKey:@"status"];
[receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"] forKey:@"version"];
//请求的有数据,进行版本比较
[self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO];
}else{
[receiveStatusDic setValue:@"-1" forKey:@"status"];
}
}else{
[receiveStatusDic setValue:@"-1" forKey:@"status"];
}
}];
[task resume];
//定义的app的地址
NSString *urld = [NSString stringWithFormat:@" http://itunes.apple.com/cn/lookup?id=%@
",@"你的appleID"]; //这里注意:链接不要动,这是苹果官方给出的接口,返回Json格式的版本信息
//网络请求app的信息,主要是取得我说需要的Version
NSURL *url = [NSURL URLWithString:urld];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init];
if (data) {
//[NSThread sleepForTimeInterval:1.0];
//data是有关于App所有的信息
NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
if ([[receiveDic valueForKey:@"resultCount"] intValue]>0) {
[receiveStatusDic setValue:@"1" forKey:@"status"];
[receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"] forKey:@"version"];
//请求的有数据,进行版本比较
[self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO];
}else{
[receiveStatusDic setValue:@"-1" forKey:@"status"];
}
}else{
[receiveStatusDic setValue:@"-1" forKey:@"status"];
}
}];
[task resume];
新增两个函数
-(void)updateVersion{
NSString *msg = [NSString stringWithFormat:@"又出新版本啦,快点更新吧!"];
// [NSThread sleepForTimeInterval:1.0]; //这个可以不用
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升级提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
// Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"下次再说" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"现在升级" style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"App Store下载链接"]];
[[UIApplication sharedApplication]openURL:url];
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}
-(void)receiveData:(id)sender
{
//获取APP自身版本号
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."];
//版本号比较
if(localArray.count == 2 && versionArray.count == 2)
{
if ([localArray[0] intValue] < [versionArray[0] intValue])
{
[self updateVersion];
}
else if ([localArray[0] intValue] == [versionArray[0] intValue])
{
if ([localArray[1] intValue] < [versionArray[1] intValue])
{
[self updateVersion];
}
}
}
if(localArray.count == 2 && versionArray.count == 3)
{
if ([localArray[0] intValue] < [versionArray[0] intValue])
{
[self updateVersion];
}
else if ([localArray[0] intValue] == [versionArray[0] intValue])
{
if ([localArray[1] intValue] < [versionArray[1] intValue])
{
[self updateVersion];
}
else if ([localArray[1] intValue] == [versionArray[1] intValue])
{
if([versionArray[2] intValue] > 0)
{
[self updateVersion];
}
}
}
}
if(localArray.count == 3 && versionArray.count == 2)
{
if([localArray[0] intValue] < [versionArray[0] intValue])
{
[self updateVersion];
}
else if ([localArray[0] intValue] == [versionArray[0] intValue])
{
if ([localArray[1] intValue] < [versionArray[1] intValue])
{
[self updateVersion];
}
}
}
if ((versionArray.count == 3) && (localArray.count == versionArray.count)) {
if ([localArray[0] intValue] < [versionArray[0] intValue]) {
[self updateVersion];
}else if ([localArray[0] intValue] == [versionArray[0] intValue]){
if ([localArray[1] intValue] < [versionArray[1] intValue]) {
[self updateVersion];
}else if ([localArray[1] intValue] == [versionArray[1] intValue]){
if ([localArray[2] intValue] < [versionArray[2] intValue]) {
[self updateVersion];
}
}
}
}
}
NSString *msg = [NSString stringWithFormat:@"又出新版本啦,快点更新吧!"];
// [NSThread sleepForTimeInterval:1.0]; //这个可以不用
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升级提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
// Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"下次再说" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"现在升级" style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"App Store下载链接"]];
[[UIApplication sharedApplication]openURL:url];
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}
-(void)receiveData:(id)sender
{
//获取APP自身版本号
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."];
//版本号比较
if(localArray.count == 2 && versionArray.count == 2)
{
if ([localArray[0] intValue] < [versionArray[0] intValue])
{
[self updateVersion];
}
else if ([localArray[0] intValue] == [versionArray[0] intValue])
{
if ([localArray[1] intValue] < [versionArray[1] intValue])
{
[self updateVersion];
}
}
}
if(localArray.count == 2 && versionArray.count == 3)
{
if ([localArray[0] intValue] < [versionArray[0] intValue])
{
[self updateVersion];
}
else if ([localArray[0] intValue] == [versionArray[0] intValue])
{
if ([localArray[1] intValue] < [versionArray[1] intValue])
{
[self updateVersion];
}
else if ([localArray[1] intValue] == [versionArray[1] intValue])
{
if([versionArray[2] intValue] > 0)
{
[self updateVersion];
}
}
}
}
if(localArray.count == 3 && versionArray.count == 2)
{
if([localArray[0] intValue] < [versionArray[0] intValue])
{
[self updateVersion];
}
else if ([localArray[0] intValue] == [versionArray[0] intValue])
{
if ([localArray[1] intValue] < [versionArray[1] intValue])
{
[self updateVersion];
}
}
}
if ((versionArray.count == 3) && (localArray.count == versionArray.count)) {
if ([localArray[0] intValue] < [versionArray[0] intValue]) {
[self updateVersion];
}else if ([localArray[0] intValue] == [versionArray[0] intValue]){
if ([localArray[1] intValue] < [versionArray[1] intValue]) {
[self updateVersion];
}else if ([localArray[1] intValue] == [versionArray[1] intValue]){
if ([localArray[2] intValue] < [versionArray[2] intValue]) {
[self updateVersion];
}
}
}
}
}
然后就好啦!启动就会有提示更新的啦
版本号支持到3位,比如2.5.17这样的,4位的自己新增就好啦