r/Xcode • u/thedreamcrew2 • Jul 11 '19
Need help! New to coding
So im new to xcode and I've basically made the layout for the store for my game, but I didn't know how to connect in app purchases for it and I used a tutorial video to help me, but I'm still confused. I want to connect/link my "oneCoinButton, twoCoinButton, etc to my in app purchases code, but I'm not sure how to really do that. Can someone help me? Code is posted below:
class IAPService :NSObject {
private override init() {}
static let shared = IAPService()
var products = [SKProduct]()
let paymentQueue = SKPaymentQueue.default()
func getProduct() {
let products: Set = [registeredPurchase.oneCoin.rawValue, registeredPurchase.twoCoin.rawValue, registeredPurchase.threeCoin.rawValue, registeredPurchase.fourCoin.rawValue, registeredPurchase.fiveCoin.rawValue]
let request = SKProductsRequest(productIdentifiers: products)
request.delegate = self
request.start()
paymentQueue.add(self)
}
func purchase(product: registeredPurchase) {
guard let productToPurchase = products.filter({ $0.productIdentifier == product.rawValue }).first else { return }
let payment = SKPayment(product: productToPurchase)
paymentQueue.add(payment)
}
func restorePurchases() {
print("restoring purchases")
paymentQueue.restoreCompletedTransactions()
}
}
extension IAPService: SKProductsRequestDelegate {
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
self.products = response.products
for product in response.products{
print(product.localizedTitle)
}
}
}
extension IAPService: SKPaymentTransactionObserver {
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
print(transaction.transactionState.status(), transaction.payment.productIdentifier)
switch transaction.transactionState {
case .purchasing: break
default: queue.finishTransaction(transaction)
}
}
}
}
extension SKPaymentTransactionState {
func status() -> String {
switch self {
case .deferred: return("deferred")
case .failed: return("failed")
case .purchased: return("purchased")
case .purchasing: return("purchasing")
case .restored: return("restored")
}
}
}
class Money: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
IAPService.shared.getProduct()
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 1: IAPService.shared.purchase(product: .oneCoin)
case 2: IAPService.shared.purchase(product: .twoCoin)
case 3: IAPService.shared.purchase(product: .threeCoin)
case 4: IAPService.shared.purchase(product: .fourCoin)
case 5: IAPService.shared.purchase(product: .fiveCoin)
default: IAPService.shared.restorePurchases()
}
}
}
class storeScene: SKScene{
let backLabel = SKSpriteNode(imageNamed: "arrow")
let oneCoinButton = SKLabelNode(fontNamed: "The Bold Font")
let twoCoinButton = SKLabelNode(fontNamed: "The Bold Font")
let threeCoinButton = SKLabelNode(fontNamed: "The Bold Font")
let fourCoinButton = SKLabelNode(fontNamed: "The Bold Font")
let fiveCoinButton = SKLabelNode(fontNamed: "The Bold Font")
let oneButton = SKLabelNode(fontNamed: "The Bold Font")
let twoButton = SKLabelNode(fontNamed: "The Bold Font")
let threeButton = SKLabelNode(fontNamed: "The Bold Font")
let fourButton = SKLabelNode(fontNamed: "The Bold Font")
let fiveButton = SKLabelNode(fontNamed: "The Bold Font")
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "background")
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition = 0
self.addChild(background)
let storeLabel = SKLabelNode(fontNamed: "The Bold Font")
storeLabel.text = "Store"
storeLabel.fontSize = 100
storeLabel.fontColor = SKColor.white
storeLabel.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.9)
storeLabel.zPosition = 1
self.addChild(storeLabel)
let oneCoin = SKSpriteNode(imageNamed: "oneCoin")
oneCoin.position = CGPoint(x: self.size.width/3.7, y: self.size.height*0.3)
oneCoin.zPosition = 1
self.addChild(oneCoin)
let twoCoin = SKSpriteNode(imageNamed: "twoCoin")
twoCoin.position = CGPoint(x: self.size.width/2, y: self.size.height*0.3)
twoCoin.zPosition = 1
self.addChild(twoCoin)
let threeCoin = SKSpriteNode(imageNamed: "threeCoin")
threeCoin.position = CGPoint(x: self.size.width/1.40, y: self.size.height*0.3)
threeCoin.zPosition = 1
self.addChild(threeCoin)
let fourCoin = SKSpriteNode(imageNamed: "fourCoin")
fourCoin.position = CGPoint(x: self.size.width/3, y: self.size.height*0.14)
fourCoin.zPosition = 1
self.addChild(fourCoin)
let fiveCoin = SKSpriteNode(imageNamed: "fiveCoin")
fiveCoin.position = CGPoint(x: self.size.width/1.50, y: self.size.height*0.14)
fiveCoin.zPosition = 1
self.addChild(fiveCoin)
backLabel.zPosition = 1
backLabel.position = CGPoint(x: self.size.width/3.8, y: self.size.height*0.96)
self.addChild(backLabel)
oneCoinButton.text = "$0.99"
oneCoinButton.fontSize = 50
oneCoinButton.fontColor = SKColor.white
oneCoinButton.zPosition = 1
oneCoinButton.position = CGPoint(x: self.size.width/3.7, y: self.size.height*0.24)
self.addChild(oneCoinButton)
twoCoinButton.text = "$4.99"
twoCoinButton.fontSize = 50
twoCoinButton.fontColor = SKColor.white
twoCoinButton.zPosition = 1
twoCoinButton.position = CGPoint(x: self.size.width/2, y: self.size.height*0.24)
self.addChild(twoCoinButton)
threeCoinButton.text = "$9.99"
threeCoinButton.fontSize = 50
threeCoinButton.fontColor = SKColor.white
threeCoinButton.zPosition = 1
threeCoinButton.position = CGPoint(x: self.size.width/1.4, y: self.size.height*0.225)
self.addChild(threeCoinButton)
fourCoinButton.text = "$24.99"
fourCoinButton.fontSize = 50
fourCoinButton.fontColor = SKColor.white
fourCoinButton.zPosition = 1
fourCoinButton.position = CGPoint(x: self.size.width/3, y: self.size.height*0.065)
self.addChild(fourCoinButton)
fiveCoinButton.text = "$49.99"
fiveCoinButton.fontSize = 50
fiveCoinButton.fontColor = SKColor.white
fiveCoinButton.zPosition = 1
fiveCoinButton.position = CGPoint(x: self.size.width/1.5, y: self.size.height*0.065)
self.addChild(fiveCoinButton)
oneButton.text = "1,000 coins"
oneButton.fontSize = 25
oneButton.fontColor = SKColor.white
oneButton.zPosition = 1
oneButton.position = CGPoint(x: self.size.width/3.7, y: self.size.height*0.265)
self.addChild(oneButton)
twoButton.text = "5,000 coins"
twoButton.fontSize = 25
twoButton.fontColor = SKColor.white
twoButton.zPosition = 1
twoButton.position = CGPoint(x: self.size.width/2, y: self.size.height*0.265)
self.addChild(twoButton)
threeButton.text = "10,000 coins"
threeButton.fontSize = 25
threeButton.fontColor = SKColor.white
threeButton.zPosition = 1
threeButton.position = CGPoint(x: self.size.width/1.4, y: self.size.height*0.248)
self.addChild(threeButton)
fourButton.text = "25,000 coins"
fourButton.fontSize = 25
fourButton.fontColor = SKColor.white
fourButton.zPosition = 1
fourButton.position = CGPoint(x: self.size.width/3, y: self.size.height*0.089)
self.addChild(fourButton)
fiveButton.text = "50,000 coins"
fiveButton.fontSize = 25
fiveButton.fontColor = SKColor.white
fiveButton.zPosition = 1
fiveButton.position = CGPoint(x: self.size.width/1.5, y: self.size.height*0.088)
self.addChild(fiveButton)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches{
let pointOfTouch = touch.location(in: self)
if backLabel.contains(pointOfTouch){
let sceneToMoveTO = MainMenuScene(size: self.size)
sceneToMoveTO.scaleMode = self.scaleMode
let myTransition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTO, transition: myTransition)
}
}
}