此过程分为两个部分,在应用内购买以移除广告和在应用内购买后删除所有视图中的所有广告。我已经完成了In App Purchase to Remove Ads,当我使用Sandbox Tester进行测试时,它可以正常工作,并且它的工作原理非常好。我需要在应用购买后实施删除所有视图的建议。
我尝试的选项是使用 static 为我的布尔值实现全局变量,可以在其他视图控制器中使用。这些只是我实现这些功能的代码的一部分。
RemoveAdsViewController.swift
static var adRemovalPurchased = false
@IBAction func removeAdButton(_ sender: Any) {
purchase(purchase: adRemoval)
}
func purchase(purchase: RegisteredPurchase){
NetworkActivityIndicatorManager.NetworkOperationStarted()
SwiftyStoreKit.purchaseProduct(bundleID + "." + purchase.rawValue, completion:{
result in
NetworkActivityIndicatorManager.networkOperationFinished()
if case .success(let product) = result {
if product.needsFinishTransaction{
SwiftyStoreKit.finishTransaction(product.transaction)
RemoveAdsViewController.adRemovalPurchased = true
print("Turning Banner off")
}
self.showAlert(alert: self.alertForPurchaseResult(result: result))
}
})
}
DifferentViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
if (RemoveAdsViewController.adRemovalPurchased == true) {
bannerView.isHidden = true
print("There is no banner")
} else {
bannerView.rootViewController = self
bannerView.load(GADRequest())
print("There is a banner!")
}
}
请注意,所有代码都有效。我用沙盒测试仪购买了removeAds,它应该给出adRemovalPurchase = true的布尔值。但它只是没有识别DifferentViewController.swift中的布尔值,它仍然显示广告。
我使用的是SwiftyStoreKit。
感谢您的时间!谢谢。
答案 0 :(得分:0)
@IBAction func removeAdButton(_ sender: Any) {
purchase(purchase: adRemoval)
}
func purchase(purchase: RegisteredPurchase){
NetworkActivityIndicatorManager.NetworkOperationStarted()
SwiftyStoreKit.purchaseProduct(bundleID + "." + purchase.rawValue, completion:{
result in
NetworkActivityIndicatorManager.networkOperationFinished()
if case .success(let product) = result {
if product.needsFinishTransaction{
SwiftyStoreKit.finishTransaction(product.transaction)
UserDefaults.standard.set(true, forKey: "IsAdRemoved")
UserDefaults.standard.synchronize()
print("Turning Banner off")
}
self.showAlert(alert: self.alertForPurchaseResult(result: result))
}
})
}
OtherViewControllers:
if let isAdRemoved = UserDefaults.standard.object(forKey: "isAdRemoved") {
if isAdRemoved as! Bool == true {
bannerView.isHidden = true
print("There is no banner")
} else {
bannerView.rootViewController = self
bannerView.load(GADRequest())
print("There is a banner!")
}
} else {
bannerView.rootViewController = self
bannerView.load(GADRequest())
print("There is a banner!")
}
答案 1 :(得分:0)
RemoveAdsViewController.swift
func purchase(purchase: RegisteredPurchase){
NetworkActivityIndicatorManager.NetworkOperationStarted()
SwiftyStoreKit.purchaseProduct(bundleID + "." + purchase.rawValue, completion:{
result in
NetworkActivityIndicatorManager.networkOperationFinished()
if case .success(let product) = result {
if product.needsFinishTransaction{
SwiftyStoreKit.finishTransaction(product.transaction)
}
self.showAlert(alert: self.alertForPurchaseResult(result: result))
}
let adRemovalPurchased = UserDefaults.standard
adRemovalPurchased.set(true, forKey: "adRemoved")
adRemovalPurchased.synchronize()
print (adRemovalPurchased.bool(forKey: "adRemoved"))
print("Turning Banner off")
})
}
DifferentViewController.swift
let adRemovalPurchased = UserDefaults.standard
if !adRemovalPurchased.bool(forKey: "adRemoved") {
bannerView.rootViewController = self
bannerView.load(GADRequest())
print("There is a banner!")
} else {
bannerView.isHidden = true
print("There is no banner")
}
我重新审视了这个问题,发现你需要在所有视图控制器中声明 let adRemovalPurchased = UserDefaults.standard 的变量。这段代码有效!