Changing canDisplayBannerAds to NO does not update the view to remove ad banner

1.2k views Asked by At

Weird question, and I've been bannging my head against this wall for hours and haven't been able to solve it.

I have a iOS 7 only app that uses storyboards. On my main root view controller, I display an iAd banner at the bottom of the view using self.canDisplayBannerAds = YES. This works perfectly.

I have a settings view controller, and from there, my users can make an in app purchase to remove the ads permanently. That functionality works as well. I store a BOOL value in NSUserDefaults standardUserDefaults to track if they've made the purchase.

What is stumping me is that if a user is receiving ads in the banner, goes into the settings view controller, then to my purchase view controller, makes the purchase successfully, then returns to the main root view, the banner ad remains visible. If the user completely quits the app, then relaunches, it doesn't show up again, becuase this is how I display the banner in viewWillAppear:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
bool saved = [defaults boolForKey:t_Save];

if (saved == NO) {
    self.canDisplayBannerAds = YES;
} else if (saved == YES) {
    self.canDisplayBannerAds = NO;
}
NSLog(self.canDisplayBannerAds ? @"YES" : @"NO");

So, this logic works fine when they re-launch the app after they've made or restored the purchase. But, I want the view to update right after they make the purchase. However, I can't for the life of me get that stupid banner ad to disappear in the app when it was there when they first launched it.

What is weird if that if I create a button that just sets self.canDisplayBannerAds = NO, then that DOES make the banner ad disappear. But it doesn't work when called in viewWillAppear, viewDidAppear, or even in a dedicated method I created that get's called once the purchase is made.

I was thinking that maybe the banner view is setting in a cache somewhere and it just isn't getting nil'ed out, but I don't know how to force that. I was also thinking maybe if I could force the root view controller to basically go to nil and start over from scratch, that might do it, but I don't know how to force that either.

Anyone have any thoughts?

2

There are 2 answers

3
Christian On BEST ANSWER

I had the same problem and found a workaround that worked for me:

self.canDisplayBannerAds = NO;
for (id aView in self.view.subviews) {
  if ([aView isKindOfClass:[ADBannerView class]]) {
     [(ADBannerView *)aView setHidden:YES];
  }
}

I don't think this is a clean solution but since it is finaly working i will keep it in until i find a better solution.

1
LawrencePires On

I would create a method in the implementation file (.m) that controls the advert, also include the method header to the .h "- (void) viewWillAppear;"

- (void)viewWillAppear
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
bool saved = [defaults boolForKey:t_Save];

if (saved == NO) {
    self.canDisplayBannerAds = YES;
} else if (saved == YES) {
    self.canDisplayBannerAds = NO;
}
NSLog(self.canDisplayBannerAds ? @"YES" : @"NO");
}

once the purchase is confirmed call the method from other class,

example;

AppDelegate *Appdel = [[AppDelegate alloc] init];
[Appdel viewWillAppear];