Transfer from http://blog.csdn.net/xiaoxuan415315/article/details/9383453
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:
[CPP]View Plaincopy
- NSDictionary *infodic = [[nsbundle mainbundle] infodictionary];
- ];
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];
The code is as follows:
-(void) oncheckversion
{
nsdictionary *infodic = [[nsbundlemainbundle] infodictionary];
Cfshow ((__bridge cftyperef) (infodic));
nsstring *currentversion = [infodic objectforkey:@ "Cfbundleversion"];
nsstring *url =@ "http://itunes.apple.com/lookup?id= your app's id";
Nsmutableurlrequest *request = [[Nsmutableurlrequestalloc] init];
[Requestseturl:[nsurlurlwithstring:url];
[RequestSethttpmethod:@ "POST"];
nshttpurlresponse *urlresponse = nil;
nserror *error = nil;
NSData *recerveddata = [nsurlconnectionsendsynchronousrequest:request returningresponse:& Urlresponse error:&error];
nsstring *results = [[nsstringalloc] initwithbytes: [recerveddatabytes] length: [ Recerveddatalength] 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 = [[uialertviewalloc] Initwithtitle:@ " update Span style= "color: #d12f1b;" > "message:@" Is there a new version update and are you going to update? "Delegate:self cancelbuttontitle:@ " close " otherbuttontitles:@ " Update nil";
Alert. tag =10000;
[Alertshow];
}
Else
{
uialertview *alert = [[uialertviewalloc] initwithtitle:@ " update message:@" This version is the latest version "Delegate:self cancelbuttontitle:@ " OK "Otherbuttontitles:nil, nil];
Alert. tag =10001;
[Alertshow];
}
}
}
-(void) Alertview: (uialertview *) Alertview clickedbuttonatindex: (nsinteger) buttonindex
{
if (Alertview. tag==10000) {
if (buttonindex==1) {
nsurl *url = [nsurlurlwithstring: @ "https://itunes.apple.com"];
[[uiapplicationsharedapplication]openurl: url];
}
}
}
IOS Detect version Updates