简体   繁体   中英

How do I detect IOS app close and load only?

I am trying to add some RevMob code into my app, the app currently has IAP in it and I am looking to add some advertising (fullscreen).

Now I currently have my code placed in the following method

- (void)applicationDidBecomeActive:(UIApplication *)application {
RevMobFullscreen *fullscreen;
fullscreen = [[RevMobAds session] fullscreen];
fullscreen.delegate = self;
[fullscreen loadAd];
[fullscreen showAd];
}

The problem is that this causes the advert to appear far too often. Ideally I only want to call the advert when the app first loads and then when the user closes the app and then reopens it (standby mode).

The above code is calling the advert code when the user also interacts with the IAP because the alert box is opened asking whether the user wants to buy.

Any Suggestions?

"Ideally I only want to call the advert when the app first loads and then when the user closes the app and then reopens it (standby mode)."

Implement the following methods of the UIApplicationDelegate :

  • application:didFinishLaunchingWithOptions:

As I'm sure you are well aware, this method fires once when the app is first launched. We have to implement this method because the following method is not called on first launch.

  • applicationWillEnterForeground:

From the documentation:

This method is called as part of the transition from the background to the active state.

This method is only called when the application moves from a background state . Unlike the applicationDidBecomeActive: method you were experiencing problems with applicationWillEnterForeground: is not called after brief interruptions such as dismissing an incoming SMS or phone call or interacting with In-App Purchases.

Code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...

    [self showAd];

    return YES;
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [self showAd];
}

- (void)showAd
{
    RevMobFullscreen *fullscreen;
    fullscreen = [[RevMobAds session] fullscreen];
    fullscreen.delegate = self;
    [fullscreen loadAd];
    [fullscreen showAd];
}

Use

application:didFinishLaunchingWithOptions:

for the initial launch of the app.

For the coming from backward feature record the date time like so

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    self.timeEnteredBackground = [NSDate date];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    NSDate* now = [NSDate date];
    double diff = [now timeIntervalSinceDate:timeEnteredBackground];
    if (diff > SOME_SECONDS) 

This way the ad will only appear after a background time greater than some threshold.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM