奖励视频广告在我的标签上显示奖励内容
Rewarded Video ad show reward content on my label
我正在开发一款带有 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)
}
}
您可以将货币值保存到 UserDefaults,然后在需要时检索或更新它,示例如下 link:
PS!如果您不想将字符串保存到 userDefaults,您也可以保存数字。
我正在开发一款带有 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)
}
}
您可以将货币值保存到 UserDefaults,然后在需要时检索或更新它,示例如下 link:
PS!如果您不想将字符串保存到 userDefaults,您也可以保存数字。