gpt4 book ai didi

xcode - 如何让应用程序购买工作

转载 作者:行者123 更新时间:2023-11-28 09:28:10 25 4
gpt4 key购买 nike

我在应用程序购买中有一个实践,即从屏幕购买中删除 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)


}
}
}
}

最佳答案

在这里查看我关于 IAP 的问题和答案: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 中

关于xcode - 如何让应用程序购买工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31930024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com