优秀比不过别人,那么努力呢?

iOS 自动更新

今天要做版本的自动更新 ,我觉得其实没做这个功能的必要  但是毕竟老板是老大嘛 就敲了敲   这是看了一个大大写的  忘记谁了  抱歉哈

NSError *error;

    NSString *appid = @"appid";

    NSString *urlStr = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appid];

    NSURL *url = [NSURL URLWithString:urlStr];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    

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

    

    if (error) {

        NSLog(@"error---->%@",[error description]);

        return;

    }

        NSArray *resultsArray = [appIndoDic objectForKey:@"results"];

        if (![resultsArray count]) {

            NSLog(@"error--->resul == nil");

            return;

    }

 

//    商店的信息

    NSDictionary *infiDic = [resultsArray objectAtIndex:0];

    

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

    

//   上架的版本号

    NSString *latestVersion = [infiDic objectForKey:@"version"];

//    网址

    _trackViewUrl = [infiDic objectForKey:@"trackViewUrl"];

//    名字

    NSString *trackName = [infiDic objectForKey:@"trackName"];

    

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

//    当前版本号

    NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];

    

//    NSLog(@"%@---------------%@",currentVersion,latestVersion);

    

    double doublelatestVersion = [latestVersion doubleValue];

    double doublecurrentVersion = [currentVersion doubleValue];

    

    

    if (doublecurrentVersion < doublelatestVersion) {

        NSString *titleSte = [NSString stringWithFormat:@"检查更新:%@",trackName];

        NSString *messageStr = [NSString stringWithFormat:@"发现新版本(%@),是否升级",latestVersion];

        

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:titleSte message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil];

        alert.delegate = self;

        [alert show];

    }

    

 

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

{

    

    if (buttonIndex == 1) {

//跳转到苹果商店

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

    }

posted @ 2016-06-24 14:11  一只卑微的蚂蚁  阅读(131)  评论(0编辑  收藏  举报