首页 > 代码库 > ios 检查版本更新
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]]; }}
ios 检查版本更新
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。