Do you need help on a specific subject? Use the contact form (Request a blog entry) on the right hand side.

2015-05-22

OSX Receipt validation in Swift, part 8: Using Receigen

With just a little trickery, it is possible to use Receigen to do the hard work.
First of, you will need to purchase the Receigen tool. It is available from the App Store.

Once you have Receigen, start the application, click on "Code Assistant", in the new window click "Continue", fill in the fields as appropriate for your App.

Then click "Continue" and generate a file with "Save As ...". For this example, let's call it "receigen.h". Add this file to your project, but don't add it to the bridging header file.

Unfortunately it is not possible to directly call the CheckReceiptAndRun operation in receigen.h, that will just create linking errors.

But it is possible to call it through an intermediate.

So, create an Objective-C file as intermediary, I called mine "startup". Create both the header and implementation files. In the header file (Startup.h) write:

//
//  Startup.h
//

#ifndef MyGreatApp_Startup_h
#define MyGreatApp_Startup_h

int startup(int argc, const char * argv[]);

#endif

In the implementation file (Startup.m) write:

//
//  Startup.m
//

#import <Foundation/Foundation.h>
#import "receigen.h"

int startup(int argc, const char * argv[])
{
    return CheckReceiptAndRun(argc, (const char **)argv);
}

Add the "Startup.h" file to the bridging header file.

I think it is better to keep the intermediary (Startup) and the file generated by Receigen separate. When we create a new release of the App, the only thing we have to do is to regenerate and replace the receigen.h file. The Startup files are unaffected.

In main.swift replace the code with:

//
//  main.swift
//

startup(Process.argc, UnsafeMutablePointer<UnsafePointer<Int8>>(Process.unsafeArgv))

Now compile and test the code. It should run without an hitch.

Congrats, your app now has properly (IMO!) implemented Receipt Validation & Verification code.

Note: If you use Receigen, you do need to add openSSL to your App. But it is not necessary to import the apple root certificate. Accessing the C-union parts can be skipped as well, as can the determination of the GUID.

Note: If your project does not have a "main.swift" file, then it probably has an annotation in your AppDelegate: @NSApplicationMain If that is the case, simply remove the annotation from the AppDelegate and create the "main.swift" file manually. It must be called main.swift though!

See Receigen at work in my Port Spy app in the App Store. A utility that helps you debug your socket based application and includes its own source code. So you can see first hand how to implement socket based io in Swift as well as Receigen usage. And you will be helping this blog!

Happy coding

Did this help?, then please help out a small independent.
If you decide that you want to make a small donation, you can do so by clicking this
link: a cup of coffee ($2) or use the popup on the right hand side for different amounts.
Payments will be processed by PayPal, receiver will be sales at balancingrock dot nl
Bitcoins will be gladly accepted at: 1GacSREBxPy1yskLMc9de2nofNv2SNdwqH

We don't get the world we wish for... we get the world we pay for.

No comments:

Post a Comment