奖励视频广告展示奖励标签上的内容

时间:2019-07-14 11:35:51

标签: swift xcode admob nsuserdefaults admob-rewardedvideoad

我只是在开发带有admob奖励视频广告的应用。 我想知道用户何时单击按钮来奖励他们。 我做了这个,但是我的问题是,当我第二次点击播放广告按钮时,我当前的(货币在标签上)没有加上新的按钮,而没有获得新的100(当前+ 100)。 我只是想使我的(当前)货币(+ 100)。 你们可以帮忙吗请......

struct Keys {
    static var currency = "Currency"
    static var currentCurr = "CurrentCurrency"
}

class ViewController: UIViewController, GADRewardBasedVideoAdDelegate {

    var currency = 0
    var userDefault = UserDefaults.standard

    @IBOutlet weak var currencyShower: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()

        GADRewardBasedVideoAd.sharedInstance().load(GADRequest(), withAdUnitID: "ca-app-pub-3940256099942544/1712485313")
        GADRewardBasedVideoAd.sharedInstance().delegate = self

        if userDefault.string(forKey: Keys.currency) != nil {
            currencyShower.text = userDefault.string(forKey: Keys.currency)
        } else {
            return
        }
    }
    //
    @IBOutlet weak var playAdOut: UIButton!
    @IBAction func playAd(_ sender: UIButton) {
        if GADRewardBasedVideoAd.sharedInstance().isReady == true {
            GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)
        } else {
            print("Video Did not Load...")
        }
    }

    func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        print("Video is Ready")
        GADRewardBasedVideoAd.sharedInstance().load(GADRequest(),
                                                    withAdUnitID: "ca-app-pub-3940256099942544/1712485313")
    }
    func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didFailToLoadWithError error: Error) {
        print("Error When Loading")
    }
    func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didRewardUserWith reward: GADAdReward) {
        currency += 100
        currencyShower.text = String(currency)
    }
}

1 个答案:

答案 0 :(得分:0)

您可以将货币值保存到UserDefaults中,然后在需要的示例中通过以下链接进行检索或更新:example

PS!如果您不想将字符串保存到userDefaults中,则还可以保存数字。