应用在AppStore检测版本更新


// appstore的情况下,版本检查升级的处理



#define  kShiperAppID     @"AppStore中查看AppId" 


#define  kURLLookup   @"http://itunes.apple.com/cn/lookup?id=%@"



#pragma
mark - appStore检查更新 - (void)checkUpdate:(id)sender{ __weak typeof(self) wself = self; [MBProgressHUD showHUDAddedTo:self.window animated:YES]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(){ // NSString *url = [NSString stringWithFormat:kURLLookup,@"423084029"]; // if ([self.type isEqualToString:@"shipper"]) { NSString *url = [NSString stringWithFormat:kURLLookup,kShiperAppID]; // } NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:url]]; [request setHTTPMethod:@"GET"]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSDictionary *result = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil]; NSString *serverV = @""; NSString *serVInfo = @""; do { if (!result){ break; } id ary = result[@"results"]; if (!ary || NO == [ary isKindOfClass:[NSArray class]]) { break; } if ([(NSArray*)ary count] <= 0) { break; } NSDictionary* dict = ary[0]; if (!dict) { break; } serverV = [dict objectForKey:@"version"]; serVInfo = [dict objectForKey:@"releaseNotes"]; }while (NO); dispatch_async(dispatch_get_main_queue(), ^(){ [MBProgressHUD hideAllHUDsForView:wself.window animated:YES]; NSString* currver = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; if ([currver length] == 0) { currver = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey]; } if (serverV.length > 0 && NSOrderedDescending == [self verseionCompare:currver ver2compare:serverV]) { NSString* tipmsg = [NSString stringWithFormat:@"发现新版:%@ %@",serverV,serVInfo]; UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:tipmsg delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新",nil]; [alert show]; }else{//如果是最新版本则不提醒 // [self showMessageWith:@"您当前的版本已是最新版本"]; // NSString* tipmsg = [NSString stringWithFormat:@"发现新版:%@ %@",serverV,serVInfo]; // // UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil // message:tipmsg // delegate:self // cancelButtonTitle:@"取消" // otherButtonTitles:@"更新",nil]; // [alert show]; } }); }); }


-(NSComparisonResult)verseionCompare:(NSString*)curversion ver2compare:(NSString*)ver2compare{
    NSString* curV = [curversion stringByReplacingOccurrencesOfString:@"." withString:@""];
    NSString* V2cp = [ver2compare stringByReplacingOccurrencesOfString:@"." withString:@""];
    
    if ([curV integerValue] == [V2cp integerValue]) {
        return NSOrderedSame;
    }
    if ([curV integerValue] < [V2cp integerValue]) {
        return NSOrderedDescending;
    }
    if ([curV integerValue] > [V2cp integerValue]) {
        return NSOrderedAscending;
    }
    return NSOrderedSame;
}
#pragma mark - AlertView Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSLog(@"quxiao");
    }else{
        NSLog(@"gengxin");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/app/id%@",kShiperAppID]]]; } }
原文地址:https://www.cnblogs.com/h-tao/p/5045367.html