gpt4 book ai didi

swift - 应用程序内购买在 Swift 2 中有效,但现在我已更新到 Swift 3 和 Xcode 8

转载 作者:行者123 更新时间:2023-11-30 12:58:47 26 4
gpt4 key购买 nike

正如标题所示,我有一个应用内购买代码,用于删除在 Swift 2 中运行的广告,然后更新到 Swift 3 并更新了代码。它会向 Apple 发送付款请求,但不会运行在 func buyProduct() 中调用的 func paymentQueue(_queue: SKPaymentQueue, UpdatedTransactions transactions: [SKPaymentTransaction]) 。代码如下:

//  MainScene.swift
// Contain
//
// Created by Phil Javinsky III on 9/16/16.
// Copyright © 2016 Phil Javinsky III. All rights reserved.
//

import SpriteKit
import StoreKit

let defaults = UserDefaults.standard
var noAds: Bool = false
var fetched: Bool = false
var product = SKProduct()
var productID = "containRemoveAds"
var readyForIAP: Bool = false

class MainScene: SKScene, SKProductsRequestDelegate, SKPaymentTransactionObserver {
var touchLocation: CGPoint = CGPoint.zero
var start, leaderboards, rate, removeAds: SKSpriteNode!

override func didMove(to view: SKView) {
SKPaymentQueue.default().add(self)
if defaults.bool(forKey: "purchased") {
print("Already purchased")
bannerAd.isHidden = true
noAds = true
}
else {
print("Not purchased")
if !fetched {
print("Fetching")
getProductInfo()
fetched = true
}
}

start = self.childNode(withName: "start") as! SKSpriteNode
leaderboards = self.childNode(withName: "leaderboards") as! SKSpriteNode
leaderboards.position = CGPoint(x: 0, y: -1150)
rate = self.childNode(withName: "rate") as! SKSpriteNode
rate.position = CGPoint(x: 0, y: -1350)
removeAds = self.childNode(withName: "removeAds") as! SKSpriteNode
removeAds.position = CGPoint(x: 0, y: -1550)

SKPaymentQueue.default().remove(self)
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
touchLocation = touches.first!.location(in: self)

if start.contains(touchLocation) {
let game: GameScene = GameScene(fileNamed: "GameScene")!
game.scaleMode = .aspectFit //.resizeFill?
view?.presentScene(game, transition: SKTransition.fade(withDuration: 1))
}
else if leaderboards.contains(touchLocation) {
//showLeaderboard()
}
else if rate.contains(touchLocation) {
/*let url = NSURL(string: "itms-apps://itunes.apple.com/us/app/whack-a-diglett/id1141187647?ls=1&mt=8")

if UIApplication.shared.canOpenURL(url! as URL) {
UIApplication.shared.openURL(url! as URL)
}*/
}
else if removeAds.contains(touchLocation) {
let alert = UIAlertController.init(title: "Remove Ads", message: "Remove ads or restore a previous purchase.", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Remove Ads", style: .default, handler:
{ action -> Void in
print("Remove Ads")
if readyForIAP {
self.buyProduct()
}
else {
print("Not ready for IAP")
let alert = UIAlertController.init(title: "Error", message: "Something went wrong, try again.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
self.view?.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
}))
alert.addAction(UIAlertAction(title: "Restore Purchase", style: .default, handler:
{ action -> Void in
print("Restore purchase")
if (SKPaymentQueue.canMakePayments()) {
SKPaymentQueue.default().restoreCompletedTransactions()
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.view?.window?.rootViewController?.present(alert, animated: true, completion: nil)

}
}

func getProductInfo() {
//print("About to fetch the products")

// Check if allowed to make the purchase
if SKPaymentQueue.canMakePayments() {
let productIdentifier: NSSet = NSSet(object: productID)
let productsRequest: SKProductsRequest = SKProductsRequest(productIdentifiers: productIdentifier as! Set<String>)
productsRequest.delegate = self
productsRequest.start()
//print("Fetching Products")
}
else {
print("can't make purchases")
}
}

func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
var products = response.products
if (products.count != 0) {
product = products[0]
print(product.localizedTitle)
print(product.localizedDescription)
print(product.price)
readyForIAP = true
print("Ready for IAP")
}
else {
print("Product not found")
}
}

func buyProduct() {
print("Sending payment request to Apple")
let payment = SKPayment(product: product)
SKPaymentQueue.default().add(payment as SKPayment)
}

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("Received payment transaction response from Apple")
for transaction in transactions {
switch transaction.transactionState {
case .purchased:
print("Product Purchased")
defaults.set(true, forKey: "purchased")
bannerAd.isHidden = true
noAds = true
SKPaymentQueue.default().finishTransaction(transaction)
case .failed:
print("Purchased Failed")
SKPaymentQueue.default().finishTransaction(transaction)
case .restored:
print("Restored")
let alert: UIAlertController = UIAlertController(title: "Restored", message: "Purchase restored, ads removed.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
self.view?.window?.rootViewController?.present(alert, animated: true, completion: nil)
defaults.set(true, forKey: "purchased")
bannerAd.isHidden = true
noAds = true
SKPaymentQueue.default().finishTransaction(transaction)
default:
print("DEFAULT")
break
}
}
}

func request(_ request: SKRequest, didFailWithError error: Error) {
print("Error Fetching product information");
}

}

最佳答案

为什么要删除 didMoveToView 中的事务观察器?这没有任何意义。

在didMoveToView中添加事务观察器

 SKPaymentQueue.default().add(self)

并且仅在您的应用关闭时将其删除。

也许将 IAP 代码移动到另一个帮助器类或其他类中也是一个好主意,这样你的代码很难阅读。

希望这有帮助

关于swift - 应用程序内购买在 Swift 2 中有效,但现在我已更新到 Swift 3 和 Xcode 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40098442/

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