检测版本更新

来源:互联网 发布:雅思自学 知乎 编辑:程序博客网 时间:2024/06/16 11:33

- (BOOL)checkHaveNewVersion_Func

{

    [MBProgressHUDshowHUDAddedTo:self.viewanimated:YES];

    __blockBOOL bHaveNewVersionFlag = FALSE; // 有新版本标志

    

//    // 首先取得本地程序版本号

    NSDictionary *infoDic = [[NSBundlemainBundle] infoDictionary];

//这个地方 一定要注意,因为appstore里返回的版本信息最前面加了个V  所以这个地方必须也加个

    NSString *localVersion = [NSStringstringWithFormat:@"V%@",[infoDicobjectForKey:@"CFBundleShortVersionString"]];     

    

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

        

        // 发送检测版本数据到app store

        NSString *URL = [NSStringstringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APPID];


        NSMutableURLRequest *request = [[NSMutableURLRequestalloc] init];

        [request setURL:[NSURLURLWithString:URL]];

        [request setHTTPMethod:@"POST"];

        

        NSHTTPURLResponse *urlResponse =nil;

        __blockNSError *error = nil;

        NSData *recervedData = [NSURLConnectionsendSynchronousRequest:request returningResponse:&urlResponse error:&error];

        

        dispatch_async(dispatch_get_main_queue(), ^{

            //IOS5自带解析类NSJSONSerializationresponse中解析出数据放到字典中: json4种方式用法及比较http://blog.csdn.net/enuola/article/details/7903632

            NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:recervedData options:NSJSONReadingMutableLeaveserror:&error];

        

            // 解析数据

            NSArray *infoArray = [dicobjectForKey:@"results"];

            if (0 < [infoArraycount]) {

                NSDictionary *releaseInfo = [infoArrayobjectAtIndex:0];

                NSString *appStoreVersion = [releaseInfoobjectForKey:@"version"];

                // appstore版本比本地版本高,则可更新

                

                if (NSOrderedAscending == [localVersioncompare:appStoreVersion]) { // 有新版本

                    bHaveNewVersionFlag = YES;

                    

                    [MBProgressHUDhideHUDForView:self.viewanimated:YES];

                    UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:MY_Local_Str(@"温馨提示")message:MY_Local_Str(@"已有新版本,是否前去更新")delegate:selfcancelButtonTitle:MY_Local_Str(@"暂不更新")otherButtonTitles:MY_Local_Str(@"更新"),nil];

                    [alertView show];

                    

                    

                } else {// 无新版本

                    

                    bHaveNewVersionFlag = NO;

                    [MBProgressHUDhideHUDForView:self.viewanimated:YES];

                    [GlobalClasspromptWithString:@"当前程序已是最新版本"onView:self.view];

                    

                }

            } else {

                bHaveNewVersionFlag = NO;

                [MBProgressHUDhideHUDForView:self.viewanimated:YES];

                [GlobalClasspromptWithString:@"当前程序已是最新版本"onView:self.view];

            }

        });

    });

    

    return bHaveNewVersionFlag;

}

0 0
原创粉丝点击