检测版本更新

来源:互联网 发布:淘宝网首页不显示图片 编辑:程序博客网 时间:2024/06/14 00:18

@interfaceViewController ()<UIAlertViewDelegate>

{

   NSString *updateURL;

}

@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    [selfisNewestVersion];

    [selfperformSelector:@selector(donghua)withObject:selfafterDelay:0.1];

    

}

- (void)isNewestVersion

{

    

    updateURL =@"";

    

    [NSURLConnectionsendAsynchronousRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:@“”]]queue:[NSOperationQueuecurrentQueue]completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError) {

        

       if (data) {

            

           NSArray *result= [NSJSONSerializationJSONObjectWithData:data options:0 error:nil];

            

            //            NSLog(@"result = %@", result);  

           //对比版本

            //          NSString * version=result[@"version"]; 

    //对应 CFBundleVersion,对应Xcode项目配置"General"中的 Build

            //            NSString * versionShort=result[@"versionShort"]; 

    //对应 CFBundleShortVersionString,对应Xcode项目配置"General"中的 Version

            

           NSString *versionShort = [result[0]objectForKey:@"versionShort"];

            

            

            // NSString * localVersion=[[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"];

           NSString * localVersionShort=[[NSBundlemainBundle] infoDictionary][@"CFBundleShortVersionString"];

            

            

            //                NSString *url=result[@"update_url"]; //如果有更新需要用Safari打开的地址

            //                NSString *changelog=result[@"changelog"]; //如果有更新需要用Safari打开的地址

            

            

            

            //这里放对比版本的逻辑 每个 app 对版本更新的理解都不同

            //有的对比 version,有的对比 build[version isEqualToString:localVersion]

           if ([versionShort isEqualToString:localVersionShort]) {

               return ;

            }else {

                

                //                    去更新

                UIAlertView *alView = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"检测到新版本,去更新么?"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"更新",nil];

                alView.delegate =self;

                [alViewshow];

                

            }

            

        }

        

    }];

    

}

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

{

   if (buttonIndex == alertView.cancelButtonIndex) {

       return;

    }else {

        [selfgotoDownLoadNeWVersion];

    }

    

}

- (void)gotoDownLoadNeWVersion

{

   NSURL* url = [[NSURLalloc] initWithString:updateURL];

    [[ UIApplicationsharedApplication]openURL:url];

}


@end


0 0