Skip to content

Instantly share code, notes, and snippets.

View eunjiChung's full-sized avatar
:octocat:

eunjiChung

:octocat:
View GitHub Profile
public struct InAppProducts {
public static let product = "(제품ID)"
private static let productIdentifiers: Set<String> = [InAppProducts.product]
public static let store = IAPHelper(productIds: InAppProducts.productIdentifiers)
}
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 }
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: [])
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:
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) {
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?