Parse Apple StoreKit Receipts Locally in Swift with ReceiptParser
ReceiptParser is an open-source, Swift-only package for reading Apple purchase receipts on the device. It helps developers inspect the binary receipt returned for in-app purchases, including subscription and purchase history, without building an ASN.1 decoder from scratch.
Why parse a receipt locally?
A StoreKit receipt is a snapshot of a user's in-app purchase history. Apps have traditionally sent that receipt to Apple's VerifyReceipt endpoint so its contents can be checked and extracted. Sending it directly from a device is not recommended because the exchange can be exposed to a man-in-the-middle attack. Local validation approaches and server-side validation are alternatives depending on the trust required.
StoreKit 2 added newer device-side APIs, including AppTransaction and Transaction, for apps that can adopt them. For apps still on earlier approaches, or for iOS versions before 15.0, Foundation's Bundle.appStoreReceiptURL has long provided access to the receipt file. That file is not opaque: its binary contents can be inspected, but the encoding is ASN.1, which is difficult to parse manually.
Using the Swift package
ReceiptParser was originally used inside an iOS SDK to make receipt handling more efficient, support free-trial eligibility calculations, and power debugging features. From version 4.16 of that SDK it became a public API. It is also distributed as a small Swift Package, so a project can use the parser without taking on the full SDK.
In Xcode, add the package repository, then choose ReceiptParser rather than the full framework when prompted. Two entry points cover the common cases: protected module code0001 parses receipt data you already have, and protected module code0002 loads and parses the local receipt in one step. The result is an AppleReceipt object whose API exposes the parsed purchase information.
A practical example is checking for an active subscription. Protected module code0003 shows how to classify a purchase by product type, treat unknown types as subscriptions only when an expiry date exists, and then compare that expiry date with the current date. The same pattern can be adapted for debugging views, support tools, or eligibility checks.
Debugging use only
Treat parsed receipt contents as debugging information, not proof of purchase. There is no on-device validation in this workflow, so the data should not be trusted for entitlement decisions on its own. For purchase verification, validate receipts on a server or use an SDK that performs backend validation.
How ASN.1 containers work
The receipt file arrives as Data: a binary blob. ReceiptParser reads it as an ASN.1 container, a structure that describes the size and type of its contents and can recursively contain other containers. Every container follows a Tag-Length-Value pattern.
Type comes first and combines an identifier, a class, and an encoding type. The identifier indicates the kind of value, such as a string, boolean, or set. The encoding type distinguishes primitive containers from constructed ones that hold another container inside.
Length describes how large the value is. When the length is below 128, a single byte is used: the first bit is 0 and the remaining seven bits hold the length. For lengths of 128 or more, the first bit is 1, the next seven bits say how many bytes follow, and those bytes hold the actual length.
Value can be a nested container, a list of containers, or a primitive value. Because most receipt components nest several levels deep, a recursive reader is essential.
Finding the receipt payload
The useful payload is not at the top level. You walk the nested containers until you find the one whose object identifier is 1.2.840.113549.1.7.1, which corresponds to data. The container immediately after that identifier holds the receipt payload. Other identifiers in the same structure, such as the signature, use the same discovery approach.
Decoding the object identifier
An object identifier is a dot-separated sequence of numbers, where each number is a node. The first two nodes share the first byte: the first node is that byte divided by 40, and the second node is the remainder after dividing by 40.
Remaining nodes use Variable-length quantity encoding, a compact way to represent integers in as few bytes as possible. Encoding splits a number into seven-bit groups, removes leading all-zero groups, and sets a continuation bit on every group except the last. Decoding reverses that process: while the first bit of a byte is 1, its seven remaining bits are appended to a buffer; when a byte starts with 0, the buffer is closed and interpreted as a binary number. Each closed number becomes the next node, and joining all nodes with dots reconstructs the identifier.
Interpreting the receipt payload
Once the payload container is identified, its contents follow Apple's receipt format definition. The payload contains an unordered set of containers representing receipt attributes. Each attribute container holds three inner containers: one for its type, one for its version, and one for its value.
The type determines how the value should be read. Types not listed in Apple's field definitions are intended for Apple only. Although a value is stored as an octet string, it often contains another ASN.1 container; opaque values and SHA-1 hashes were exceptions observed in testing. For example, type 17 represents an in-app purchase and uses a nested definition similar to the receipt itself. Primitive values are decoded directly from the binary payload, with strings in UTF-8 and dates as RFC 3306 ASCII strings.
Decision checklist and summary
Use this checklist before adding local receipt parsing to a project:
- Confirm whether StoreKit 2 APIs already meet the need, or whether pre-iOS 15 support requires Bundle.appStoreReceiptURL.
- Decide whether the goal is debugging, eligibility estimation, or backend submission, and keep untrusted local data out of entitlement decisions.
- Add the standalone ReceiptParser package if the full SDK is not already a dependency.
- Parse existing receipt data with code0001, or load the local receipt with code0002.
- Walk ASN.1 containers recursively, locate the data object identifier, then interpret receipt attributes by type.
- Verify purchase status on a server when trust matters.
ReceiptParser removes the need to hand-roll the ASN.1, object identifier, and attribute decoding layers, leaving the AppleReceipt API as the interface for inspection and debugging tools.