App version auto update-ios Sneak Cleaning series

Source: Internet
Author: User

Principle Ah, in fact, is very simple, is to compare the current version of the app and the server version number is the same, not the same as prompted to update.

The implementation of the automatic prompt update for iOS programs is broadly divided into two types:
First, your server provides an interface that informs you about the current version of the app, whether it needs to be updated, and the updated address.
The second is to use the relevant API provided by Apple's AppStore to update the query.

The code is as follows:

#define APPURL @ "http://itunes.apple.com/lookup?id= you apply ID on itunes"//Check for updates-(void) oncheckversion{//1. Get the current version number NSD    Ictionary *infodic = [[NSBundle mainbundle] infodictionary];    Cfshow ((__bridge cftyperef) (infodic));        NSString *currentversion = [infodic objectforkey:@ "cfbundleversion"];    NSString *url = AppUrl;    2. Send a request to Apple API to get app info 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]; Nsdictionary *appinofdic=[nsjsonserialization Jsonobjectwithdata:recerveddata options:NSJSONReadingMutableLeaves    error:&error];    if (Error) {NSLog (@ "error:%@", [error description]);    } nsarray *resultarray=[appinofdic objectforkey:@ "Results"]; if (![ Resultarray Count]) {NSLog (@ "Error:results==nil"); } if ([Resultarray count]>0) {nsdictionary *infodics=[resultarray objectatindex:0];
Get AppStore Latest Version number NSString *latestversion=[infodics objectforkey:@ "version"];
     Get the address of the application   _trackviewurl=[infodics objectforkey:@ "Trackviewurl"]; NSString *trackname=[infodics objectforkey:@ "TrackName"]; Double Doublecurrentversion=[currentversion Doublevalue]; Double Doubleupdateversion=[latestversion Doublevalue]; if (doublecurrentversion<doubleupdateversion) {nsstring *titlestr=[nsstring stringwithformat:@ "Check for updates:%@", tra Ckname]; NSString *messagestr=[nsstring stringwithformat:@ "discover new version (%@), upgrade?", Latestversion]; Uialertview *alertview=[[uialertview alloc]initwithtitle:titlestr Message:messagestr Delegate:self cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "Upgrade", nil]; alertview.tag=1; [Alertview show]; }else{nsstring *titlestr=[nsstring stringwithformat:@ "Check for updates:%@", tracknAME]; Uialertview *alertviews=[[uialertview alloc]initwithtitle:titlestr message:@ "no new version" Delegate:self cancelbuttontitle:@ "OK" Otherbuttontitles:nil, nil]; alertviews.tag=2; [Alertviews show]; }}else {Uialertview *alertviews=[[uialertview alloc]initwithtitle:@ "Notification" message:@ "no new version" Delegate:self cancelbuttontitle:@ "OK" Otherbuttontitles:nil, nil]; alertviews.tag=3; [Alertviews show]; }}-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) buttonindex{if (buttonindex==1) { [Saveuserinfo Saveuserinfo:nil]; [Self.tableview Reloaddata]; } if (ALERTVIEw.tag==1) {if (buttonindex==1) {////If there is a new version, then jump AppStore [[UIApplication sharedapplication ]openurl:[nsurl Urlwithstring:_trackviewurl]]; } } }

Attention:

1. App ID  is available here (where the app is listed)

2. Easy to confuse

The "bundle version" field in the Info.plist file is for debugging purposes, that is, the internal debug version number , which is not displayed on the AppStore, and version corresponds to the "bundle" in the Info.plist file. Versions string, short "field, this is the real version number , shown on the AppStore.



App version auto update-ios Sneak Cleaning series

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.