Finding Out What Version Of Your iOS App Was Originally Installed : Part 2 – The Ugly Way

In part 1 I wrote up a method of finding out what version of your iOS app a user originally installed that works great, provided you can get a network connection and the App Store servers are up.

But if you can't rely on that being the case, and you don't want to wait for it to be true before you take whatever action you're basing on the original version number, you can get the receipt data locally and process it.

As noted in the Receipt Validation Programming Guide (links added) ...

[The app store receipt] is a PKCS #7 container, as defined by RFC 2315, with its payload encoded using ASN.1 (Abstract Syntax Notation One), as defined by ITU-T X.690. The payload is composed of a set of receipt attributes. Each receipt attribute contains a type, a version, and a value.

Well now, that makes it sound easy. And in fact processing ASN.1 DER isn't all that hard, relatively speaking. The proper way of going about it is explained in the docs. Add OpenSSL to your project, use asn1c to generate some parsing code, and off you go. But that would take, oh hours, and be too long for a blog post on a Thursday. Maybe I'll get to that in part 3.

Like the last post, the point of this one is less about demonstrating how to do it, and more about demonstrating that it can be done. So to that end, here's a very nasty bit of code for processing a subset of ASN.1 that relies heavily on some very ugly and probably quite unsafe simplifying assumptions about what's going to be in the input and what you're going to ask of it. Since it contains no error or bounds checking of any kind, it should be considered illustrative, not normative. In other words, don't ship this code. I'm sure you won't anyway, once you've seen it. Speaking of which ...

Flinging the bytes of Phoreal's sandbox receipt at that yields output very like the following.

Type : 19 Version : 1 Octets : 1.0 is the one we're after. That corresponds to the original_application_version key in the dict we got back from the previous, on line, version.

So there you go. The original app install version is right there in the receipt in the app bundle. Just don't get it out like this if you're planning to do anything important with it!

Codemonkey, iOS developer, neophyte entrepreneur, frequent hat wearer. Founder of Enigmatic Ape (@EnigmaticApe). Recidivist tea drinker. Sound tennis noob with NEVITC. Now available in Twitter, and ADN

Built With Bootstrap
Powered By Wordpress
Coded By Enigmatic Ape