博客> 关于APP更新功能的实现
关于APP更新功能的实现
2019-10-19 14:28 评论:1 阅读:1144 feng_zi
ios id 软件更新 Apple

一般我们做APP可能涉及到软件更新的功能实现,现在给大家谈一谈这个功能的实现: 直接上代码:

pragma mark - 版本检查更新

  • (void)onCheckVersion {

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

    //CFShow((__bridge CFTypeRef)(infoDic));

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

    NSString *URL = @"http://itunes.apple.com/cn/lookup?id=你的应用的Apple id";(这个需要注意)

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString:URL]];

    [request setHTTPMethod:@"POST"];

    NSHTTPURLResponse *urlResponse = nil;

    NSError *error = nil;

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

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:recervedData options:NSJSONReadingAllowFragments error:nil]; NSLog(@"%@" , dic);

    // NSString results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedData length]encoding:NSUTF8StringEncoding]; // // NSDictionary dic = [results JSONValue];

    NSArray *infoArray = [dic objectForKey:@"results"];

    if ([infoArray count]) {

    NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
    
    NSString *lastVersion = [releaseInfo objectForKey:@"version"];
    
    if ([lastVersion floatValue] > [currentVersion floatValue]) {
    
        //trackViewURL = [releaseInfo objectForKey:@trackVireUrl];
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新,是否前往更新?" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil];
    
        alert.tag = 10000;
    
        [alert show];
    }else
    
    {
    
        [SVProgressHUD showInfoWithStatus:@"当前已经是最新版本"];
    }

    }else { [SVProgressHUD showInfoWithStatus:@"当前已经是最新版本"]; } }

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

{

if (alertView.tag==10000) {

    if (buttonIndex==1) {

        NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com"];

        [[UIApplication sharedApplication]openURL:url];

    }

}

}

http://itunes.apple.com/cn/lookup?id=你的应用的Apple id 其实所有的坑点 都在这一句话中,如果是全球的可以去掉/cn 中国的要加上这个,还有这个id 是Apple id不是APP id这个要注意,花了两个小时解决了OK 就这样吧

收藏
0
sina weixin mail 回到顶部