应用程序不处理消费品的应用内购买促销代码的兑换

zs2*_*020 17 in-app-purchase promotion-code swift3

我的应用内置了应用内购买(我按照本教程),购买功能有效.但是,当我在App Store中为其中一个应用内购买产品兑换促销代码时,我的应用不响应它.即使App Store说该产品已成功兑换,我的应用程序也没有回复.

如果您的应用可以处理促销代码,是否有任何人在应用内购买测试?你介意分享解决方案吗?

我从这开始:

override func viewDidLoad() {
    NotificationCenter.default.addObserver(self,
        selector: #selector(applicationDidBecomeActive(notification:)),
        name: NSNotification.Name.UIApplicationDidBecomeActive,
        object: nil
    )
}

func applicationDidBecomeActive(notification: NSNotification){
    let store = IAPHealper()

    //what needs to be put here?
}


extension IAPHelper: SKPaymentTransactionObserver {

    public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            switch (transaction.transactionState) {
            case .purchased:
                completeTransaction(transaction)
                break
            case .failed:
                failedTransaction(transaction)
                break
            case .restored:
                restoreTransaction(transaction)
                break
            case .deferred:
                break
            case .purchasing:
                break
            }
        }
    }

    fileprivate func completeTransaction(_ transaction: SKPaymentTransaction) {
        print("completeTransaction...")
        deliverPurchaseNotificatioForIdentifier(transaction.payment.productIdentifier)
        defaultQueue.finishTransaction(transaction)
        purchaseCompletionHandler?(true, transaction)
    }

    fileprivate func restoreTransaction(_ transaction: SKPaymentTransaction) {
        guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }

        print("restoreTransaction... \(productIdentifier)")
        deliverPurchaseNotificatioForIdentifier(productIdentifier)
        defaultQueue.finishTransaction(transaction)
    }

    fileprivate func failedTransaction(_ transaction: SKPaymentTransaction) {
        print("failedTransaction...")

        if transaction.error!._code != SKError.paymentCancelled.rawValue {
            print("Transaction Error: \(String(describing: transaction.error?.localizedDescription))")
            purchaseCompletionHandler?(false, transaction)
        }

        defaultQueue.finishTransaction(transaction)
    }

    fileprivate func deliverPurchaseNotificatioForIdentifier(_ identifier: String?) {
        guard let identifier = identifier else { return }
        purchasedProductIdentifiers.insert(identifier)
        //NSNotificationCenter.defaultCenter().postNotificationName(IAPHelper.IAPHelperPurchaseNotification, object: identifier)
    }

    public func paymentQueue(_ queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]){
        print("Removed from queue.")
        print(transactions)
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Poc*_*chi 5

除了安德鲁所说的(关于仅适用于生产环境的促销代码),您的"应用购买"处理机制似乎可能存在问题.

您应该在AppDelegate中初始化您的购买处理对象,以便它立即收到待处理的购买和刚创建的购买,(或者在这种情况下,当代码被兑换时)

如果您查看此处显示的示例:

https://developer.apple.com/library/content/technotes/tn2387/_index.html#//apple_ref/doc/uid/DTS40014795-CH1-BEST_PRACTICES-ADD_A_TRANSACTION_QUEUE_OBSERVER_AT_APPLICATION_LAUNCH

你实际上正在做苹果不推荐的事情.

而是在AppDelegate中添加StoreKit观察者:

class AppDelegate: UIResponder, UIApplicationDelegate {
                           ....
    // Attach an observer to the payment queue.
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Attach an observer to the payment queue.
        SKPaymentQueue.default().add(your_observer)
        return true
    }

    // Called when the application is about to terminate.
    func applicationWillTerminate(_ application: UIApplication) {
       // Remove the observer.
       SKPaymentQueue.default().remove(your_observer)
    }

    // OTHER STUFF...

}
Run Code Online (Sandbox Code Playgroud)

您可能实际上错过了应用程序因此而收到购买的时间.

顺便说一句,您已经有一个"In App Purchases"帮助对象(IAPHealper).您需要做的就是让AppDelegate存储一个变量,并在"didFinishLaunchingWithOptions"方法中实例化它.

就像是:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var store : IAPHealper;
                           ....
    // Attach an observer to the payment queue.
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        store = IAPHelper();

        return true
    }

    // OTHER STUFF...

}
Run Code Online (Sandbox Code Playgroud)

编辑:(保持堆栈溢出的所有信息)

根据Apple的员工回复:https: //forums.developer.apple.com/message/204476#204476

促销代码只能在生产商店中进行测试.

所以,为了测试它们:

  1. 提交您的应用程序以进行App Review.
  2. 在将来某个时间设置应用程序的发布日期,以便App Review批准该应用程序后,普通用户无法使用该应用程序.
  3. 使用应用促销代码安装应用
  4. 使用in app促销代码.

最后,开发人员的帖子还警告我们,在应用程序获得批准后,您必须等待48小时才能开始运行代码.


因此,如果按照上述步骤操作后,您的应用程序的行为不符合预期.然后,当Apple向您发送"购买成功"通知时,您遇到的问题是您的应用程序没有"准备就绪".因此,为什么你应该遵循本答案第一部分所描述的指导原则.(关于启动应用程序后立即初始化事务侦听器)


zs2*_*020 1

@Eatton在另一个线程上提供了非常有用的信息。我只是想总结一下处理消耗品促销代码兑换的解决方案。

I. 您应该使用SwiftyStoreKit,并将以下代码放入 AppDelegate 中:

SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
    for purchase in purchases {
        switch purchase.transaction.transactionState {
        case .purchased, .restored: 
            if purchase.needsFinishTransaction {
                SwiftyStoreKit.finishTransaction(purchase.transaction)
            }
            // Unlock content
            self.unlockIAPContent(productID: purchase.productId)
        case .failed, .purchasing, .deferred:
            break // do nothing
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

二. 如果你想调用任意ViewController中的逻辑,请考虑使用NotificationCenter,将代码放在//Unlock content下

三.如何测试呢?

在 iOS 11 版本中,Apple 引入了一项新功能,可直接在 App Store 中推广应用内购买

首先添加处理程序:

#if DEBUG
SwiftyStoreKit.shouldAddStorePaymentHandler = { payment, product in
    return true
}
#endif
Run Code Online (Sandbox Code Playgroud)

然后在 Mac 上编写以下 URL,并将其通过 AirDrop 发送到您的 iOS 设备并在 Safari 中打开

itms-services://?action=purchaseIntent&bundleId=com.example.app&productIdentifier=product_name
Run Code Online (Sandbox Code Playgroud)

SwiftyStoreKit.completeTransactions()然后你的完成块AppDelegate将被触发。

这也可以用于测试促销代码兑换,因为 URL 请求创建一个待处理事务并将其添加到队列中。确保为您的产品版本删除此代码。

希望这可以帮助!