#import "ViewController.h"//1 must first configure their own project in the store AppID, configure the best to run on the real machine to see the full effect Oh#defineStoreappid @ "1080182980"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //a code implementation detection update[self Hsupdateapp];}/** * Celestial Special Detection app update*/-(void) hsupdateapp{//2 Get the current project version number firstNsdictionary *infodic=[[NSBundle Mainbundle] infodictionary]; NSString*currentversion=infodic[@"cfbundleshortversionstring"]; //3 Getting the AppStore version number from the networkNserror *error; NSData*response = [Nsurlconnection sendsynchronousrequest:[nsurlrequest requestwithurl:[nsurl URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@", Storeappid]] Returningresponse:nil Error:nil]; if(Response = =Nil) {NSLog (@"you're not connected to the network."); return; } nsdictionary*appinfodic = [Nsjsonserialization jsonobjectwithdata:response options:nsjsonreadingmutableleaves error:&ERROR]; if(Error) {NSLog (@"hsupdateapperror:%@", error); return; } Nsarray*array = appinfodic[@"Results"]; Nsdictionary*dic = array[0]; NSString*appstoreversion = dic[@"version"]; //Print Version numberNSLog (@"Current version number:%@\n Store version number:%@", currentversion,appstoreversion); //4 The current version number is less than the store version number, update if([CurrentVersion Floatvalue] <[appstoreversion Floatvalue]) {Uialertview*alert = [[Uialertview alloc] Initwithtitle:@"version is updated"Message:[nsstring stringWithFormat:@"new version (%@) detected, updated?", Appstoreversion]Delegate: Self Cancelbuttontitle:@"Cancel"Otherbuttontitles:@"Update", nil]; [Alert show]; }Else{NSLog (@"The version number seems to be bigger than the store. No updates detected"); }}- (void) Alertview: (uialertview*) Alertview Clickedbuttonatindex: (nsinteger) buttonindex{//5 Implementation jumps to the store for updates if(buttonindex==1) { //6 Here to add the application in the App Store address, convenient for users to update, a way to achieve the following:Nsurl *url = [Nsurl urlwithstring:[nsstring stringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8", Storeappid]]; [[UIApplication sharedapplication] openurl:url]; }}@end
iOS Development app Update