iOS应用版本更新

来源:互联网 发布:php实现注册登录 编辑:程序博客网 时间:2024/05/01 15:00

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

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

    //ios应用版本更新

    [selfVersionClick];

    returnYES;

}

获得发布版本的Version

//更新实现方法

-(void)VersionClick{

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

    NSString *string = [NSStringstringWithContentsOfURL:[NSURLURLWithString:@"http://itunes.apple.com/lookup?id=662004496"]encoding:NSUTF8StringEncodingerror:nil];

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

        [selfcheckAppUpdata:string];

    }

}

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

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

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

    //获取当前版本

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

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

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

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

    if (![appInfo1isEqualToString:version]) {

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

        if ([[UIDevicecurrentDevice].systemVersionintValue] >= 9.0) {

            UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:nilmessage:[NSStringstringWithFormat:@"新版本 %@已发布!",appInfo1]preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"知道了"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *action) {

                

            }];

            UIAlertAction *otherAction = [UIAlertActionactionWithTitle:@"前往更新"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {

                [selfUpdata];

            }];

            [alertController addAction:cancelAction];

            [alertController addAction:otherAction];

            

        }else

        {

            UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:nilmessage:[NSStringstringWithFormat:@"新版本 %@已发布!",appInfo1]delegate:selfcancelButtonTitle:@"知道了"otherButtonTitles:@"前往更新",nil];

            alert.delegate =self;

            [alert show];

        }

    }

}

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

    if (buttonIndex ==1 ) {

        [selfUpdata];

    }

}

-(void)Updata{

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

    [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:url]];

}

0 0
原创粉丝点击