如何在iOS中支持IAP订阅免费试用?

x8diyxa7  于 2022-12-15  发布在  iOS
关注(0)|答案(2)|浏览(832)

我有自动更新订阅。我添加了介绍提供免费试用-1周。
我的代码:

class IAPManager:NSObject {

    static let shared = IAPManager()
    private override init() {    }

    var products: [SKProduct] = []
    let paymentQueue = SKPaymentQueue.default()

    public func setupPurchases(callback: @escaping(Bool) -> ()) {
        if SKPaymentQueue.canMakePayments() {
            paymentQueue.add(self)
            callback(true)
            return
        }
        callback(false)
    }

    public func getProducts() {
        let identifiers: Set = [
            IAPProducts.auto.rawValue
        ]

        let productRequest = SKProductsRequest(productIdentifiers: identifiers)
        productRequest.delegate = self
        productRequest.start()
    }

    public func purchase(productWith identifier: String) {
        guard let product = products.filter({ $0.productIdentifier == identifier }).first else { return }

        let payment = SKPayment(product: product)
        paymentQueue.add(payment)
    }

    public func restoreCompletedTransactions() {
        paymentQueue.restoreCompletedTransactions()
    }
}
extension IAPManager: SKPaymentTransactionObserver {
    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            switch transaction.transactionState {
            case .deferred: break
            case .purchasing: break
            case .failed: failed(transaction: transaction)
            case .purchased: completed(transaction: transaction)
            case .restored: restored(transaction: transaction)
            }
        }
    }

    private func failed(transaction: SKPaymentTransaction) {
        if let transactionError = transaction.error as NSError? {
            if transactionError.code != SKError.paymentCancelled.rawValue {
                print("Ошибка транзакции: \(transaction.error!.localizedDescription)")
            }
        }
        paymentQueue.finishTransaction(transaction)
    }

    private func completed(transaction: SKPaymentTransaction) {
        print("success")
        NotificationCenter.default.post(name: NSNotification.Name(transaction.payment.productIdentifier), object: nil)
        paymentQueue.finishTransaction(transaction)
    }

    private func restored(transaction: SKPaymentTransaction) {
        paymentQueue.finishTransaction(transaction)
    }
}


extension IAPManager: SKProductsRequestDelegate {
    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
        self.products = response.products
        print(self.products.count)
        products.forEach { print($0.localizedTitle) }
    }
}

如何将用户选择的免费试用订阅发送到Apple服务器?对于自动续订订阅,我只需调用:

iapManager.purchase(productWith: identifier)

如何激活免费试用订阅?

7d7tgy0s

7d7tgy0s1#

免费试用由苹果自动管理--你只需要在App Store Connect中的产品上进行配置,苹果将允许每个订阅组的每个用户免费试用一次,你只需正常购买产品即可。

weylhg0b

weylhg0b2#

澄清:如果你使用X1 M0 N1 X网站来定义例如,对特定的自动可更新订阅的免费试用,免费试用将在首次购买订购时自动应用(通过订阅SKProduct)。没有办法允许用户跳过相关联的试用并立即购买付费订阅或者仅激活用于该订阅的相关联的免费试用。当自动应用免费试用时,iOS购买确认提醒会显示:“* 订阅{some}自动续订订阅。从现在开始免费{some time}。从{some date}开始自动续订{some price}。*”

相关问题