2017-06-02 27 views
13

我的應用內置了應用內購買功能(我遵循此tutorial),購買功能起作用。但是,當我在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) 
    } 
} 

感謝。

+0

請繼續扔這個[視頻](https://www.youtube.com/watch?v=dwPFtwDJ7tc)它會幫助你很多。 – Rex

回答

1

促銷代碼僅適用於生產環境。

如果您希望確保IAP在沒有實際應用程序發佈的情況下工作,那麼當應用程序處於「待定開發人員發佈」狀態(審覈後)時使用促銷代碼。一個應用程序促銷代碼安裝應用程序,另一個應用程序用於測試IAP有時可能需要額外的時間(最多2天)才能爲所有Apple服務器分配數據。

from official devforum answer

4

除了安德魯說什麼(約僅工作在生產環境中促銷代碼)看來你可能對您的「應用內購買」處理機制的問題。

你應該把你的購買處理對象初始化在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

你實際上做的是不推薦蘋果。

而是添加StoreKit觀察者INSIDE AppDelegate中:

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... 

} 

你實際上可能會在定時您的應用程序丟失接收購買因爲這個原因。

順便說一下,您已經有了「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 = IAPHealper(); 

     return true 
    } 

    // OTHER STUFF... 

} 

編輯:(爲了讓這裏所有的信息在堆棧溢出)

根據蘋果的員工迴應: https://forums.developer.apple.com/message/204476#204476

促銷代碼只能在生產商店進行測試。

因此,爲了對它們進行測試:

  1. 提交您的應用程序的應用程序審查。
  2. 設置應用程序在未來的發佈日期,以便一旦App Review批准該應用程序,它不適用於一般用戶。
  3. 使用應用程序促銷代碼來安裝應用程序
  4. 使用應用程序促銷代碼。

最後,開發商的帖子也提醒我們,後一個應用程序已經被批准,你必須等待48小時代碼開始工作之前。


因此,如果在按照上述步驟執行操作後,您的應用程序無法按預期運行。然後,當Apple向您發送「購買成功」通知時,您遇到的問題是您的應用程序未「準備就緒」。因此,爲什麼你應該遵循這個答案的第一部分描述的準則。 (關於一旦你的應用程序啓動,初始化你的交易監聽器)

+0

不起作用。你有沒有試過你的解決方案?基本上,該解決方案適用於應用內購買,但它不適用於促銷代碼兌換。 – zsong

+0

問題是,應用內促銷代碼與「應用內購買」完全相同。不過,他們會遵循應用邏輯,在應用啓動時「接收」成功的購買通知。因此,如果您已經按照此處提供的步驟操作:https://forums.developer.apple.com/message/204476#204476,但它仍然無法使用,這意味着「成功」購買通知正在丟失,因爲您的應用程序是「未準備好」,因此爲什麼我寫了如何正確配置應用程序購買以接收App Launch上的通知的說明。 – Pochi

+0

@zsong Apple的員工迴應說同樣的事情: 「如果在SKProductsRequest工作後in_app促銷代碼沒有激活,我會在啓動時驗證transactionObserver處於活動狀態。」 – Pochi

相關問題