Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8161665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:32:50+00:00 2026-06-06T18:32:50+00:00

I have written a Singleton Class for managing iAds.The iAds pop up after 5

  • 0

I have written a Singleton Class for managing iAds.The iAds pop up after 5 seconds of the user inactivity. The idleTimerExceeded call generate a notification to show the iAd. This code works fine for my requirements but since I am new to iOS development, my application sometimes hangs unexpectedly after integrating this code. This code results in lots of warnings etc. I would like to optimize my code in terms of memory and performance.

I would be very thankful for your kind suggestions and reviews.

Below is my code:

iAdSingleton.h

#import <Foundation/Foundation.h>
#import "AppDelegate.h"
#import "iAd/iAd.h"

@interface iAdSingleton : UIViewController<ADBannerViewDelegate> {
    ADBannerView *adView;
    UIViewController *displayVC;
    NSTimer *idleTimer;
    BOOL isItFirstTime;
}
@property (nonatomic, retain) ADBannerView *adView;
@property (nonatomic, retain) UIViewController *displayVC;
@property (nonatomic) BOOL isItFirstTime;

+ (id) shareAdSingleton;
- (void) resetIdleTimer;
- (void) idleTimerExceeded;

@end

iAdSingleton.m

#import "iAdSingleton.h"

@implementation iAdSingleton

static iAdSingleton* _sharedAdSingleton = nil;

BOOL bannerVisible = NO;
BOOL controlAccessBannerVisibility = NO;
@synthesize adView, displayVC;
@synthesize isItFirstTime;

#define kMaxIdleTimeSeconds 5.0

+(id)sharedAdSingleton
{
    @synchronized(self)
    {
        if(!_sharedAdSingleton)
            _sharedAdSingleton = [[self alloc] init];
        return _sharedAdSingleton;
    }
    return nil;
}

+(id)alloc
{
    @synchronized([iAdSingleton class])
    {
        NSAssert(_sharedAdSingleton == nil, @"Attempted to allocate a second instance of a singleton.");
        _sharedAdSingleton = [super alloc];
        return _sharedAdSingleton;
    }

    return nil;
}

-(id)init
{
    self = [super init];
    if (self != nil) {

    /*                  Initialize The Parameters Over Here                  */

        //adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 480, 0, 0)];
        adView = [[ADBannerView alloc] init];
        adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
        self.adView.delegate=self;
        [self resetIdleTimer];
    }
    return self;
}

-(void)dealloc
{
    displayVC = nil;
    if (adView) {
        [adView removeFromSuperview]; //Remove ad view from superview
        [adView setDelegate:nil];
        adView = nil;
    }
    [super dealloc];
}

-(UIViewController *)viewControllerForPresentingModalView
{
    return displayVC;
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner 
{
    banner.hidden = NO;
    if(!bannerVisible){
        NSLog(@"Banner Changes 1 - Purpose: Visibility");
        // [UIView beginAnimations:@"bannerAppear" context:NULL];
        // banner.frame = CGRectOffset(banner.frame, 0, -100);
        // [UIView commitAnimations];
        bannerVisible = YES;
        controlAccessBannerVisibility = YES;

    }
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    //NSLog(@"Unable to receive Ad.");
    NSLog(@"Banner Changes 2 - Purpose: Unable to Receive Ad.");
    banner.hidden = YES;
    if(bannerVisible){
        [UIView beginAnimations:@"bannerDisappear" context:NULL];
        banner.frame = CGRectOffset(banner.frame, 0, 100);
        [UIView commitAnimations];
        bannerVisible = NO;
    }
}

- (BOOL) bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    NSLog(@"Pause anything necessary");
    return YES;
}

- (void) bannerViewActionDidFinish:(ADBannerView *)banner
{
    NSLog(@"We now resume to normal operations");
}

- (void)resetIdleTimer {

    if (!idleTimer) {
        idleTimer = [[NSTimer scheduledTimerWithTimeInterval:kMaxIdleTimeSeconds
                                                      target:self
                                                    selector:@selector(idleTimerExceeded)
                                                    userInfo:nil
                                                     repeats:NO] retain];
    }
    else {
        if (fabs([idleTimer.fireDate timeIntervalSinceNow]) < kMaxIdleTimeSeconds-1.0) {
            [idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:kMaxIdleTimeSeconds]];
            /*
             Notification: HideAd
             */

            NSLog(@"Notification Generated For HideAd");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"HideAdBanner" object:nil userInfo:nil];

        }
    }
}

- (void)idleTimerExceeded {

    AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    if (appDel.adVisible == NO) {
        NSLog(@"Notification Generated For ShowAd");

        /*
         Notification: ShowAd
         */

        if (controlAccessBannerVisibility == YES) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ShowAdBanner" object:nil userInfo:nil];
        }
    }
}

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:32:52+00:00Added an answer on June 6, 2026 at 6:32 pm

    This is what you need. This code is thread safe as well as will not have any memory issue and warnings.

    + (iAdSingleton *) sharedInstance
    {
        static dispatch_once_t onceToken;
        static iAdSingleton * __sharedInstance = nil;
    
        dispatch_once(&onceToken, ^{
            __sharedInstance = [[self alloc] init];
        });
    
        return __sharedInstance;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL singleton class written in PHP. Its code is listed below:
have written this little class, which generates a UUID every time an object of
I have some class which uses boost singleton. It calls some function from own
I have written a custom SessionStoreProvider class that inherits from the SessionStateStoreProviderBase. I have
I have written a class (AbcdBase) which holds several static objects, these include maps
I have written a simple server - client program with Swing interface using singleton
Have written all the code in a silverlight class library (dll) and linked this
I have written a small class Dice that imitates the behavior of real dice
I have a large application written in native C++. I also have a class
I have written a bash script which installs a number of packages, however for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.