Search code examples
iosswiftin-app-purchasestorekit

How to properly handle In App Purchases for iOS apps and how to define the product?


I followed this guide to set up an in-app purchase in my app. I've done everything and set it up in App Store Connect. My problem is the actual purchase at the moment is simply a boolean in UserDefaults for ArePagesUnlocked = true But I am having trouble on where I actually define this in the product. I understand how to set up and manage the purchases and restores but where do I actually define the product as being an instruction to change the value of ArePagesUnlocked from false to true, On the Buy Button Action? I tried that but if there was no connection then the user has still unlocked it. Do I have to build this seperately and send it? I have been just getting a bit confused during my research and not found a clear answer thus far so any help would be appreciated. Thank you.

EDIT: code:

   public init(productIds: Set<ProductIdentifier>) {
    productIdentifiers = productIds
    for productIdentifier in productIds {
        let purchased = UserDefaults.standard.bool(forKey: productIdentifier) // this i use to see if purchased
        if purchased {
            purchasedProductIdentifiers.insert(productIdentifier)
            print("Previously purchased: \(productIdentifier)")
        } else {
            print("Not purchased: \(productIdentifier)")
        }
    }
    super.init()

    SKPaymentQueue.default().add(self)
}
}

Do I need to add a seperate UD Value from the one above? It seems It doesnt know its purchased until I restart the app, how can I make it instant?

extension IAPHelper: SKPaymentTransactionObserver {

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

private func complete(transaction: SKPaymentTransaction) {
    print("complete...")
    deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier)
    SKPaymentQueue.default().finishTransaction(transaction)
}

private func restore(transaction: SKPaymentTransaction) {
    guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }

    print("restore... \(productIdentifier)")
    deliverPurchaseNotificationFor(identifier: productIdentifier)
    SKPaymentQueue.default().finishTransaction(transaction)
}

private func fail(transaction: SKPaymentTransaction) {
    print("fail...")
    if let transactionError = transaction.error as NSError?,
        let localizedDescription = transaction.error?.localizedDescription,
        transactionError.code != SKError.paymentCancelled.rawValue {
        print("Transaction Error: \(localizedDescription)")
    }

    SKPaymentQueue.default().finishTransaction(transaction)
}

private func deliverPurchaseNotificationFor(identifier: String?) {
    guard let identifier = identifier else { return }

    purchasedProductIdentifiers.insert(identifier)
    UserDefaults.standard.set(true, forKey: identifier)
    NotificationCenter.default.post(name: .IAPHelperPurchaseNotification, object: identifier)
}

Solution

  • The usual strategy is that in your paymentQueue(_:updatedTransactions:) you write into user defaults if the user purchases.