how to get in app Purchase to work

| | August 10, 2015

I have a practice in app purchases the removes the iAds from the screen buy simple running a function called remove ads. all it does is set the practice/test iAd to a position that is off the screen the problem is that this is my first time doing in app purchases. i made the in app purchases an iTunes connect but i can get it to work here. does anybody now how to do a in app purchase? Thanks I’m using swift in Xcode and the GoHome SKlabel simple take you back to the home screen so don’t mind it. Im using SpritKit.

Code:

 import Foundation
 import AVFoundation
 import SpriteKit
 import GameKit
 import UIKit
 import StoreKit

 class ThirdScene: SKScene,SKStoreProductViewControllerDelegate,      SKProductsRequestDelegate, SKPaymentTransactionObserver{

let RemoveaddsButton = SKSpriteNode(imageNamed: "PlayAltenative")
let RestorePurchases = SKSpriteNode(imageNamed: "StopButton")
let GoHome = SKSpriteNode(imageNamed: "RedHomeButton")
var iAdLabel = SKLabelNode()
var RemoveAdvertisment = SKLabelNode()

    override func didMoveToView(view: SKView) {
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
   //-----------------------------------------------------------
      var iAdLabel = SKLabelNode(fontNamed: "OriyaSangamMN-Bold")
    iAdLabel.text = "I am An Advertisment"
    iAdLabel.fontSize = 20
    iAdLabel.zPosition = 20
    iAdLabel.fontColor = SKColor.blackColor()
    iAdLabel.position = CGPoint(x: self.frame.size.width/2, y:     self.frame.size.height * 0.70)
    self.addChild(iAdLabel)
   //--------------------------------------------------------
    self.RemoveaddsButton.position = CGPoint(x: self.frame.size.width *  0.70, y: self.frame.size.height * 0.20)
    RemoveaddsButton.zPosition = 100
    RemoveaddsButton.size = CGSize(width: 115, height: 170)
    self.addChild(self.RemoveaddsButton)
    //-----------------------------------------------------
self.RemoveAdvertisment.position = CGPoint(x: self.frame.size.width * 0.70, y: self.frame.size.height * 0.28)
     RemoveAdvertisment.text = "Remove Adds here"
     RemoveAdvertisment.zPosition = 100
     RemoveAdvertisment.fontSize = 15
    self.addChild(self.RemoveAdvertisment)
     //---------------------------------------------------------------------
    self.GoHome.position = CGPoint(x: self.frame.size.width * 0.80, y:  self.frame.size.height * 0.90)
    GoHome.zPosition = 100
    GoHome.size = CGSize(width: 115, height: 170)
    self.addChild(self.GoHome)
    //------------------------------
   // Set IAPS
    if(SKPaymentQueue.canMakePayments()) {
        println("Advertisement are enabled is enabled, loading")
        let productID:NSSet = NSSet(objects: "SkateLinesNolliePackage");
        var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as Set<NSObject>)
        request.delegate = self
        request.start()
    } else {
        println("please Buy Remove Advertisments")
    }
}
func removeAds() {
    CGPoint(x: self.frame.size.width/2, y: self.frame.size.height * 10.70)

        }

var list = [SKProduct]()
var p = SKProduct()


func buyProduct() {
    let payment: SKPayment = SKPayment(product: p)
    SKPaymentQueue.defaultQueue().addPayment(payment)

}
func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {
    println("Just Requested A product")
    var myProduct = response.products
    for product in myProduct {
        println("product added")
        println(product.productIdentifier)
        println(product.localizedTitle)
        println(product.localizedDescription)
        println(product.price)
        list.append(product as! SKProduct)
    }
}

func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {
    println("transactions are now restored")
    var purchasedItemIDS = []
    for transaction in queue.transactions {
        var t: SKPaymentTransaction = transaction as! SKPaymentTransaction
        let prodID = t.payment.productIdentifier as String
        switch prodID {
        case "SkateLine.iap.removeads":
            println("Now Removing ads")
            removeAds()
         default:
            println("Must buy remove ads")
        }

    }
}


func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
    println("add paymnet")
    for transaction:AnyObject in transactions {
        var trans = transaction as! SKPaymentTransaction
        println(trans.error)
        switch trans.transactionState {
         case .Purchased:
            println("buy, ok unlock Remove ads here")
            println(p.productIdentifier)
            let prodID = p.productIdentifier as String
            switch prodID {
            case "SkateLinesNolliePackage":
                println("removeing the ads now")
                removeAds()
            default:
                println("Buy Remove ads")
            }
            queue.finishTransaction(trans)
            break;
        case .Failed:
            println("buy error")
            queue.finishTransaction(trans)
            break;
        default:
            println("default")
            break;
        }
    }
}

func finishTransaction(trans:SKPaymentTransaction)
{
    println("finishing Transaction")
    SKPaymentQueue.defaultQueue().finishTransaction(trans)
}
func paymentQueue(queue: SKPaymentQueue!, removedTransactions transactions: [AnyObject]!)
{
    println("remove all trans");
}
func restoreyourpurchases() {
     println("Now Your purchases are restored")
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {

        let location = touch.locationInNode(self)
        if self.nodeAtPoint(location) == self.RemoveaddsButton {
            for product in list {
                var prodID = product.productIdentifier
                if(prodID == "SkateLinesNolliePackage") {
                    p = product
                    buyProduct()
                    break;
                }
            }
        }

        let location2 = touch.locationInNode(self)
            if self.nodeAtPoint(location) == self.RestorePurchases {
                restoreyourpurchases()

        }

        let location3 = touch.locationInNode(self)
            if self.nodeAtPoint(location) == self.GoHome {

                println(" PlayBtn Going to game play.")
                var scene = GameScene(size: self.size)
                let sKView = self.view! as SKView
                sKView.ignoresSiblingOrder = true
                scene.size = sKView.bounds.size
                let reveal = SKTransition.fadeWithDuration(0.45)
                sKView.presentScene(scene, transition: reveal)


  }
  }
  }
   }

Leave a Reply