objective-cswiftxcodeanalyticschartboost

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 https://answers.chartboost.com/hc/en-us/articles/205606995 https://answers.chartboost.com/hc/en-us/articles/204639335-Post-Install-Analytics-Event-Level-Tracking-via-SDK#ios


Solution

  • 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:

    //Swift
    @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:

    //Obj-C
    [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:

    //Obj-C
    NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
    NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];