教程
在项目中添加应用内购买功能
Published 2023年11月27日
本指南将演示如何在导出的 hyperPad 项目中添加应用内购买。继续操作前,请检查以下所有要求。
要求
- 你需要一台 Mac 电脑,用于导出项目并在导出的项目中添加应用内购买。
- 你需要在 Mac 上安装 cocoapods。如果尚未安装,请按照此处的说明操作。
- 你需要在 Mac 上安装 Terminal 应用。
- 你需要在 Mac 上安装一款文本编辑器应用。
操作步骤
- 使用文本编辑器,将 pod 'SwiftyStoreKit' 添加到导出项目 Podfile 文件的 pod 列表中。
- 在终端中运行 pod install。这可能需要一些时间,请耐心等待!
- 将 HPSwift.swift 的内容替换为以下文件
//
// HPSwift.swift
// hyperPad-Project
//
// Created by Hamed Saadat on 2019-08-07.
//
import Foundation
import SwiftyStoreKit
@objc class HPSwift: NSObject {
fileprivate var _behaviours: HPBehaviours? = nil
@objc var behaviours: HPBehaviours? {
get {
return _behaviours
}
set(behaviours) {_behaviours = behaviours
SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
for purchase in purchases {
switch purchase.transaction.transactionState {
case .purchased, .restored:
if purchase.needsFinishTransaction {
// Deliver content from server, then:
SwiftyStoreKit.finishTransaction(purchase.transaction)
}
// Unlock content
behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")
case .failed, .purchasing, .deferred:
behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseError")
break // do nothing
@unknown default:
break
}
}
}
behaviours?.addReceiveKey("restorePurchases", onReceive: { (receiveValue) in
SwiftyStoreKit.restorePurchases(atomically: true) { results in
if results.restoreFailedPurchases.count > 0 {
behaviours?.broadcastValue("Restore Failed: \(results.restoreFailedPurchases)" as NSString, withKey: "purchaseError")
}
else if results.restoredPurchases.count > 0 {
results.restoredPurchases.forEach { (purchase) in
behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")
}
}
else {
print("Nothing to Restore")
}
}
})
/*
Add Behaviour functionality here
*/
behaviours?.addReceiveKey("purchase", onReceive: { (recieveValue) in
// handle in app purchase logic! //
SwiftyStoreKit.purchaseProduct("INSERT_PURCHASE_IDENTIFIER_HERE_OR_USE_recieveValue", quantity: 1, atomically: true) { result in
switch result {
case .success(let purchase):
behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")
case .error(let error):
var message = ""
switch error.code {
case .unknown: message = "Unknown error. Please contact support"
case .clientInvalid: message = "Not allowed to make the payment"
case .paymentCancelled: break
case .paymentInvalid: message = "The purchase identifier was invalid"
case .paymentNotAllowed: message = "The device is not allowed to make the payment"
case .storeProductNotAvailable: message = "The product is not available in the current storefront"
case .cloudServicePermissionDenied: message = "Access to cloud service information is not allowed"
case .cloudServiceNetworkConnectionFailed: message = "Could not connect to the network"
case .cloudServiceRevoked: message = "User has revoked permission to use this cloud service"
default: message = ((error as NSError).localizedDescription)
}
behaviours?.broadcastValue(message as NSString, withKey: "purchaseFailed")
}
}
})
}
}
}
注意事项
behaviours?.addReceiveKey("restorePurchases", onReceive: { (receiveValue) in
// add your code here
})
当你触发带有 restorePurchases 键的广播行为时,就会触发此代码。
behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")
购买完成后,将触发键为 purchaseComplete 的接收消息行为。INSERT_PURCHASE_IDENTIFIER_HERE_OR_USE_recieveValue 可以替换为你在 AppStoreConnect 中的购买项目标识符,或 receiveValue 参数。如果使用 receiveValue,请确保在广播行为中发送的是 App Store Connect 中的标识符。

