Юра Яценко Юра Яценко - 5 months ago 11
Swift Question

How to use Chartboost Analytics with swift?

I've imported Chartboost via headers and cannot get CBAnalytics from Chartboost.framework to call next function in Swift.

+ (void) trackInAppPurchaseEvent:(NSData *)transactionReceipt
product:(SKProduct *)product;

I followed these instructions


I had a pre-existing Obj-C project, so I jury-rigged it to show this call in Swift.

Here's the SDK header import I needed to add to my project's Bridging-Header.h file:

#import <Chartboost/CBAnalytics.h>

Here's my actual Swift call:

@objc func makeChartboostPIACall(myReceipt:NSData, myProduct:SKProduct) {
    CBAnalytics.trackInAppPurchaseEvent(myReceipt, product: myProduct)

It SHOULD be that easy, but here's a little more detail on where those values I was passing in are coming from:

In my case, I was passing in the values from my Obj-C code:

[mySwiftInstance makeChartboostPIACall:transaction.transactionReceipt myProduct:myProduct];

I made that call after I had just confirmed that a payment transaction had successfully completed (SKPaymentTransactionStatePurchased). Thus, "transaction" is a SKPaymentTransaction object, and "myProduct" is the matching SKProduct of what had just been purchased.

https://developer.apple.com/library/ios/documentation/StoreKit/Reference/SKPaymentTransaction_Class/index.html#//apple_ref/occ/instp/SKPaymentTransaction/transactionReceipt https://developer.apple.com/library/ios/documentation/StoreKit/Reference/SKProduct_Reference/index.html

It's worth noting that transaction.transactionReceipt is deprecated as of iOS 7, and this is the recommended method to use going forward:

NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];