This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct InAppProducts { | |
public static let product = "(제품ID)" | |
private static let productIdentifiers: Set<String> = [InAppProducts.product] | |
public static let store = IAPHelper(productIds: InAppProducts.productIdentifiers) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ViewController { | |
private func initIAP() { | |
NotificationCenter.default.addObserver(self, selector: #selector(handleIAPPurchase(_:)), name: .IAPHelperPurchaseNotification, object: nil) | |
// IAP 불러오기 | |
InAppProducts.store.requestProducts { [weak self] (success, products) in | |
guard let self = self, success else { return } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension IAPHelper { | |
// 구매이력 영수증 가져오기 - 검증용 | |
public func getReceiptData() -> String? { | |
if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL, | |
FileManager.default.fileExists(atPath: appStoreReceiptURL.path) { | |
do { | |
let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped) | |
let receiptString = receiptData.base64EncodedString(options: []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension IAPHelper: SKProductsRequestDelegate { | |
// 인앱결제 상품 리스트를 가져온다 | |
public func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { | |
let products = response.products | |
productsRequestCompletionHandler?(true, products) | |
clearRequestAndHandler() | |
} | |
// 상품 리스트 가져오기 실패할 경우 | |
public func request(_ request: SKRequest, didFailWithError error: Error) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import StoreKit | |
public typealias ProductsRequestCompletionHandler = (_ success: Bool, _ products: [SKProduct]?) -> Void | |
class IAPHelper: NSObject { | |
private let productIdentifiers: [String] | |
private var productsRequest: SKProductsRequest? | |
private var productsRequestCompletionHandler: ProductsRequestCompletionHandler? | |