Hey everyone, so iv been creating a game and the game is finally finished, i have added iAds and have made a button that allows users to remove the adds for 1.29, i have done the devaloper portal side of things and have watched many a tutorials on how to add the in app purchase functionality. I want to charge the user and then run the function removeAdvertisements which stores a proUser variable in NSUserdefault as true, then when i add the ads it checks if proUser is equal to true and then hides the add. Anyway the problem i am having is when i press the remove ads button it does not work. Any and all help will be helpful!!!!!!! BTW IM USING SWIFT 2 AND IOS 9
import Foundation
import SpriteKit
import AVFoundation
import StoreKit
class OptionsScene: SKScene, SKProductsRequestDelegate, SKPaymentTransactionObserver {
let removeAdsText = SKLabelNode(fontNamed: "Bebas Neue Bold")
let removeAds = SKSpriteNode(imageNamed: "resetHighscoreBtn")
let removeAdsComplete = SKLabelNode(fontNamed: "Bebas Neue Bold")
var backgroundMusic = AVAudioPlayer()
override func didMoveToView(view: SKView) {
removeAdsText.text = "Remove Ads"
removeAdsText.fontColor = UIColor.whiteColor()
removeAdsText.fontSize = 60
removeAdsText.position = CGPointMake(self.frame.size.width/2, self.frame.size.height*0.55)
removeAdsText.zPosition = 100
self.addChild(removeAdsText)
removeAds.size = CGSize(width: 300, height: 80)
removeAds.position = CGPointMake(self.frame.size.width/2, self.frame.size.height*0.45)
self.addChild(removeAds)
removeAdsComplete.text = "Thank you ads removed. Restart App"
removeAdsComplete.fontColor = UIColor.whiteColor()
removeAdsComplete.fontSize = 20
removeAdsComplete.position = CGPointMake(self.frame.size.width/2, self.frame.size.height*0.35)
removeAdsComplete.hidden = true
self.addChild(removeAdsComplete)
if(SKPaymentQueue.canMakePayments()) {
print("IAP is enabled, loading")
var productID: NSSet = NSSet(objects: "com.test.example")
var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
request.delegate = self
request.start()
} else {
print("Device IAP Gone")
}
}
var list = [SKProduct]()
var p = SKProduct()
func buyProduct() {
print("Buy" + p.productIdentifier)
var pay = SKPayment(product: p)
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
}
func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
print("Request the IAPS Products")
var myProduct = response.products
for product in myProduct {
print("product added")
print(product.productIdentifier)
print(product.localizedTitle)
print(product.localizedDescription)
print(product.price)
list.append(product as SKProduct)
}
print("Remove ads now avaliable")
}
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("Add Payment")
for transaction: AnyObject in transactions {
var trans = transaction as! SKPaymentTransaction
print(trans.error)
switch trans.transactionState {
case .Purchased:
print("bought, unlock ads")
print(p.productIdentifier)
let prodID = p.productIdentifier as String
switch prodID {
case "com.test.example":
removeAdvertisements()
default:
print("IAP Not Setup")
}
queue.finishTransaction(trans)
break
case .Failed:
print("Buy Error")
queue.finishTransaction(trans)
break
default:
print("default")
break
}
}
}
func finishTransaction(trans: SKPaymentTransaction) {
print("Finish Trans")
}
func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
print("Remove Trans")
}
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
print("Transactions restored")
var purchasedItemIDS = []
for transaction in queue.transactions {
var t: SKPaymentTransaction = transaction as SKPaymentTransaction
let prodID = t.payment.productIdentifier as String
switch prodID {
case "com.test.example":
print("Remove Ads")
default:
print("IAP Not setup")
}
}
}
func removeAdvertisements() {
let proUser = NSUserDefaults.standardUserDefaults()
proUser.setBool(true, forKey: "proUser")
proUser.synchronize()
removeAdsComplete.hidden = false
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == resetHightscore {
GameScene().resetHighscore()
resetComplete.hidden = false
}
if self.nodeAtPoint(location) == removeAds {
print("print")
for product in list {
var prodID = product.productIdentifier
print(product.productIdentifier)
if(prodID == "com.test.example") {
p = product
buyProduct()
break;
}
}
}
if self.nodeAtPoint(location) == backBtn {
backgroundMusic.stop()
let goMenuScene = MenuScene(size: self.size)
goMenuScene.scaleMode = scaleMode
let transition = SKTransition.fadeWithDuration(1)
self.view?.presentScene(goMenuScene, transition: transition)
}
}
}
}