I want to use Singleton to show ads, but it doesn't work well. When I don't use Singleton and use only ViewController, it works well.(can through "vampLoadStart" and "vampDidReceive")
How can I solve it?
Pattern1: when I use Singleton (can't load and show ad)
VAMPAdReward.swift
import Foundation
import UIKit
import VAMP
class VAMPAdReward: NSObject,VAMPDelegate{
static let sharedInstance = VAMPAdReward()
var adReward:VAMP!
override init() {
super.init()
}
func loadAdReward(parentViewController: UIViewController) {
adReward = VAMP()
adReward.setPlacementId("26812") //test ID
adReward.delegate = self
adReward.setRootViewController(self)
}
func showAdReward(){
if adReward.isReady() {
print("show ad")
adReward.show()
}else{
print("couldn't show ad")
}
}
func vampLoadStart(_ placementId: String!, adnwName: String!) {
print("start loading")
}
func vampDidReceive(_ placementId: String!, adnwName: String!) {
print("finished loading")
}
}
ViewController
import UIKit
class ViewController: UIViewController {
var adReward: VAMPAdReward!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
VAMPAdReward.sharedInstance.loadAdReward(parentViewController: self)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
//when touch screen, show Ad
VAMPAdReward.sharedInstance.showAdReward()
}
}
Pattern2: when I don't use Singleton (can load and show ad)
import UIKit
import VAMP
class ViewController: UIViewController, VAMPDelegate {
var ad: VAMP!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
//load ad
ad = VAMP()
ad.setPlacementId("59755") //test ID
ad.delegate = self
ad.setRootViewController(self)
ad.load()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
ad.show()
}
func vampLoadStart(_ placementId: String!, adnwName: String!) {
print("start loading") //through
}
func vampDidReceive(_ placementId: String!, adnwName: String!) {
print("finished loading") //through
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}