IOS detection version update and ios detection version

Source: Internet
Author: User

IOS detection version update and ios detection version

If we want to detect updates to the app version, we must obtain the version information of the currently running app version and the latest version released on the appstore.


The current running version can be obtained through the bundle version in the info. plist file:

[Cpp]View plaincopy
  1. NSDictionary * infoDic = [[NSBundle mainBundle] infoDictionary];
  2. CFShow (infoDic );
  3. NSString * appVersion = [infoDic objectForKey: @ "CFBundleVersion"];

In this way, the version of the currently running app is obtained.


You can use either of the following methods to obtain the latest version of the current app store,

1. Publish and store the latest version information of an app on a specific server. If necessary, request the latest version information from the server.


2. query the app store to obtain the author, connection, and version of the app. Official documents

Www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.htm


The procedure is as follows:
1. Send the request in POST mode:
Http://itunes.apple.com/search? Term = your application name & entity = software

The more accurate method is to search by app id:
Http://itunes.apple.com/lookup? Id = the ID of your application

# Define APP_URL http://itunes.apple.com/lookup? Id = the ID of your application

Your application ID 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 must be parsed. The raw 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 = version number;
WrapperType = software;
}
);
}

Then obtain the results array. The Code is as follows:

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 open it in the browser, the introduction page of your application in appstore will be displayed. 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 = Your Application ID ";

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: & urlResponseerror: & 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 is1_tostring: currentVersion]) {

// TrackViewURL = [releaseInfo objectForKey: @ "trackVireUrl"];

UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Update" message: @ "there is a new version update. Are you sure you want to update it? "Delegate: self cancelButtonTitle: @" Disable "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];

}

}

}

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.