iOS子线程操作检测版本更新,有新版本通知用户更新, CheckVersion

iOS子线程操作检测版本更新,有新版本通知用户更新 CheckVersion

一:如何使用:

#import "CheckVersion.h"

    //输入你的app在appStore的 id
    [CheckVersion check_APP_UPDATE_WITH_APPID:@"350962117"];

上述代码写完就可以了,当用户打开app检测到新版本时,为通知用户,更新,并显示最新版本的更新内容;

                   

二:CheckVersion 类

复制代码
//
//  CheckVersion.h
//  TopProgressView
//
//  Created by cocoajin on 14-1-20.
//  Copyright (c) 2014年 www.zhgu.net. All rights reserved.
//

#import <Foundation/Foundation.h>

extern NSString const *iTnuesApi;



@interface CheckVersion : NSObject

//+ (instancetype)check;

+ (NSString *)check_LocalApp_Version;

+ (void )check_APP_UPDATE_WITH_APPID:(NSString *)appid;

@end
复制代码
复制代码
//
//  CheckVersion.m
//  TopProgressView
//
//  Created by cocoajin on 14-1-20.
//  Copyright (c) 2014年 www.zhgu.net. All rights reserved.
//


#import "CheckVersion.h"

NSString const *iTnuesApi = @"http://itunes.apple.com/lookup";

#define kTestApp @"http://itunes.apple.com/lookup?id=350962117"  //新浪微博 app测试


@implementation CheckVersion

+ (instancetype)check
{
    static CheckVersion *check = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        check = [[CheckVersion alloc]init];
    });
    
    return check;
}

+ (NSString *)check_LocalApp_Version;
{
    NSString *localVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    
    return localVersion;
}

+ (void )check_APP_UPDATE_WITH_APPID:(NSString *)appid
{
    __block id JSON = nil;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSError *dataError = nil;
        NSString *appURLAPI = [NSString stringWithFormat:@"%@?id=%@",iTnuesApi,appid];
        NSData *appData = [NSData dataWithContentsOfURL:[NSURL URLWithString:appURLAPI] options:0 error:&dataError];
        if (dataError) {
            //NSLog(@"appStore app版本信息请求错误!请重新尝试");
            [self showAlertWithMessage:@"appStore app版本信息请求错误!请重新尝试"];
            return ;
        }
        JSON = [NSJSONSerialization JSONObjectWithData:appData options:0 error:nil];
        //NSLog(@"ddd : %@",JSON);
        
        if ([[JSON objectForKey:@"resultCount"] intValue] > 0) {
            NSString *remoteVersion = [[[JSON objectForKey:@"results"] objectAtIndex:0] objectForKey:@"version"];
            NSString *releaseNotes = [[[JSON objectForKey:@"results"] objectAtIndex:0] objectForKey:@"releaseNotes"];
            NSString *trackURL = [[[JSON objectForKey:@"results"] objectAtIndex:0] objectForKey:@"trackViewUrl"];
            [[NSUserDefaults standardUserDefaults] setObject:trackURL forKey:@"KK_THE_APP_UPDATE_URL"];
            //NSLog(@"%@ %@ %@",remoteVersion,releaseNotes,trackURL);
            
            NSString *localVersion = [self check_LocalApp_Version];
            
            if ([remoteVersion floatValue] > [localVersion floatValue]) {
                [[CheckVersion check] newVersionUpdate:remoteVersion notes:releaseNotes];
            }
            else
            {
                return;
            }
            
        }
        else
        {
            //NSLog(@"appStore 无app信息,请检查您的 app id");
            [self showAlertWithMessage:@"appStore 无此app信息,请检查您的 app id"];
            return ;
        }
        
        
    });
    
}



+ (void)showAlertWithMessage:(NSString *)messages
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"版本更新提示" message:messages delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
#if !__has_feature(objc_arc)
        [alert release];
#endif
    });
    
}

- (void)newVersionUpdate:(NSString *)version notes:(NSString *)releaseNotes
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"新版本 %@",version] message:releaseNotes delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil];
        [alert show];
        
#if !__has_feature(objc_arc)
        [alert release];
#endif
    });
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
        //NSString *apiUrl = @"https://itunes.apple.com/us/app/wei-bo/id350962117?mt=8&uo=4";
        //apiUrl = @"itms-apps://itunes.apple.com/cn/app/wei-bo/id350962117?mt=8";
        NSString *theAppURL = [[NSUserDefaults standardUserDefaults] objectForKey:@"KK_THE_APP_UPDATE_URL"];
        NSURL *appStoreURL = [NSURL URLWithString:theAppURL];
        [[UIApplication sharedApplication] openURL:appStoreURL];
    }
}
@end
复制代码
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值