我在应用程序购买中练习从屏幕上删除iAds购买简单运行一个名为删除广告的功能。它只是将练习/测试iAd设置到屏幕外的位置,问题是这是我第一次在应用程序购买中。我在应用程序中购买了iTunes连接,但我可以在这里工作。现在有人怎么做应用程序购买?谢谢我在Xcode中使用swift,GoHome SKlabel简单地将您带回主屏幕,所以不要介意。我正在使用SpritKit。
代码:
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)
}
}
}
}
答案 0 :(得分:-1)
在此处查看我的问题和答案:My IAP isn't working. Bugs at func Paymentqueue
它包含用于移除广告的IAP的工作代码和一些提示。我希望能帮到你!
以下是完整的IAP代码:
import UIKit
import StoreKit
class GameViewController: UIViewController, ADBannerViewDelegate, SKProductsRequestDelegate, SKPaymentTransactionObserver, GKGameCenterControllerDelegate,GADBannerViewDelegate{
@IBOutlet var outRemoveAds: UIButton!
@IBOutlet var outRestorePurchases: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if NSUserDefaults.standardUserDefaults().objectForKey("val") != nil {
print("Has a value.")
banner.removeFromSuperview()
bannerGoogle.removeFromSuperview()
outRemoveAds.removeFromSuperview()
outRestorePurchases.removeFromSuperview()
removeInterFrom = 1
}
else {
print("No Value.")
}
if(SKPaymentQueue.canMakePayments()){
print("IAP is enabled, loading...")
let productID:NSSet = NSSet(objects:"IAP id")
let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
request.delegate = self
request.start()
}
else{
print("Please enable IAPS")
}
}
//IAP Ads
@IBAction func removeAds(sender: UIButton) {
for product in list{
let prodID = product.productIdentifier
if (prodID == "IAP id"){
p = product
buyProduct()
break
}
}
}
@IBAction func restorePurchases(sender: UIButton) {
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
//IAP Functions
var list = [SKProduct]()
var p = SKProduct()
func removeAds(){
banner.removeFromSuperview()
bannerGoogle.removeFromSuperview()
outRemoveAds.removeFromSuperview()
outRestorePurchases.removeFromSuperview()
let theValue = 10
NSUserDefaults.standardUserDefaults().setObject(theValue, forKey: "val")
NSUserDefaults.standardUserDefaults().synchronize()
}
func buyProduct(){
print("Buy: "+p.productIdentifier)
let pay = SKPayment (product: p)
SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
}
func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
print("Product Request")
let 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)
}
}
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("Add Payment")
for transaction:AnyObject in transactions{
let trans = transaction as! SKPaymentTransaction
print(trans.error)
switch trans.transactionState{
case .Purchased:
print("IAP unlocked")
print(p.productIdentifier)
let prodID = p.productIdentifier as String
switch prodID{
case "IAP id":
print("Remove Ads")
removeAds()
default:
print("IAP not setup")
}
queue.finishTransaction(trans)
break
case .Failed:
print ("Buy error")
queue.finishTransaction(trans)
break
default:
print("default: Error")
break
}
}
}
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
print("Purchases Restored")
_ = []
for transaction in queue.transactions {
let t: SKPaymentTransaction = transaction as SKPaymentTransaction
let prodID = t.payment.productIdentifier as String
switch prodID{
case "IAP id":
print("Remove Ads")
removeAds()
default:
print("IAP not setup")
}
}
}
func finishTransaction(trans:SKPaymentTransaction){
print("Finshed Transaction")
}
func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
print("Remove Transaction")
}
}
放:
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
在viewDidLoad或viewDidAppear
中