Как я уже упоминал в заголовке, у меня есть 2 покупки в приложении с разными ценами, и я хочу обрабатывать их с разных контроллеров. У меня есть IAPHelper
IAPHelper.swift
import StoreKit
import SwiftKeychainWrapper
public typealias ProductIdentifier = String
public typealias ProductsRquestCompletionHandler = (_ success: Bool, _ products: [SKProduct]?) -> Void
class IAPHelper: NSObject {
public var purchasedProducts = Set<ProductIdentifier>()
private let productIdentifiers: Set<String>
private var productsRequest: SKProductsRequest?
private var productsRquestCompletionHandler: ProductsRquestCompletionHandler?
public func isPurchased(_ productIdentifier: ProductIdentifier) -> Bool {
return purchasedProducts.contains(productIdentifier)
}
public func restorePurchases() {
SKPaymentQueue.default().restoreCompletedTransactions()
}
public class func canMakePayments() -> Bool {
return SKPaymentQueue.canMakePayments()
}
init(productIDs: Set<String>) {
productIdentifiers = productIDs
purchasedProducts = Set(productIDs.filter {
KeychainWrapper.standard.bool(forKey: $0) ?? false
})
super.init()
}
}
extension IAPHelper {
func buyProduct(product: SKProduct) {
let payment = SKPayment(product: product)
SKPaymentQueue.default().add(payment)
}
func requestProducts(completionHandler: @escaping ProductsRquestCompletionHandler) {
productsRequest?.cancel()
productsRquestCompletionHandler = completionHandler
productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)
productsRequest?.delegate = self
productsRequest?.start()
}
}
extension IAPHelper: SKProductsRequestDelegate {
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
productsRquestCompletionHandler?(true, response.products)
productsRquestCompletionHandler = .none
productsRequest = .none
}
func request(_ request: SKRequest, didFailWithError error: Error) {
productsRquestCompletionHandler?(false, nil)
productsRquestCompletionHandler = .none
productsRequest = .none
}
, где я могу справиться со стадиями, и PdfProduct.swift
для моих продуктов.
PdfProducts.swift
public struct PdfProducts {
static let productIDsNonConsumables: Set<ProductIdentifier> = ["io.myApp.myApp.PDF1"]
static let store = IAPHelper(productIDs: PdfProducts.productIDsNonConsumables)
static func handlePurchase(purchaseIdentifier: String) {
if productIDsNonConsumables.contains(purchaseIdentifier) {
store.purchasedProducts.insert(purchaseIdentifier)
}
}
}
У меня есть tableView
с одним inApp ("io.myApp.myApp.PDF1"), который похож на это.
BuyTableViewCell.swift
static let priceFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.formatterBehavior = .behavior10_4
formatter.numberStyle = .currency
return formatter
}()
var buyButtonHandler: ((_ product: SKProduct) -> ())?
var product: SKProduct? {
didSet {
guard let product = product else { return }
let price = BuyTableViewCell.priceFormatter.string(from: product.price)
if buyBtn.allTargets.count == 0 {
buyBtn.addTarget(self, action: #selector(buyButtonTapped), for: .touchUpInside)
}
if PdfProducts.store.isPurchased(product.productIdentifier) {
openBtn.setTitle(NSLocalizedString("open", comment: "Open button."), for: .normal)
buyBtn.isHidden = true
previewBtn.isHidden = true
openBtn.isHidden = false
SVProgressHUD.dismiss()
}else if IAPHelper.canMakePayments() {
openBtn.isHidden = true
buyBtn.setTitle(NSLocalizedString("buyButton", comment: "Buy button.")+" \(price!)", for: .normal)
previewBtn.setTitle("Preview", for: .normal)
}else {
buyBtn.setTitle("Not Available", for: .normal)
}
}
}
@objc func buyButtonTapped(_ sender: AnyObject) {
guard let product = product, let buyButtonHandler = buyButtonHandler else { return }
buyButtonHandler(product)
}
и я могу отлично справиться с inApp с помощью
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "buyCell", for: indexPath) as! BuyTableViewCell
let product = products[(indexPath as NSIndexPath).row]
cell.product = product
cell.buyButtonHandler = { product in
SVProgressHUD.show()
PdfProducts.store.buyProduct(product: product)
}
}
Все работает отлично, но теперь я хочу обработать другоеВ приложении из моего MainViewController
но я не могу найти способ заставить его работать. Второй productID
: "io.myApp.myApp.PDF2"