This page contains examples of code integration in Objective-C/Swift for OS X.

To integrate the generated code with Swift, a bit of work is required. You can’t drop the header in the bridging header !!!.

Startup Integration

As it is not possible to directly call the CheckReceiptAndRun function, a workaround is needed.

If you haven’t done yet, you need to add a bridging header to your Swift application. See that page for more information about bridging header.

Create a header file that define the startup function:

//
//  startup.h
//
#ifndef MyApp_startup_h
#define MyApp_startup_h

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

#endif

Then, implement it by calling the CheckReceiptAndRun function:

//
//  startup.m
//
#import <Foundation/Foundation.h>
#import "receigen.h" // <= This is the header containing the generated code

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

Once it is done, add the startup.h header to the bridging header. It should look like:

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "startup.h"

Now, create a file named main.swift (the name is important) that will call the thunk, hence the startup validation code:

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

Note that if your project does not have a `main.swift` or a `main.m` file, then your application delegate is probably annotated with an `@NSApplicationMain`. In this case, simply remove the annotation from the application delegate so the `main.m` file is properly called.

That’s it.

Common Integration

The generic way to integrate the generated code is done through an Objective-C class. Other integrations are possible and you are encouraged to have imagination about it.

Create a surrogate class (both header/source files):

#import <Foundation/Foundation.h>

@interface MisleadingNameManager : NSObject

- (void)initializeSetttings; // <= Note that the function's name is misleading on purpose

@end
#import "MisleadingNameManager.h"
#import "receigen.h" // <= This is the header containing the generated code

@implementation MisleadingNameManager

- (void)initializeSetttings {
    ReceiptValidation_CheckReceipt(); // <= Note that the function's name depends on your parameter
}

@end

The surrogate class allows to wrap the validation code and to avoid nasty linkage errors. Once it is done, add the MisleadingNameManager.h header to the bridging header. It should look like:

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "MisleadingNameManager.h"

Now you can call the validation code through the surrogate class:

var manager = MisleadingNameManager()
manager.initializeSetttings()

That’s it.