ios 检查版本更新

//
//  AppCheckVersion.h
//  MobileBusiness
//
//  Created by kevin on 14-7-4.
//
//

#import <Foundation/Foundation.h>
#import "Reachability.h"

@class AppCheckVersion;
@protocol AppCheckVersionDelegate <NSObject>
@optional
-(void)checkVersionResult:(AppCheckVersion*)checkVersion isUpdate:(BOOL)update resultData:(NSDictionary*)dictionary;
@end

@interface AppCheckVersion : NSObject<NSURLConnectionDelegate>
{
     Class originalClass;
    __unsafe_unretained id <AppCheckVersionDelegate> _delegate;
}
@property (nonatomic, retain) NSURLConnection *connection;
@property (nonatomic, retain) NSMutableData  *reciveData;
@property (nonatomic, retain) NSURLRequest  *currentRequest;
@property (nonatomic, assign) id<AppCheckVersionDelegate> delegate;

-(void)checkVersion;
-(BOOL)isDelegateRelease;
+(void)onCheckVersion:(id<AppCheckVersionDelegate>)target;
@end

.m文件

//
//  AppCheckVersion.m
//  MobileBusiness
//
//  Created by kevin on 14-7-4.
//
//

#import "AppCheckVersion.h"

#define App_ID  @"886620861" //284417350 应用程序APP ID
#define APP_URL @"http://itunes.apple.com/lookup?id=886620861"

Class object_getClass(id object);

@implementation AppCheckVersion
@synthesize connection;
@synthesize reciveData;
@synthesize currentRequest;
@synthesize delegate;

+(void)onCheckVersion:(id)target{
    AppCheckVersion *app = [[AppCheckVersion alloc] init];
    app.delegate = target;
    [app checkVersion];
}

-(BOOL)isDelegateRelease{
    if(originalClass == object_getClass(_delegate)){
        return YES;
    }
    return NO;
}
-(void)cancelConnection{
    if(self.connection){
        [self.connection cancel];
    }
    self.connection = nil;
    self.currentRequest = nil;
    self.reciveData = nil;
}
-(void)checkVersion{
    originalClass = object_getClass(_delegate);
    [self cancelConnection];
    NetworkStatus networkStatus = [[Reachability reachabilityForInternetConnection] currentReachabilityStatus];
    if(networkStatus == NotReachable)
    {
        //无网络
        return;
    }
    NSURLRequest * requeset = [NSURLRequest requestWithURL:[NSURL URLWithString:APP_URL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
    self.currentRequest = requeset;
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:self.currentRequest delegate:self];
    self.connection = conn;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.reciveData = [NSMutableData data];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.reciveData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError *error = nil;
    NSDictionary *appDictionary = [NSJSONSerialization JSONObjectWithData:self.reciveData options:NSJSONReadingMutableContainers error:&error];
    if(error == nil){
//        NSLog(@"dataObject ===  %@",appDictionary);
        //当前应用版本
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];
        int resultCount = [[appDictionary objectForKey:@"resultCount"] intValue];
        if (resultCount)
        {
            //有版本信息
            NSDictionary *resultDictionary = [[appDictionary objectForKey:@"results"] objectAtIndex:0];
            if(resultDictionary)
            {
                //获取appstore最新的版本号
                NSString *version = [resultDictionary objectForKey:@"version"];
                //获取应用程序的地址
//                NSString *appURL = [resultDictionary objectForKey:@"trackViewUrl"];
                //获取更新的内容
//                NSString *appUpdateContent = [resultDictionary objectForKey:@"description"];
                if([self isDelegateRelease])
                {
                    if(self.delegate && [self.delegate respondsToSelector:@selector(checkVersionResult:isUpdate:resultData:)]){
                        if(![currentVersion isEqualToString:version]){
                            //版本不同有更新
                            [self.delegate checkVersionResult:self isUpdate:YES resultData:resultDictionary];
                        }else{
                            //已经是最新版本
                            [self.delegate checkVersionResult:self isUpdate:NO resultData:nil];
                        }
                    }
                }

            }
        }else{
            if([self isDelegateRelease])
            {
                 if(self.delegate && [self.delegate respondsToSelector:@selector(checkVersionResult:isUpdate:resultData:)]){
                     //已经是最新版本
                     [self.delegate checkVersionResult:self isUpdate:NO resultData:nil];
                 }
            }
        }
    }

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    //失败
    [self cancelConnection];
}
@end

代理对象,一般就是appdelegate

#pragma mark - 检查版本更新
-(void)checkVersionResult:(AppCheckVersion *)checkVersion isUpdate:(BOOL)update resultData:(NSDictionary *)dictionary{
    UIAlertView *alterView = nil;
    if(update){
        //新颁布
        //获取应用程序的地址
        appURL = [dictionary objectForKey:@"trackViewUrl"];
        alterView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"软件已有新版本了,请下载最新版本" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alterView show];
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex==1){
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appURL]];
    }
}
时间: 2024-10-24 01:34:39

ios 检查版本更新的相关文章

iOS检查版本更新

项目原来的检查代码适用于1.2 1.3格式.一般正常的项目格式应该是1.2.2 ,如此大版本.小版本格式. 贴下代码 -(void)onCheckVersion { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^{ NSDictionary *infoDic = [[NSBundle mainBundle] infoDic

iOS 检测版本更新(02)

iOS 检测版本更新 如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息. 当前运行版本信息可以通过info.plist文件中的bundle version中获取: [cpp] view plaincopy NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDic); NSString *appVersion = [infoDic

iOS 检测版本更新

iOS 检测版本更新 分类: IOS_XCODE 2013-07-19 16:52 18252人阅读 评论(5) 收藏 举报 如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息. 当前运行版本信息可以通过info.plist文件中的bundle version中获取: [cpp] view plaincopy NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionar

App上架版本号配置的注意事项:CFBundleVersion与CFBundleShortVersionString设置,以及内部检查版本更新的代码

一.CFBundleVersion与CFBundleShortVersionString设置 CFBundleVersion:标识内部版本号,如果发包错误时可以将错误的撤回及时更改内部版本号即可. CFBundleShortVersionString:代表应用的发布版本号,该版本的版本号是三个时期分隔的整数组成的字符串.第一个整数代表重大修改的版本,如实现新的功能或重大变化的修订.第二个整数表示的修订,实现较突出的特点.第三个整数代表维护版本. 如上图:Version对应的就是CFBundleS

检查版本更新的https://itunes.apple.com/lookup?id=XXX 返回的没有数据

Looks like your app is ONLY available in Thailand storefront - so the lookup should include country code in the base lookup URL: http://itunes.apple.com/th/lookup?bundleId=com.writingfuture.where2 The alternative to using bundleId is simply looking u

SmsManager - Windows Phone手机PC端短信管理工具检查版本更新地址

SmsManager - Windows Phone手机PC端短信管理工具检查版本更新时将抓取此页面中两个特殊字符串之间的文字内容(共3处),并替换\n为换行,html转义字符为原字符. wpsmsmanager0-start[1.2]end-wpsmsmanager0 wpsmsmanager1-start[新版本V1.2已发布(2015/4/10)\n更新说明:\n可导出短信至Android手机\n前往查看?]end-wpsmsmanager1 wpsmsmanager2-start[htt

iOS 检查版本号的代码

- (void)checkNewVersion{ if ([@"appStore" isEqualToString:CHANNEL]) { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSString *url = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",

iOS Appstore 版本更新

1,版本更新 通过比较构建号/版本号 检查更新 ///  构建号 50 // NSString * currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"]; /// 版本号 2.2.0 //CFBundleShortVersionString NSString * currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBun

iOS 检测版本更新处理

如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息. 当前运行版本信息可以通过info.plist文件中的bundle version中获取: NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"]; 这样就获取