Reputation: 171
i made a app with Xcode (swift) and i have implemented AdMob interstitial ads in the app but the only way i know to show the ad is click the button and ad will show, i want to show ads in webView without any button click , like when someone is scrolling down after some seconds ad will pop up. is thats possible ?
this is whats i am currently using
var interstital: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
interstital = GADInterstitial(adUnitID: "")
let request = GADRequest()
interstital.load(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Ad(_ sender: Any) {
if (interstital.isReady) {
interstital.present(fromRootViewController: self)
interstital = CreateAd()
}
}
func CreateAd() -> GADInterstitial {
let interstital = GADInterstitial (adUnitID: "")
interstital.load(GADRequest())
return interstital
}
Upvotes: 0
Views: 1865
Reputation: 904
Using the timer, here is the implementation for the objective c. Here new random timer will fire within 20sec to 200sec once the add is closed.
self.interstitial = [self createAndLoadInterstitial];
double time = (arc4random() % 200) + 20;
[NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(showAdIfReady) userInfo:nil repeats:NO];
- (void)showAdIfReady {
if (self.interstitial.isReady) {
[self.interstitial presentFromRootViewController:self];
} else {
NSLog(@"Ad wasn't ready");
}
}
- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
self.interstitial = [self createAndLoadInterstitial];
double time = (arc4random() % 200) + 20;
[NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(showAdIfReady) userInfo:nil repeats:NO];
}
Upvotes: 0
Reputation: 1213
To do this you can use Timers, which allow you to execute a function,CreateAd()
in your case, after an amount of time. In the example below I make it display an interstitial ad after 20-200 seconds. To change the minimum and maximum interval just change those numbers. I also removed the part which redeclares interstitial each time as setting it once in viewDidLoad
is enough.
class YourViewController: UIViewController {
var interstital: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
interstital = GADInterstitial(adUnitID: "")
let request = GADRequest()
interstital.load(request)
let randomTime = Double(arc4random() + 20).truncatingRemainder(dividingBy: 200.0)
Timer.scheduledTimer(timeInterval: randomTime, target: self, selector: #selector(YourViewController.CreateAd), userInfo: nil, repeats: false)
}
func CreateAd() -> GADInterstitial {
if (interstital.isReady) {
interstital.present(fromRootViewController: self)
let randomTime = Double(arc4random() + 20).truncatingRemainder(dividingBy: 200.0)
Timer.scheduledTimer(timeInterval: randomTime, target: self, selector: #selector(YourViewController.CreateAd), userInfo: nil, repeats: false)
}
}
}
I had to nest the timers instead of setting repeats
to true to make the interval random each time.
Upvotes: 1