3

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.
    }

}
K.K.D
  • 917
  • 1
  • 12
  • 27
  • 2
    In Pattern1, loadAdReward implementation in VAMPAdReward class, why is the "adReward.delegate = parentViewController as! VAMPDelegate" since your ViewController clearly doesn't comply to that protocol, and implementation of that delegate is in VAMPAdReward, so shouldn't it be the singleton. – Gokul G Mar 15 '18 at 07:51
  • 1
    I'm sorry I took mistake. As you said "adReward.delegate = self" is correct. I updated code. – K.K.D Mar 15 '18 at 07:54
  • 3
    i don't see adReward.load() in singleton method – Shehata Gamal Mar 15 '18 at 07:56
  • 1
    adReward.delegate = self is correct but adReward.setRootViewController(self) is wrong, it should be what it was before isnt adReward.setRootViewController(parentViewController), and as @Sh_Khan mentioned, .load() is never called – Gokul G Mar 15 '18 at 07:58
  • 1
    thank you for your answers. I fixed my code and it did work well!! Thank you very much and I'm sorry this question was too silly.... – K.K.D Mar 15 '18 at 08:11

2 Answers2

3

Follow these step for accomplish your singleton class

// MARK: - Singleton

final class Singleton {

    // Can't init is singleton
    private init() { }

    // MARK: Shared Instance

    static let shared = Singleton()

    // MARK: Local Variable

    var emptyStringArray : [String] = []

}
Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38
2

Correct method in singleton

func loadAdReward(parentViewController: UIViewController) {
    adReward = VAMP()
    adReward.setPlacementId("26812") //test ID
    adReward.delegate = self
    adReward.setRootViewController(parentViewController)
    adReward.load()
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87