检测版本号更新操作新葡京8455,关于检测App更新

作者:新闻中心

关于检测更新,苹果审核是不允许弹出提示更新的提示的,所以可以后台设置应用商店里面已经上架的appid,等上架后再改回新版的appid。

[objc] view plain copy

在迭代项目的过程中,曾出现过在个人中心里出现莫名其妙的卡顿,很是摸不着头脑,最开始以为后台的接口卡顿,但是试了好多次也不是,因为网络请求毕竟使用的是AFNetworking,网络的请求是放在子线程里面的。最后找到了是检测App的方法卡住了主线程。

1.一定要先配置自己项目在商店的APPID(从苹果开发者中心登录自己的账号可看),配置完最好在真机上运行才能看到完全效果哦!

关于自动提醒更新,直接上代码:

-(void)hsUpdateApp

/**

/**

#import"ViewController.h"//1一定要先配置自己项目在商店的APPID,配置完最好在真机上运行才能看到完全效果哦

{

app版本检测

  • 天朝专用检测app更新一句代码实现检测更新,很简单哦 (需要在viewDidAppear完成时,再调用改方法。不然 在网速飞快的时候,会出现一个bug,就是当前控制器viewDidLoad调用的话,可能当前视 图还没加载完毕就需要推出UIAlertAction)*/-hsUpdateApp{//2先获取当前工程项目版本号NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];NSString *currentVersion=infoDic[@"CFBundleShortVersionString"];

    //3从网络获取appStore版本号NSError *error;NSData *response = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"] returningResponse:nil error:nil];if (response == nil) {NSLog(@"你没有连接网络哦");return;}NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];if {NSLog(@"hsUpdateAppError:%@",error);return;}// NSLog(@"%@",appInfoDic);NSArray *array = appInfoDic[@"results"];NSDictionary *dic = array[0];NSString *appStoreVersion = dic[@"version"];//打印版本号NSLog(@"当前版本号:%@n商店版本号:%@",currentVersion,appStoreVersion);//设置版本号currentVersion = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];if (currentVersion.length==2) {currentVersion = [currentVersion stringByAppendingString:@"0"];}else if (currentVersion.length==1){currentVersion = [currentVersion stringByAppendingString:@"00"];}appStoreVersion = [appStoreVersion stringByReplacingOccurrencesOfString:@"." withString:@""];if (appStoreVersion.length==2) {appStoreVersion = [appStoreVersion stringByAppendingString:@"0"];}else if (appStoreVersion.length==1){appStoreVersion = [appStoreVersion stringByAppendingString:@"00"];}

    //4当前版本号小于商店版本号,就更新if([currentVersion floatValue] < [appStoreVersion floatValue]){UIAlertController *alercConteoller = [UIAlertController alertControllerWithTitle:@"版本有更新" message:[NSString stringWithFormat:@"检测到新版本,是否更新?",dic[@"version"]] preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *actionYes = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {//此处加入应用在app store的地址,方便用户去更新,一种实现方式如下NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"", STOREAPPID]];[[UIApplication sharedApplication] openURL:url];}];UIAlertAction *actionNo = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }];[alercConteoller addAction:actionYes];[alercConteoller addAction:actionNo];[self presentViewController:alercConteoller animated:YES completion:nil];
    

    }else{NSLog(@"版本号好像比商店大噢!检测到不需要更新");}

#defineSTOREAPPID @"1080182980"

//2先获取当前工程项目版本号

*/

}

@interfaceViewController ()@end@implementationViewController- viewDidLoad {

NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];

(void)updateApp:(UIViewController *)VC{

[super viewDidLoad];//一句代码实现检测更新[self hsUpdateApp];

NSString *currentVersion=infoDic[@"CFBundleShortVersionString"];

//2先获取当前工程项目版本号

}/**

//3从网络获取appStore版本号

NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];

* 天朝专用检测app更新*/-hsUpdateApp

NSError *error;

NSString *currentVersion=infoDic[@"CFBundleShortVersionString"];

{//2先获取当前工程项目版本号NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];

NSData *response = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"] returningResponse:nil error:nil];

//3从网络获取appStore版本号

NSString*currentVersion=infoDic[@"CFBundleShortVersionString"];//3从网络获取appStore版本号NSError *error;

if (response == nil) {

NSError *error;

NSData*response = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"] returningResponse:nil error:nil];if(response ==nil) {

NSLog(@"你没有连接网络哦");

NSData *response = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"] returningResponse:nil error:nil];

NSLog(@"你没有连接网络哦");return;

return;

if (response == nil) {

}

}

NSLog(@"你没有连接网络哦");

NSDictionary*appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];if {

NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];

[Factory alertMes:@"您没有连接网络"];

NSLog(@"hsUpdateAppError:%@",error);return;

if (error) {

return;

}

NSLog(@"hsUpdateAppError:%@",error);

}

NSArray*array = appInfoDic[@"results"];

return;

NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];

NSDictionary*dic = array[0];

}

if (error) {

NSString*appStoreVersion = dic[@"version"];//打印版本号NSLog(@"当前版本号:%@n商店版本号:%@",currentVersion,appStoreVersion);//4当前版本号小于商店版本号,就更新if([currentVersion floatValue] <[新葡京8455,appStoreVersion floatValue])

NSArray *array = appInfoDic[@"results"];

NSLog(@"hsUpdateAppError:%@",error);

{

NSDictionary *dic = array[0];

return;

UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@"版本有更新"message:[NSString stringWithFormat:@"检测到新版本,是否更新?",appStoreVersion]delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"更新",nil];

NSString *appStoreVersion = dic[@"version"];

}

[alert show];

//打印版本号

//    NSLog(@"%@",appInfoDic);

}else{

NSLog(@"当前版本号:%@n商店版本号:%@",currentVersion,appStoreVersion);

NSArray *array = appInfoDic[@"results"];

NSLog(@"版本号好像比商店大噢!检测到不需要更新");

//4当前版本号小于商店版本号,就更新

if (array.count == 0 || array == nil) {

}

if([currentVersion floatValue] < [appStoreVersion floatValue])

NSLog(@"还没上线");

}

{

}else{

- alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本有更新" message:[NSString stringWithFormat:@"检测到新版本(%@),是否更新?",appStoreVersion] delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"更新",nil];

NSDictionary *dic = array[0];

{//5实现跳转到应用商店进行更新if(buttonIndex==1)

[alert show];

NSString *appStoreVersion = dic[@"version"];

{//6此处加入应用在app store的地址,方便用户去更新,一种实现方式如下:NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"", STOREAPPID]];

}else{

//打印版本号

[[UIApplication sharedApplication] openURL:url];

NSLog(@"版本号好像比商店大噢!检测到不需要更新");

NSLog(@"当前版本号:%@n商店版本号:%@",currentVersion,appStoreVersion);

}

}

//设置版本号

}@end

}

currentVersion = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];

- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

if (currentVersion.length==2) {

{

currentVersion  = [currentVersion stringByAppendingString:@"0"];

//5实现跳转到应用商店进行更新

}else if (currentVersion.length==1){

if(buttonIndex==1)

currentVersion  = [currentVersion stringByAppendingString:@"00"];

{

}

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id"]];

appStoreVersion = [appStoreVersion stringByReplacingOccurrencesOfString:@"." withString:@""];

}

if (appStoreVersion.length==2) {

}

appStoreVersion  = [appStoreVersion stringByAppendingString:@"0"];

}else if (appStoreVersion.length==1){

appStoreVersion  = [appStoreVersion stringByAppendingString:@"00"];

}

//4当前版本号小于商店版本号,就更新

if([currentVersion floatValue] < [appStoreVersion floatValue])

{

//初始化AlertView

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本有更新"

message:[NSString stringWithFormat:@"检测到新版本(%@),是否更新?",dic[@"version"]]

delegate:self

cancelButtonTitle:@"取消"

otherButtonTitles:@"更新",nil];

[alert show];

//        UIAlertController *alercConteoller = [UIAlertController alertControllerWithTitle:@"版本有更新" message:[NSString stringWithFormat:@"检测到新版本(%@),是否更新?",dic[@"version"]] preferredStyle:UIAlertControllerStyleAlert];

//        UIAlertAction *actionYes = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

//            //此处加入应用在app store的地址,方便用户去更新,一种实现方式如下

//            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"", STOREAPPID]];

//            [[UIApplication sharedApplication] openURL:url];

//        }];

//        UIAlertAction *actionNo = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

//

//        }];

//        [alercConteoller addAction:actionYes];

//        [alercConteoller addAction:actionNo];

//        [VC presentViewController:alercConteoller animated:YES completion:nil];

}else{

NSLog(@"版本号好像比商店大噢!检测到不需要更新");

}

}

}

这个方法是App去App Store调取版本号的相关信息,但是会卡主线程,导致体验极差。

在优化的过程中,我的解决卡顿办法是,让后台控制App的更新,而不是去App Store。在App上架完成之后,告诉后台更改版本号,以提示用户更新。

本文由新葡京8455发布,转载请注明来源

关键词: