SwiftyStoreKit IAP购买,但在启动后不记得它的设置



我试图在我的游戏中整合一个非常基本的单一IAP,这里我从我的GameScene呼叫

let alert = UIAlertController(title: "Upgrade", message: "Would you like to remove ads?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Remove Ads", style: .default, handler: { action in
print("Pressed Remove Ads")

GameViewController().buytheIAP()

}))

alert.addAction(UIAlertAction(title: "Restore Purchases", style: .default, handler: { action in
print("Pressed Restore")
GameViewController().restoretheIAP()
}))

alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { action in
print("Pressed Cancel")
}))

view?.window?.rootViewController?.present(alert, animated: true, completion: nil)
}

这些方法被正确调用,并在GameViewController.swift中引用它们;

func buytheIAP(){

iAPHelper.purchase()
print("OK Lets upgrade")
}

func restoretheIAP(){

iAPHelper.restorePurchase()
print("OK Lets restore")
}

func restoreDidSucceed() {

UserDefaults.setValue(true, forKey: iAPHelper.productID)
//this should have something like hide banner etc.
bannerView.isHidden = true
}

func purchaseDidSucceed() {
UserDefaults.setValue(true, forKey: iAPHelper.productID)
//as above this should have something like hide banner etc.

bannerView.isHidden = true

print("Purchased upgrade ENJOYYYYYYYY")

}

func nothingToRestore() {

}

func paymentCancelled() {

}

测试IAP通过了,它从应用商店获得了正确的信息,我使用我的沙盒细节进行购买,它正确地通过了一个成功的购买信息。然而,bannerView并没有隐藏,更重要的是,当重新开始游戏时,一切都被遗忘了,游戏认为没有购买任何东西。我猜一定是缺少了某种检查。

我有这个在我的viewDidLoad

if userDefaults.bool(forKey: iAPHelper.productID) {

bannerView.isHidden = true
print("It is purchased, so DO NOT show the ads")
} else{

bannerView.adSize = getAdaptiveSize()
bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
bannerView.delegate = self
bannerView.rootViewController = self
bannerView.load(GADRequest())
addBannerViewToView(bannerView)

print("Not purchased, so show the ads")

}

总是显示print("Not purchased, so show the ads")

用于购买的IAPHelper文件为;

func purchase() {

SwiftyStoreKit.purchaseProduct(productID, quantity: 1, atomically: true) { [self] result in
switch result {
case .success:

delegate?.purchaseDidSucceed()
print("OK It's purchased")

case .error(let error):
switch error.code {
case .unknown: print("Unknown error. Please contact support")
case .clientInvalid: print("Not allowed to make the payment")
case .paymentCancelled:

delegate?.paymentCancelled()

case .paymentInvalid: print("The purchase identifier was invalid")
case .paymentNotAllowed: print("The device is not allowed to make the payment")
case .storeProductNotAvailable: print("The product is not available in the current storefront")
case .cloudServicePermissionDenied: print("Access to cloud service information is not allowed")
case .cloudServiceNetworkConnectionFailed: print("Could not connect to the network")
case .cloudServiceRevoked: print("User has revoked permission to use this cloud service")
default: print((error as NSError).localizedDescription)

}
}
}
}
}

和日志显示print("OK It's purchased")最初购买后-所以我正在努力看看是什么出了问题。

IAP委托函数不会(保证)在UI/主线程上被调用,这就是为什么你的视图不会隐藏的原因。

你没有看到一些iOS警告说,你试图设置一个UIView属性在非主线程?

你的自定义购买信息没有保存在UserDefaults中,这可能是由于Xcode过早地杀死了应用程序。

(UserDefaults确实采取保存在正常应用程序流程中设置的内容。)

好的,所以我找到了答案,在我的AppDelegate.swift部分didFinishLaunchingWithOptions

缺少一行
UserDefaults.standard.register(defaults: ["adRemove" : false])

我重命名userDefaults.bool(forKey: iAPHelper.productID),使其更容易使用/理解。所以在我原来的帖子中,它已经被UserDefaults.standard.register(defaults: ["adRemove" : false])代替了。

你可以在任何地方使用它来检查,比如;

if !userDefaults.bool(forKey: "adRemove") {
// Do something here
}

希望这对将来遇到同样问题的人有所帮助!

最新更新