objective-c - 如何通过 swift 使用 Chartboost 分析?

标签 objective-c swift xcode analytics chartboost

我已通过 header 导入 Chartboost,但无法从 Chartboost.framework 获取 CBAnalytics 以调用 Swift 中的下一个函数。

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

我遵循了这些说明 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

最佳答案

我有一个预先存在的 Obj-C 项目,所以我临时操纵它以在 Swift 中显示此调用。

这是我需要添加到项目的 Bridging-Header.h 文件中的 SDK header 导入:

#import <Chartboost/CBAnalytics.h>

这是我实际的 Swift 调用:

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

应该这么简单,但这里有一些关于我传递的这些值来自何处的更多详细信息:

在我的例子中,我传递了来 self 的 Obj-C 代码的值:

//Obj-C
[mySwiftInstance makeChartboostPIACall:transaction.transactionReceipt myProduct:myProduct];

我在刚刚确认付款交易已成功完成 (SKPaymentTransactionStatePurchased) 后调用了该电话。因此,“transaction”是一个 SKPaymentTransaction 对象,“myProduct”是刚购买的匹配 SKProduct

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

值得注意的是 transaction.transactionReceipt 从 iOS 7 开始被弃用,这是推荐使用的方法:

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

关于objective-c - 如何通过 swift 使用 Chartboost 分析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38964982/

相关文章:

ios - 在 Swift 中定义一个只读属性

ios - UISearchController 触摸后看起来不对

ios - 将 UITextView 中的文本替换为可链接文本 - iOS

ios - 如何修复 ipad 中的方向错误?

objective-c - 检查变量是否属于枚举值

ios - 如何在用户退出时暂停我的应用程序,并在他们重新进入应用程序时恢复应用程序?

ios - 如何定位 UITableViewCell 内容 View

ios - swift : Terminating app due to uncaught exception 'NSInvalidArgumentException' , 原因: 'Entity name must not be nil.'

ios - cellForRowAt 中的 com.apple.main-thread EXC_BAD_ACCESS KERN_INVALID_ADDRESS

ios - 如何通过应用内购买锁定表格 View 中的特定行?