Tutoriály
Pridávanie nákupov v aplikácii do vášho projektu
Last updated November 27, 2023
Tento sprievodca vám ukáže, ako pridať nákupy v aplikácii do vášho exportovaného projektu hyperPad. Pred pokračovaním si prečítajte všetky požiadavky nižšie.
Požiadavky
- Musíte mať Mac počítač, aby ste mohli exportovať svoj projekt a pridať nákupy v aplikácii do vášho exportovaného projektu.
- Musíte mať cocoapods nainštalovaný na Macu. Ak nemáte nainštalovaný cocoapods na vašom Macu, postupujte podľa pokynov tu.
- Musíte mať nainštalovanú aplikáciu Terminál na vašom Macu.
- Musíte mať nainštalovanú aplikáciu textový editor na vašom Macu.
Postup
- Pridajte pod 'SwiftyStoreKit' do zoznamu podov v súbore Podfile vo vašom exportovanom projekte pomocou textového editora.
- V termináli spustite pod install. Môže to chvíľu trvať, tak chvíľu vydržte!
- Nahraďte obsah súboru HPSwift.swift nasledovným súborom
//
// HPSwift.swift
// hyperPad-Project
//
// Created by Hamed Saadat on 2019-08-07.
//
import Foundation
import SwiftyStoreKit
@objc class HPSwift: NSObject {
fileprivate var _behaviours: HPBehaviours? = nil
@objc var behaviours: HPBehaviours? {
get {
return _behaviours
}
set(behaviours) {_behaviours = behaviours
SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
for purchase in purchases {
switch purchase.transaction.transactionState {
case .purchased, .restored:
if purchase.needsFinishTransaction {
// Deliver content from server, then:
SwiftyStoreKit.finishTransaction(purchase.transaction)
}
// Unlock content
behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")
case .failed, .purchasing, .deferred:
behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseError")
break // do nothing
@unknown default:
break
}
}
}
behaviours?.addReceiveKey("restorePurchases", onReceive: { (receiveValue) in
SwiftyStoreKit.restorePurchases(atomically: true) { results in
if results.restoreFailedPurchases.count > 0 {
behaviours?.broadcastValue("Restore Failed: \(results.restoreFailedPurchases)" as NSString, withKey: "purchaseError")
}
else if results.restoredPurchases.count > 0 {
results.restoredPurchases.forEach { (purchase) in
behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")
}
}
else {
print("Nothing to Restore")
}
}
})
/*
Add Behaviour functionality here
*/
behaviours?.addReceiveKey("purchase", onReceive: { (recieveValue) in
// handle in app purchase logic! //
SwiftyStoreKit.purchaseProduct("INSERT_PURCHASE_IDENTIFIER_HERE_OR_USE_recieveValue", quantity: 1, atomically: true) { result in
switch result {
case .success(let purchase):
behaviours?.broadcastValue(purchase.productId as NSString, withKey: "purchaseComplete")
case .error(let error):
var message = ""
switch error.code {
case .unknown: message = "Unknown error. Please contact support"
case .clientInvalid: message = "Not allowed to make the payment"
case .paymentCancelled: break
case .paymentInvalid: message = "The purchase identifier was invalid"
case .paymentNotAllowed: message = "The device is not allowed to make the payment"
case .storeProductNotAvailable: message = "The product is not available in the current storefront"
case .cloudServicePermissionDenied: message = "Access to cloud service information is not allowed"
case .cloudServiceNetworkConnectionFailed: message = "Could not connect to the network"
case .cloudServiceRevoked: message = "User has revoked permission to use this cloud service"
default: message = ((error as NSError).localizedDescription)
}
behaviours?.broadcastValue(message as NSString, withKey: "purchaseFailed")
}
}
})
}
}
}

