If we want to detect an update to the app version, we must obtain the version information of the currently running app version and the latest version posted on AppStore.
The current run version information can be obtained from the bundle version in the Info.plist file:
Nsdictionary *infodic = [[NSBundle mainbundle] infodictionary];
Cfshow (Infodic);
NSString *appversion = [infodic objectforkey:@ "cfbundleversion"];
This gets the version of the app that is currently running.
To get the latest version on the current app store, there are two ways to
First, on a specific server, publish and store the latest version of the app's information, when needed to request queries to the server.
Second, from the App Store query, you can get to the app's author, connection, version and so on. Official Related documents
Www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.htm
The steps are as follows:
1, send the request by POST:
Http://itunes.apple.com/search?term= your application name &entity=software
A more accurate approach is to look for the app's ID:
Http://itunes.apple.com/lookup?id= the ID of your app
#define APP_URL http://itunes.apple.com/lookup?id= The ID of your app
The ID of your app is the Apple ID in itunes Connect
2, parse the required data from the obtained response data. Because the information obtained from the AppStore query is in JSON format, it needs to be parsed. The original data obtained after parsing is as follows:
{
Resultcount = 1;
Results = (
{
ArtistID = Developer ID;
artistname = developer Name;
Price = 0;
isgamecenterenabled = 0;
kind = software;
LANGUAGECODESISO2A = (
En
);
Trackcensoredname = review name;
Trackcontentrating = rating;
TrackID = Application ID;
TrackName = Application Name ";
Trackviewurl = Application Introduction URL;
Userratingcount = user Rating;
Userratingcountforcurrentversion = 1;
Version = revision number;
Wrappertype = software;
}
);
}
The results array can then be obtained from it, as shown in the following code:
Nsdictionary *jsondata = [Datapayload jsonvalue];
Nsarray *infoarray = [Jsondata objectforkey:@ "Results"];
Nsdictionary *releaseinfo = [Infoarray objectatindex:0];
NSString *latestversion = [releaseinfo objectforkey:@ "version"];
NSString *trackviewurl = [Releaseinfo objectforkey:@ "Trackviewurl"];
If you copy the actual address of Trackviewurl and then open it in the browser, it will open the introduction page of your application in AppStore. Of course we can also call safari in the code to open it.
UIApplication *application = [UIApplication sharedapplication];
[Application Openurl:[nsurl Urlwithstring:trackviewurl];
-(void) Oncheckversion: (NSString *) CurrentVersion
{
NSString *url = App_url;
Nsmutableurlrequest *request = [[Nsmutableurlrequest alloc] init];
[Request Seturl:[nsurl Urlwithstring:url];
[Request sethttpmethod:@ "POST"];
Nshttpurlresponse *urlresponse = nil;
Nserror *error = nil;
NSData *recerveddata = [nsurlconnection sendsynchronousrequest:request returningresponse:&urlresponse Error: &error];
NSString *results = [[nsstring alloc] initwithbytes:[recerveddata bytes] length:[recerveddata length] Encoding: Nsutf8stringencoding];
Nsdictionary *dic = [Results jsonvalue];
Nsarray *infoarray = [dic objectforkey:@ "Results"];
if ([Infoarray count]) {
Nsdictionary *releaseinfo = [Infoarray objectatindex:0];
NSString *lastversion = [releaseinfo objectforkey:@ "version"];
if (![ Lastversion Isequaltostring:currentversion]) {
Trackviewurl = [Releaseinfo objectforkey:@ "Trackvireurl"];
Uialertview *alert = [[[Uialertview alloc] initwithtitle:@ "Update" message:@ "have a new version updated, do you want to go to the update?" "Delegate:self cancelbuttontitle:@" Off "otherbuttontitles:@" Update ", Nil] autorelease];
[Alert show];
}
}
}