IOS Detect version Update (02)

Source: Internet
Author: User

IOS Detect version updates

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 < param name= "wmode" value= "Transparent" >
    1. Nsdictionary *infodic = [[NSBundle mainbundle] infodictionary];
    2. Cfshow (Infodic);
    3. 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];

The code is as follows:

-(void) oncheckversion

{

Nsdictionary *infodic = [[NSBundle mainbundle] infodictionary];

Cfshow ((__bridge cftyperef) (infodic));

NSString *currentversion = [infodic objectforkey:@ "cfbundleversion"];

NSString *url = @ "http://itunes.apple.com/lookup?id= the ID of your application";

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:[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 = [[Uialertview alloc] initwithtitle:@ "Update" message:@ "have a new version update, do you want to go to the update?" "Delegate:self cancelbuttontitle:@" Off "otherbuttontitles:@" Update ", nil];

Alert.tag = 10000;

[Alert show];

}

Else

{

Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Update" message:@ "This version is the latest version" Delegate:self cancelbuttontitle:@ "OK "Otherbuttontitles:nil, nil";

Alert.tag = 10001;

[Alert show];

}

}

}

-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) Buttonindex

{

if (alertview.tag==10000) {

if (buttonindex==1) {

Nsurl *url = [Nsurl urlwithstring:@ "https://itunes.apple.com"];

[[UIApplication Sharedapplication]openurl:url];

}

}

}

IOS Detect version Update (02)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.