iOS版本更新的实现方法

在iOS中,有时是需要写个更新提示的,经过自己晚上找的和实际实验的,总结如下:

在这个方法里

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

//版本更新

    [self VersionButton];

}

然后获取想在运行的版本:

-(void)VersionButton{

    //获取发布版本的verion

    

    NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSStringstringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APPID]] encoding:NSUTF8StringEncodingerror:nil];

    NSRange range;

    range = [string rangeOfString:@"\"version\":"];

    NSString *appVersion = [string substringWithRange:NSMakeRange(range.location + range.length+1, 5)];

    [self checkAppUpdate:appVersion];


}

我这样是写的比较死的,因为我的版本号设置是1.0.1这样子的,自己可以灵活获取。

然后在获取线上版本号来进行比较:

//当前版本与上线版本作比较

-(void)checkAppUpdate:(NSString *)appVersion{

    //这是Build

    NSString *version = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleVersion"];

    //这是version

//    NSString *version =[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

//    NSString *appInfo1 = [appInfo substringFromIndex:[appInfo rangeOfString:@"\"version\":"].location+10];

//    appInfo1 = [[appInfo1 substringToIndex:[appInfo1 rangeOfString:@","].location]stringByReplacingOccurrencesOfString:@"\"" withString:@""];

    NSLog(@"新版本:%@,当前版本:%@ ",appVersion,version);

    NSArray *a2 = [appVersion componentsSeparatedByString:@"."];

    NSArray *a1 = [version componentsSeparatedByString:@"."];

    for (int i = 0; i < [a1 count]; i++) {

        if ([a2 count] > i) {

            if ([[a1 objectAtIndex:i] floatValue] < [[a2 objectAtIndex:i] floatValue]) {

                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[NSStringstringWithFormat:@"新版本%@已发布!",appVersion] message:@"" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];

                alert.delegate = self;

                [alert addButtonWithTitle:@"前往更新"];

                [alert show];

                alert.tag = 20;

            }

            else if ([[a1 objectAtIndex:i] floatValue] > [[a2 objectAtIndex:i] floatValue])

            {


            }

        }

        else

        {


        }

    }

//    float doubleVersion = [version floatValue];

//    float doubleAppinfo1 = [appVersion floatValue];

//    NSLog(@"新版本:%f,当前版本:%f ",doubleVersion,doubleAppinfo1);

//    if (doubleAppinfo1 > doubleVersion) {

//        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"新版本%@已发布!",appVersion] message:@"" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];

//        alert.delegate = self;

//        [alert addButtonWithTitle:@"前往更新"];

//        [alert show];

//        alert.tag = 20;

//    }else{

        [[[UIAlertView alloc]initWithTitle:@"已是最高版本" message:nil delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil] show];

//    }

}

上面程序在用的是可以判断多位的版本,而下面注释掉的是只能判断2位的版本。
然后在加上,弹框按钮的代理:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1&&alertView.tag == 20) {

        NSString *url = @"https://itunes.apple.com/us/app/he-yi-hui/id1059964988?l=zh&ls=1&mt=8";

        [[UIApplication sharedApplicationopenURL:[NSURL URLWithString:url]];

    }

}

上面的url是你app所在App Store的页面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值