iOS uses version number comparison between local and App Store to realize version update

Keywords: Mobile SSL JSON Javascript

The comparison version number is generally used to realize the function of version update. The pop-up window prompts the user to update or directly jump to the App Store.

Get version information of APP in App Store

Here we use iTunes's lookup interface, provided that you have published it to the App Store.

Get the id of the app in the App Store

  1. App Store searches for < software name >, for example: wechat. After entering the details page, click the share button and click the copy link

  1. Long press the downloaded software, click Share App, click Copy link

Find idhttps://apps.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124 in the App Store

Get the version number of the app in the App Store

GET request using AFNetworking, as follows

+ (void)getAppStoreVersionInfoWithResponseBlock:(void (^)(NSDictionary *, NSError *))responseBlock {
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    [sessionManager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil]];
    
    [sessionManager GET:@"https://itunes.apple.com/cn/lookup?id=414478124" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *dataArray = responseObject[@"results"];
        if (dataArray.count != 0) {// First determine whether the returned data is empty
            NSDictionary *dict = dataArray.firstObject;
            responseBlock(dict, nil);
        } else {
            NSError *emptyError = [[NSError alloc] initWithDomain:NSStringFromClass(self) code:NetWorkResponseCodeAppearError userInfo:@{NSLocalizedDescriptionKey:@"Content is empty"}];
            responseBlock(nil, emptyError);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        responseBlock(nil, error);
    }];
    
}

json after success

{
	"resultCount": 1,
	"results": [{
		"isGameCenterEnabled": false,
		"artworkUrl60": "https://is1-ssl.mzstatic.com/image/thumb/Purple113/v4/0f/bc/3c/0fbc3c2a-f664-c4e3-bda2-970146031f0e/source/60x60bb.jpg",
		"artworkUrl512": "https://is1-ssl.mzstatic.com/image/thumb/Purple113/v4/0f/bc/3c/0fbc3c2a-f664-c4e3-bda2-970146031f0e/source/512x512bb.jpg",
		"artworkUrl100": "https://is1-ssl.mzstatic.com/image/thumb/Purple113/v4/0f/bc/3c/0fbc3c2a-f664-c4e3-bda2-970146031f0e/source/100x100bb.jpg",
		"artistViewUrl": "https://apps.apple.com/cn/developer/wechat/id614694882?uo=4",
		"screenshotUrls": ["https://is4-ssl.mzstatic.com/image/thumb/Purple123/v4/98/9e/61/989e614c-0c12-298d-9960-3aa178a88649/pr_source.png/392x696bb.png", "https://is1-ssl.mzstatic.com/image/thumb/Purple113/v4/83/3c/d0/833cd07d-5b02-8ac1-b59a-fe2f3b17d7f4/pr_source.png/392x696bb.png", "https://is2-ssl.mzstatic.com/image/thumb/Purple113/v4/37/7b/7f/377b7f92-2d20-5fc2-d662-63da28155353/pr_source.png/392x696bb.png", "https://is5-ssl.mzstatic.com/image/thumb/Purple123/v4/35/ac/e2/35ace258-55be-2699-c9be-f8a9532e2aed/pr_source.png/392x696bb.png", "https://is2-ssl.mzstatic.com/image/thumb/Purple123/v4/ac/c1/54/acc15416-9cbf-092d-7342-699afa8874b4/pr_source.png/392x696bb.png"],
		"ipadScreenshotUrls": ["https://is4-ssl.mzstatic.com/image/thumb/Purple113/v4/18/8d/b6/188db60e-7d8c-fad3-1a29-0902284945f7/mzl.imkvrcco.jpg/576x768bb.jpg", "https://is1-ssl.mzstatic.com/image/thumb/Purple113/v4/1e/b6/33/1eb633ef-7b94-2eec-d135-ae9d38b29eb6/mzl.bwmfmxug.jpg/576x768bb.jpg", "https://is3-ssl.mzstatic.com/image/thumb/Purple123/v4/4e/45/89/4e458955-91de-a903-0250-9e56537b4d9d/mzl.gxopfyrb.jpg/576x768bb.jpg"],
		"appletvScreenshotUrls": [],
		"advisories": ["frequently/A strong adult/The theme of sexual suggestion"],
		"supportedDevices": ["Watch4-Watch4", "iPhone5s-iPhone5s", "iPadAir-iPadAir", "iPadAirCellular-iPadAirCellular", "iPadMiniRetina-iPadMiniRetina", "iPadMiniRetinaCellular-iPadMiniRetinaCellular", "iPhone6-iPhone6", "iPhone6Plus-iPhone6Plus", "iPadAir2-iPadAir2", "iPadAir2Cellular-iPadAir2Cellular", "iPadMini3-iPadMini3", "iPadMini3Cellular-iPadMini3Cellular", "iPodTouchSixthGen-iPodTouchSixthGen", "iPhone6s-iPhone6s", "iPhone6sPlus-iPhone6sPlus", "iPadMini4-iPadMini4", "iPadMini4Cellular-iPadMini4Cellular", "iPadPro-iPadPro", "iPadProCellular-iPadProCellular", "iPadPro97-iPadPro97", "iPadPro97Cellular-iPadPro97Cellular", "iPhoneSE-iPhoneSE", "iPhone7-iPhone7", "iPhone7Plus-iPhone7Plus", "iPad611-iPad611", "iPad612-iPad612", "iPad71-iPad71", "iPad72-iPad72", "iPad73-iPad73", "iPad74-iPad74", "iPhone8-iPhone8", "iPhone8Plus-iPhone8Plus", "iPhoneX-iPhoneX", "iPad75-iPad75", "iPad76-iPad76", "iPhoneXS-iPhoneXS", "iPhoneXSMax-iPhoneXSMax", "iPhoneXR-iPhoneXR", "iPad812-iPad812", "iPad834-iPad834", "iPad856-iPad856", "iPad878-iPad878", "iPadMini5-iPadMini5", "iPadMini5Cellular-iPadMini5Cellular", "iPadAir3-iPadAir3", "iPadAir3Cellular-iPadAir3Cellular", "iPodTouchSeventhGen-iPodTouchSeventhGen", "iPhone11-iPhone11", "iPhone11Pro-iPhone11Pro", "iPadSeventhGen-iPadSeventhGen", "iPadSeventhGenCellular-iPadSeventhGenCellular", "iPhone11ProMax-iPhone11ProMax"],
		"kind": "software",
		"features": ["iosUniversal"],
		"trackViewUrl": "https://apps.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124?uo=4",
		"trackCensoredName": "WeChat",
		"languageCodesISO2A": ["AR", "ZH", "EN", "FR", "DE", "ID", "IT", "JA", "KO", "MS", "PT", "RU", "ZH", "ES", "TH", "ZH", "TR", "VI"],
		"fileSizeBytes": "273698816",
		"sellerUrl": "http://weixin.qq.com",
		"contentAdvisoryRating": "17+",
		"averageUserRatingForCurrentVersion": 4.0,
		"userRatingCountForCurrentVersion": 74205,
		"trackContentRating": "17+",
		"currentVersionReleaseDate": "2019-09-25T03:13:08Z",
		"releaseNotes": "This update:\n- Some known problems have been solved.\n\n Last update:\n- You can set the favorite notes, file preview and other pages as floating windows.",
		"isVppDeviceBasedLicensingEnabled": true,
		"primaryGenreName": "Social Networking",
		"genreIds": ["6005", "6007"],
		"primaryGenreId": 6005,
		"sellerName": "Tencent Technology (Shenzhen) Company Limited",
		"releaseDate": "2011-01-21T01:32:15Z",
		"trackId": 414478124,
		"trackName": "WeChat",
		"formattedPrice": "Free Admission",
		"minimumOsVersion": "9.0",
		"currency": "CNY",
		"version": "7.0.8",
		"wrapperType": "software",
		"artistId": 614694882,
		"artistName": "WeChat",
		"genres": ["Social contact", "efficiency"],
		"price": 0.00,
		"description": "Wechat is a full range of mobile communication applications to help you easily connect with friends around the world. Wechat can group chat, video chat, play games with friends, and share their own\n Living in a circle of friends makes you feel a new mobile lifestyle.\n\n  Why use wechat:\n  • Multimedia message: support sending video, picture, text and voice messages.\n  • Group chat and call: set up a group chat of up to 500 people and a real-time video chat of up to 9 people.\n  • Voice and video chat: provide high quality calls around the world.\n  • Expression store: a large number of dynamic expressions, including popular cartoon characters and movies, make chatting more vivid and interesting.\n  • Circle of friends: share every wonderful moment with your friends and record your life.\n  • Privacy protection: strictly protect the privacy of users, which is the only one through TRUSTe Certified real-time communication application.\n  • Meet new friends: get to know new friends through "radar plus friends", "people nearby" and "shake".\n  • Real time location sharing: share geographical location with friends without telling each other through language.\n  • Multilingual: support more than 20 language interfaces, and support message translation in multiple languages.\n  • Wechat campaign: support access Apple Watch And health app Data can be compared with friends on the step ranking. If you need to use it, you can set the-currency-Enabled in accessibility. \n  • More functions: Support cross platform, chat room wallpaper customization, message reminder, custom and public number service.",
		"bundleId": "com.tencent.xin",
		"averageUserRating": 4.5,
		"userRatingCount": 5041979
	}]
}

You can use Postman to call first, and then write the data model according to your own needs. After obtaining the result, you can directly retrieve the version number "version" in the App Store: "7.0.8".

Comparison between local version number and App Store version number

Take wechat as an example. Clicking the version update button of any version (lower than, higher than or equal to the App Store version) will jump to the App Store. My rule is to make a pop-up window of version information.

Get local version number

  1. To obtain App information, you need to retrieve the information contained in Info.plist.
//A dictionary, constructed from the bundle's Info.plist file, that contains information about the receiver.
- (NSDictionary<NSString *, id> *)appInfoDictionary {
    return [[NSBundle mainBundle] infoDictionary];
}
  1. Take out version number
- (NSString *)appVersion {
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}

Contrast

Most version numbers are 1.0.0, so you need to take out each bit for comparison.

I used to use the for in loop. After referring to the + compareSystemVersion: toVersion: Method in QMUIKit, I think the while loop is more suitable. The code is as follows

+ (NSComparisonResult)compareSystemVersion:(NSString *)currentVersion toVersion:(NSString *)targetVersion {
    NSArray *currentVersionArr = [currentVersion componentsSeparatedByString:@"."];
    NSArray *targetVersionArr = [targetVersion componentsSeparatedByString:@"."];
    
    NSInteger pos = 0;
    
    while ([currentVersionArr count] > pos || [targetVersionArr count] > pos) {
        NSInteger v1 = [currentVersionArr count] > pos ? [[currentVersionArr objectAtIndex:pos] integerValue] : 0;
        NSInteger v2 = [targetVersionArr count] > pos ? [[targetVersionArr objectAtIndex:pos] integerValue] : 0;
        if (v1 < v2) {
            return NSOrderedAscending;
        }
        else if (v1 > v2) {
            return NSOrderedDescending;
        }
        pos++;
    }
    
    return NSOrderedSame;
}

Version update full code

- (void)p_versionUpdateButtonAction  {
	//Get local App version number
    NSString *appVersion = [VPUtilityTool sharedInstance].appVersion;
	
    [VPMineRequestHandler getAppStoreVersionInfoWithResponseBlock:^(NSDictionary * _Nonnull response, NSError * _Nonnull error) {
        VPLog(@"%@",response);
        if (error) {
		//Jump to the App Store details page
            [UIApplicationInstance vp_openUrlString:@"https://apps.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124"];
        } else {
            NSString *appStoreVersion = response[@"version"];
            NSComparisonResult comparisonResult = [QMUIHelper compareSystemVersion:appVersion toVersion:appStoreVersion];
			//Pop up only if the result is higher than the local version
            if (comparisonResult == NSOrderedAscending) {
		//Update pop-up
                [AppAlertViewController showWithTitle:@"Version update" content:[NSString stringWithFormat:@"Latest version found: V%@\n succeed in inviting sb. App Store Download updates!", appStoreVersion] cancelButtonTitle:@"cancel" confirmButtonTitle:@"To update" actionsHandlerBlock:^(BOOL isCancel) {
                    if (!isCancel) {
                        [UIApplicationInstance vp_openUrlString:@"https://apps.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124"];
                    }
                }];
            } else {
                [UIApplicationInstance vp_openUrlString:@"https://apps.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124"];
            }
        }
    }];
	
}

There are so many functions of the version update.

Posted by Shazbot! on Wed, 06 Nov 2019 03:10:04 -0800