У меня есть приложение, которое предлагает монеты в качестве расходных IAP.В большинстве случаев это работает нормально, но иногда пользователь связывается со мной, чтобы сказать, что несмотря на то, что платеж прошел, и он получил счет от Apple, он не получил свои монеты.
Кроме того, приложение не обрабатываетвыкупить расходные промо-коды вообще.При погашении в App Store отображается сообщение о том, что IAP успешно погашен, но при открытии приложения монет там никогда не бывает.
Я следовал рекомендации Apple и добавляю наблюдателя транзакции в файл делегата приложения.Но эта проблема сохраняется.Это сводит меня с ума, поэтому, если кто-нибудь, знакомый с расходуемыми IAP, сможет взглянуть на мой код и помочь мне, я буду вечно благодарен.
Обратите внимание: я пытался адаптировать этот учебник https://www.raywenderlich.com/1145-in-app-purchases-tutorial-consumables, который не добавляет наблюдателя транзакции в файл делегата приложения, но, по-моему, я упустил хитрость.
AppDelegate.swift (показан только соответствующий код):
class AppDelegate: UIResponder, UIApplicationDelegate {
let iapHelper = IAPHelper()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
SKPaymentQueue.default().add(iapHelper) // adds the transaction observer
}
func applicationWillTerminate(_ application: UIApplication) {
SKPaymentQueue.default().remove(iapHelper) // remove the transaction observer
}
}
IAPHelper.swift: Не уверен, что я все делаю правильно с линией static var helper = IAPHelper()
import StoreKit
// Notification that is generated when a product is purchased.
public let IAPHelperPurchaseNotification = "IAPHelperPurchaseNotification"
// Notification that is generated when a transaction fails.
public let IAPHelperTransactionFailedNotification = "IAPHelperTransactionFailedNotification"
// Notification that is generated when cannot retrieve IAPs from iTunes.
public let IAPHelperConnectionErrorNotification = "IAPHelperConnectionErrorNotification"
// Notification that is generated when we need to stop the spinner.
public let IAPHelperStopSpinnerNotification = "IAPHelperStopSpinnerNotification"
// Product identifiers are unique strings registered on the app store.
public typealias ProductIdentifier = String
// Completion handler called when products are fetched.
public typealias ProductsRequestCompletionHandler = (_ success: Bool, _ products: [SKProduct]?) -> ()
open class IAPHelper : NSObject {
/// MARK: - User facing API
fileprivate let productIdentifiers: Set<ProductIdentifier>
fileprivate var purchasedProductIdentifiers = Set<ProductIdentifier>()
fileprivate var productsRequest: SKProductsRequest?
fileprivate var productsRequestCompletionHandler: ProductsRequestCompletionHandler?
static var helper = IAPHelper() // is this right??
/// Init method
override init() {
// Set up the list of productIdentifiers
let PackOf4000Coins = "com.xxx.xxx.4000Coins" // This is the ProductID in iTunes Connect
let PackOf10000Coins = "com.xxx.xxx.10000Coins"
let PackOf30000Coins = "com.xxx.xxx.30000Coins"
let PackOf75000Coins = "com.xxx.xxx.75000Coins"
let PackOf175000Coins = "com.xxx.xxx.175000Coins"
let PackOf500000Coins = "com.xxx.xxx.500000Coins"
let RemoveAds = "com.xxx.xxx.RemoveAds"
let PlayerEditor = "com.xxx.xxx.PlayerEditor"
self.productIdentifiers = [PackOf4000Coins, PackOf10000Coins, PackOf30000Coins, PackOf75000Coins, PackOf175000Coins, PackOf500000Coins, RemoveAds, PlayerEditor]
for productIdentifier in self.productIdentifiers {
let purchased = UserDefaults.standard.bool(forKey: productIdentifier)
if purchased {
purchasedProductIdentifiers.insert(productIdentifier)
print("Previously purchased: \(productIdentifier)")
} else {
print("Not purchased: \(productIdentifier)")
}
}
super.init()
}
}
// MARK: - StoreKit API
extension IAPHelper {
public func requestProducts(_ completionHandler: @escaping ProductsRequestCompletionHandler) {
productsRequest?.cancel()
productsRequestCompletionHandler = completionHandler
productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)
productsRequest!.delegate = self
productsRequest!.start()
}
public func buyProduct(_ product: SKProduct) {
print("Buying \(product.productIdentifier)...")
let payment = SKPayment(product: product)
SKPaymentQueue.default().add(payment)
}
public func isProductPurchased(_ productIdentifier: ProductIdentifier) -> Bool {
return purchasedProductIdentifiers.contains(productIdentifier)
}
public class func canMakePayments() -> Bool {
return SKPaymentQueue.canMakePayments()
}
public func restorePurchases() {
SKPaymentQueue.default().restoreCompletedTransactions()
}
public func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
print("Restore queue finished.")
NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelperStopSpinnerNotification), object: nil)
}
public func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
print("Restore queue failed.")
NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelperConnectionErrorNotification), object: nil)
}
}
// MARK: - SKProductsRequestDelegate
extension IAPHelper: SKProductsRequestDelegate {
public func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
print("Loaded list of products...")
let products = response.products
productsRequestCompletionHandler?(true, products)
clearRequestAndHandler()
}
public func request(_ request: SKRequest, didFailWithError error: Error) {
print("Failed to load list of products.")
print("Error: \(error.localizedDescription)")
productsRequestCompletionHandler?(false, nil)
NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelperConnectionErrorNotification), object: nil)
clearRequestAndHandler()
}
fileprivate func clearRequestAndHandler() {
productsRequest = nil
productsRequestCompletionHandler = nil
}
}
// MARK: - SKPaymentTransactionObserver
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...")
deliverPurchaseNotificationForIdentifier(transaction.payment.productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
}
fileprivate func restoreTransaction(_ transaction: SKPaymentTransaction) {
guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }
print("restoreTransaction... \(productIdentifier)")
deliverPurchaseNotificationForIdentifier(productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
}
fileprivate func failedTransaction(_ transaction: SKPaymentTransaction) {
print("failedTransaction...")
NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelperStopSpinnerNotification), object: nil)
if transaction.error!._code != SKError.paymentCancelled.rawValue {
print("Transaction Error: \(String(describing: transaction.error?.localizedDescription))")
NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelperTransactionFailedNotification), object: nil)
} else {
print("Transaction Error else statement")
}
SKPaymentQueue.default().finishTransaction(transaction)
NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelperTransactionFailedNotification), object: nil)
}
fileprivate func deliverPurchaseNotificationForIdentifier(_ identifier: String?) {
guard let identifier = identifier else { return }
purchasedProductIdentifiers.insert(identifier)
UserDefaults.standard.set(true, forKey: identifier)
UserDefaults.standard.synchronize()
NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelperPurchaseNotification), object: identifier)
}
}
GameStoreViewController.swift : (Я обеспокоен, что не могу получить доступ к IAPHelper правильно из этого VC, это кажется неправильным)
import UIKit
import StoreKit
class GameStoreViewController: UIViewController, GetMoneyViewControllerDelegate {
// A list of available IAPs
var products = [SKProduct]() // non-consumables
var _coinProducts = [SKProduct]() // consumables
override func viewDidLoad() {
super.viewDidLoad()
retrieveIAPs() // Fetch the products from iTunes connect
// ** NSNotifications - this is how IAPHelper sends messages for me to handle here ** //
// Subscribe to a notification that fires when a product is purchased.
NotificationCenter.default.addObserver(self, selector: #selector(GameStoreViewController.productPurchased(_:)), name: NSNotification.Name(rawValue: IAPHelperPurchaseNotification), object: nil)
// Subscribe to a notification that fires when a transaction fails.
NotificationCenter.default.addObserver(self, selector: #selector(GameStoreViewController.transactionFailed(_:)), name: NSNotification.Name(rawValue: IAPHelperTransactionFailedNotification), object: nil)
// Subscribe to a notification that fires when there's a connection error.
NotificationCenter.default.addObserver(self, selector: #selector(GameStoreViewController.cannotConnect(_:)), name: NSNotification.Name(rawValue: IAPHelperConnectionErrorNotification), object: nil)
// Subscribe to a notification that fires when the activity spinner needs to stop.
NotificationCenter.default.addObserver(self, selector: #selector(GameStoreViewController.stopSpinner(_:)), name: NSNotification.Name(rawValue: IAPHelperStopSpinnerNotification), object: nil)
}
// Fetch the products from iTunes connect, put into the products array
func retrieveIAPs() {
products = []
IAPHelper.helper.requestProducts { success, products in
if success {
self.products = products!
print("Success. Products are: \(self.products)")
self._IAPsHaveBeenRetrieved = true
self.activitySpinnerStop()
// * Set up _coinProducts * //
// Filter out 'Other' IAPs
self._coinProducts = self.products.filter { $0.productIdentifier != "com.xxx.xxx.PlayerEditor" && $0.productIdentifier != "com.xxx.xxx.RemoveAds" }
} else {
print("Unable to connect to iTunes Connect. Products are: \(self.products)")
self.activitySpinnerStop()
self.showAlertWith(Localization("NoConnectionAlertTitle"), message: Localization("NoConnectionAlertMessage"))
}
}
}
// Restore purchases to this device.
@IBAction func restoreTapped(_ sender: AnyObject) {
print("Restore button tapped...")
activitySpinnerStart()
IAPHelper.helper.restorePurchases()
}
@IBAction func playerEditorPressed(_ sender: Any) {
// First, make sure the user is authorised to make payments on his/her device
if IAPHelper.canMakePayments() {
// Then, make sure the product has been retrieved from ITC and placed in the products array. If not, show alert
if products.count > 0 {
activitySpinnerStart()
if let theProduct = self.products.first(where: {$0.productIdentifier == "com.xxx.xxx.PlayerEditor"}) {
IAPHelper.helper.buyProduct(theProduct) // Purchasing the product. Fires productPurchased(notification:)
} else {
print("Could not find com.xxx.xxx.PlayerEditor in playerEditorPressed()")
}
} else {
showAlertWith(Localization("NoConnectionAlertTitle"), message: Localization("NoConnectionAlertMessage"))
}
} else {
showAlertWith(Localization("NoConnectionAlertTitle"), message: Localization("NoIAPAllowedMessage"))
}
}
@IBAction func removeAdsPressed(_ sender: Any) {
// First, make sure the user is authorised to make payments on his/her device
if IAPHelper.canMakePayments() {
// Then, make sure the product has been retrieved from ITC and placed in the products array. If not, show alert
if products.count > 0 {
activitySpinnerStart()
if let theProduct = self.products.first(where: {$0.productIdentifier == "com.xxx.xxx.RemoveAds"}) {
IAPHelper.helper.buyProduct(theProduct) // Purchasing the product. Fires productPurchased(notification:)
} else {
print("Uh-oh, could not find com.xxx.xxx.RemoveAds in removeAdsPressed()")
}
} else {
showAlertWith(Localization("NoConnectionAlertTitle"), message: Localization("NoConnectionAlertMessage"))
}
} else {
showAlertWith(Localization("NoConnectionAlertTitle"), message: Localization("NoIAPAllowedMessage"))
}
}
// MARK: - App Store NSNotification methods
// When a product is purchased, this notification fires
@objc func productPurchased(_ notification: Notification) {
print("productPurchased(), message received in GameStoreVC")
let productIdentifier = notification.object as! String
for (index, product) in products.enumerated() {
if product.productIdentifier == productIdentifier {
activitySpinnerStop()
print("Successful purchase! index = \(index), product = \(product.localizedTitle)")
// Apply purchase to game
switch product.productIdentifier {
case "com.xxx.xxx.4000Coins":
var coinsTotal = retrieveNumberOfCoins()
coinsTotal += 4_000
UserDefaults.standard.set(coinsTotal, forKey: "Coins")
case "com.xxx.xxx.10000Coins":
var coinsTotal = retrieveNumberOfCoins()
coinsTotal += 10_000
UserDefaults.standard.set(coinsTotal, forKey: "Coins")
case "com.xxx.xxx.30000Coins":
var coinsTotal = retrieveNumberOfCoins()
coinsTotal += 30_000
UserDefaults.standard.set(coinsTotal, forKey: "Coins")
case "com.xxx.xxx.75000Coins":
var coinsTotal = retrieveNumberOfCoins()
coinsTotal += 75_000
UserDefaults.standard.set(coinsTotal, forKey: "Coins")
case "com.xxx.xxx.175000Coins":
var coinsTotal = retrieveNumberOfCoins()
coinsTotal += 175_000
UserDefaults.standard.set(coinsTotal, forKey: "Coins")
case "com.xxx.xxx.500000Coins":
var coinsTotal = retrieveNumberOfCoins()
coinsTotal += 500_000
UserDefaults.standard.set(coinsTotal, forKey: "Coins")
case "com.xxx.xxx.PlayerEditor":
print("Player Editor successfully bought!")
// 'Unlock' Player Editor by setting it to 1
UserDefaults.standard.set(1, forKey: "PlayerEditor")
case "com.xxx.xxx.RemoveAds":
print("Remove Ads successfully bought!")
// 'Unlock' Remove Ads by setting it to 1
UserDefaults.standard.set(1, forKey: "RemoveAds")
default:
print("No such productIdentifier found")
}
}
}
}
// When a transaction fails, this notification fires
@objc func transactionFailed(_ notification: Notification) {
print("transactionFailed(), message received in GameStoreVC")
showAlertWith(Localization("TransactionFailedAlertTitle"), message: Localization("TransactionFailedAlertMessage"))
}
// When we cannot connect to iTunes to retrieve the IAPs, this notification fires
@objc func cannotConnect(_ notification: Notification) {
print("cannotConnect(), message received in GameStoreVC")
showAlertWith(Localization("NoConnectionAlertTitle"), message: Localization("NoConnectionAlertMessage"))
}
}
// MARK: - UITableViewDataSource
extension GameStoreViewController: UITableViewDataSource {
@objc func tableView(_ tableView: UITableView!, didSelectRowAtIndexPath indexPath: IndexPath!) {
// * COIN IAPs * //
if _selectionIndex == 1 {
// First, make sure the user is authorised to make payments on his/her device
if IAPHelper.canMakePayments() {
// Then, make sure the product has been retrieved and placed in the _coinProducts array. If not, show alert
if _coinProducts.count > 0 {
let product = _coinProducts[(indexPath as NSIndexPath).row]
IAPHelper.helper.buyProduct(product) // Purchasing the product. Fires productPurchased(notification:)
} else {
showAlertWith(Localization("NoConnectionAlertTitle"), message: Localization("NoConnectionAlertMessage"))
}
} else {
showAlertWith(Localization("NoConnectionAlertTitle"), message: Localization("NoIAPAllowedMessage"))
}
_selectedCoinIndex = indexPath.row // keep track of the selected coin index
iapTableView.reloadData() // to change colour of selected cell and unselected cells
}
}
}
Дополнительная информация: работают не расходные материалы («Редактор игрока» и «Удалить рекламу»)хорошо, хотя их промо-коды работают только при открытии Game Store и нажатии «Восстановить» - они не работают сразу после их погашения.
Кто-нибудь может помочь, пожалуйста?!