////AppCheckVersion.h//mobilebusiness////Created by Kevin on 14-7-4.////#import<Foundation/Foundation.h>#import "Reachability.h"@classappcheckversion;@protocolAppcheckversiondelegate <NSObject>@optional-(void) Checkversionresult: (appcheckversion*) checkversion isupdate: (BOOL) Update Resultdata: (nsdictionary*) dictionary;@end@interfaceAppcheckversion:nsobject<nsurlconnectiondelegate>{Class originalclass; __unsafe_unretainedID<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 file
////appcheckversion.m//mobilebusiness////Created by Kevin on 14-7-4.////#import "AppCheckVersion.h"#defineapp_id @ "886620861"//284417350 app app ID#defineApp_url @ "http://itunes.apple.com/lookup?id=886620861 "Class Object_getclass (ID Object);@implementationappcheckversion@synthesizeconnection;@synthesizeRecivedata;@synthesizecurrentrequest;@synthesize Delegate;+(void) Oncheckversion: (ID) target{appcheckversion*app =[[Appcheckversion alloc] init]; App.Delegate=Target; [App Checkversion];}-(BOOL) isdelegaterelease{if(Originalclass = =Object_getclass (_delegate)) { returnYES; } returnNO;}-(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) { //No network return; } nsurlrequest* Requeset = [nsurlrequest requestwithurl:[nsurl Urlwithstring:app_url] CachePolicy: Nsurlrequestuseprotocolcachepolicy timeOutInterval: -]; Self.currentrequest=Requeset; Nsurlconnection*conn = [[Nsurlconnection alloc] InitWithRequest:self.currentRequestDelegate: 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); //Current app versionNsdictionary *infodic =[[NSBundle Mainbundle] infodictionary]; NSString*currentversion = [Infodic objectforkey:@"cfbundleversion"]; intResultcount = [[Appdictionary objectforkey:@"Resultcount"] intvalue]; if(resultcount) {//Version information availableNsdictionary *resultdictionary = [[Appdictionary objectforkey:@"Results"] Objectatindex:0]; if(resultdictionary) {//get the latest version number for AppStoreNSString *version = [Resultdictionary objectforkey:@"version"]; //get the address of the application//nsstring *appurl = [resultdictionary objectforkey:@ "Trackviewurl"]; //get the updated content//nsstring *appupdatecontent = [resultdictionary objectforkey:@ "description"]; if([self isdelegaterelease]) {if(self.)Delegate&& [Self.Delegaterespondstoselector: @selector (checkVersionResult:isUpdate:resultData:)]) { if(![CurrentVersion Isequaltostring:version]) { //different versions have updates[Self.Delegatecheckversionresult:self Isupdate:yes Resultdata:resultdictionary]; }Else{ //is already the latest version[Self.Delegatecheckversionresult:self Isupdate:no Resultdata:nil]; } } } } }Else{ if([self isdelegaterelease]) {if(self.)Delegate&& [Self.Delegaterespondstoselector: @selector (checkVersionResult:isUpdate:resultData:)]) { //is already the latest version[Self.Delegatecheckversionresult:self Isupdate:no Resultdata:nil]; } } } } }-(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{//failed[self cancelconnection];}@end
Agent object, generally is appdelegate
#pragmaMark-Check for version updates-(void) Checkversionresult: (appcheckversion *) checkversion isupdate: (BOOL) Update Resultdata: (Nsdictionary *) dictionary{Uialertview*alterview =Nil; if(update) {//New Promulgation//get the address of the applicationAppUrl = [Dictionary objectforkey:@"Trackviewurl"]; Alterview= [[Uialertview alloc] Initwithtitle:@"Warm Tips"Message@"The software already has a new version, please download the latest version" Delegate: Self Cancelbuttontitle:@"Cancel"Otherbuttontitles:@"Determine", nil]; [Alterview show]; }}-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (nsinteger) buttonindex{if(buttonindex==1{[[uiapplication sharedapplication] Openurl:[nsurl Urlwithstring:appurl]]; }}
iOS Check for version updates