我在Swift 2中制作了一个使用Xcode 7 beta和SpriteKit的iAd插页式游戏,我尝试使用一个函数来移除它们,但它不起作用。我使用两个不同的文件,GameScene.swift和GameViewController.swift。我在文件中使用如何从Swift 2中使用SpriteKit和Xcode 7 beta应用程序中删除iAd?
代码,GameScene.swift:
func gameOver() {
isGameOver = true
print("Game Over")
loadAd()
}
//iAd
func close(sender: UIButton) {
closeButton.removeFromSuperview()
interAdView.removeFromSuperview()
}
func loadAd() {
print("load ad")
interAd = ADInterstitialAd()
interAd.delegate = self
closeButton.frame = CGRectMake(15, 15, 22, 22)
closeButton.layer.cornerRadius = 11
closeButton.setTitle("x", forState: .Normal)
closeButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
closeButton.backgroundColor = UIColor.whiteColor()
closeButton.layer.borderColor = UIColor.blackColor().CGColor
closeButton.layer.borderWidth = 1
closeButton.addTarget(self, action: "close:", forControlEvents: UIControlEvents.TouchDown)
}
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
print("ad did load")
interAdView = UIView()
interAdView.frame = self.view!.bounds
view!.addSubview(interAdView)
interAd.presentInView(interAdView)
UIViewController.prepareInterstitialAds()
interAdView.addSubview(closeButton)
}
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
}
func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
print("failed to receive")
print(error.localizedDescription)
closeButton.removeFromSuperview()
interAdView.removeFromSuperview()
}
在GameViewController.swift是在应用程序内购买去除广告(购买Pro版本):
@IBAction func removeAds(sender: UIButton) {
print("Remove Ads Button pressed")
for product in list {
let prodID = product.productIdentifier
if(prodID == "Squares.RemoveAds") {
p = product
buyProduct()
break;
}
}
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
func removeAds() {
}
override func prefersStatusBarHidden() -> Bool {
return true
}
//Remove Ads Payment
var list = [SKProduct]()
var p = SKProduct()
//Squares.regular.removeAds
//Squares.6Plus.removeAds
func buyProduct() {
print("Buy" + p.productIdentifier)
let pay = SKPayment(product: p)
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
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)
}
removeAdsButton.enabled = true
removeAdsIPhone6Plus.enabled = true
}
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
print("Transactions Restored")
var purchasedItemIDS = []
for transaction in queue.transactions {
let t: SKPaymentTransaction = transaction as SKPaymentTransaction
let prodID = t.payment.productIdentifier as String
switch prodID {
case "Squares.RemoveAds":
print("Remove Ads")
removeAds()
case "Squares.RemoveAds":
print("Remove Ads for iPhone 6 Plus")
removeAds()
default:
print("IAP not setup")
}
}
}
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("Buy, Ok unlock Squares here")
print(p.productIdentifier)
let prodID = p.productIdentifier as String
switch prodID {
case "Squares.RemoveAds":
print("Remove Ads")
removeAds()
case "Squares.RemoveAds":
print("Remove Ads for iPhone 6 Plus")
removeAds()
default:
print("IAP not Setup")
}
queue.finishTransaction(trans)
break;
case .Failed:
print("Buy Error")
queue.finishTransaction(trans)
break;
default:
print("Default")
break;
}
}
}
func finishTransaction(trans:SKPaymentTransaction){
print("Finish Transaction")
}
func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
print("Remove Transaction")
}
如果您阅读文件中的代码,GameViewController.swift函数removeAds()
为空,并在同一个fi中的某些代码中调用le,所以它会永远删除广告,我需要做的是将功能removeAds()
中的代码永久删除广告,问题是我不知道如何调用它以及以何种方式删除它们,因为函数在不同的文件中,我尝试了很多方法,但不起作用。 你能告诉我更多的细节如何做到这一点?
现在我将清理意见,现在,很高兴它为你工作。未来还有一件事你可能会考虑将数据保存在Keychain而不是NSUserDefaults中,尤其是在应用程序购买等方面。否则,如果您拥有越狱设备,则很容易解锁。这是我使用的帮手,它非常简单。 https://github.com/jrendel/SwiftKeychainWrapper – crashoverride777
这里是我用于Game Center的代码,我也有一些问题。 http://stackoverflow.com/questions/32145090/how-to-make-high-score-of-game-to-be-saved-on-leaderboard-with-swift 检查答案呢! 如果你想添加我在Facebook上这是它: https://www.facebook.com/Emin.Rma.Emini.Im.Madridista.A.JoCcKeR.Rma – Emm
太棒了。我想你应该真的只是得到我的全部帮手,因为你已经使用了一半。我还会说约20-30%的下载来自不支持iAd的国家。另外,iAds有时会失败,所以你总是把Google作为备份。它非常值得,特别是像我们这样的独立开发者。它可能会让你最多30分钟设置你的谷歌帐户等我做了一步一步的指示,所以应该没有问题。 https://github.com/crashoverride777/Swift-iAds-and-AdMob-Helper – crashoverride777