I made a game that have iAd interstitial using Xcode 7 beta and SpriteKit in Swift 2, I tried to use a function to remove them but It's not working. I'm using two different files, GameScene.swift and GameViewController.swift.
Code that I used in file, 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()
}
In GameViewController.swift is In-App Purchase to remove ads (to buy Pro Version):
@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")
}
If You read the code in file GameViewController.swift function removeAds()
is empty and is called in some codes in same file, so it will remove ads forever,and what I need to do is to put a code in function removeAds()
that will remove ads permanently, the problem is what I don't know how to call it and in which way to remove them, because functions are in different files, I tried many ways but doesn't work.
Can You show me please with more details how to do that ?