標籤:des style blog http io color ar os sp
//// 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 檢查版本更新