iOS应用版本更新

#import "AppDelegate.h" 文件中的application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中调用检测结果

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

    //ios应用版本更新

    [self VersionClick];

    return YES;

}

获得发布版本的Version

//更新实现方法

-(void)VersionClick{

//获取发布版本的Version,id替换成自己应用的id

    NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://itunes.apple.com/lookup?id=662004496"] encoding:NSUTF8StringEncoding error:nil];

    if (string !=nil && [string length]>0 && [string rangeOfString:@"version"].length==7) {

        [self checkAppUpdata:string];

    }

}

比较当前版本与新上线版本做比较

#pragma mark ---比较当前版本与新上线版本做比较

-(void)checkAppUpdata:(NSString *)appInfo{

    //获取当前版本

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

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

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

    //判读,如果不同,则进入更新

    if (![appInfo1 isEqualToString:version]) {

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

        if ([[UIDevice currentDevice].systemVersion intValue] >= 9.0) {

            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"新版本 %@ 已发布!",appInfo1] preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

                

            }];

            UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"前往更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

                [self Updata];

            }];

            [alertController addAction:cancelAction];

            [alertController addAction:otherAction];

            

        }else

        {

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

            alert.delegate = self;

            [alert show];

        }

    }

}

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

    if (buttonIndex ==1 ) {

        [self Updata];

    }

}

-(void)Updata{

    NSString *url = @"";//填写自己应用的更新地址

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

}

猜你喜欢

转载自blog.csdn.net/Kevin_love_Chrissy/article/details/50246727