5

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?

János
  • 32,867
  • 38
  • 193
  • 353
  • I'm having a similar problem. After making the purchase (or restoring the purchase), on the view controller where the purchase (or restore) was done, the ads keep appearing. HOWEVER, the view has been resized back to the size *without* the banner ad. So, in my case the scroll view doesn't scroll quite enough now. On the other view controllers in may app that have ads, they are disabled correctly with the purchase. I've also seen that if the ad isn't *currently* being displayed at the time of the purchase, then the problem doesn't occur. – Chris Prince Jan 31 '14 at 22:26

2 Answers2

6

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.

Christian
  • 149
  • 5
  • Hmmm. Now, after various changes to my app, I find I don't need this work-around. Just setting self.canDisplayBannerAds to NO causes the currently displayed banner ad to animate off screen in the suitable manner. It's annoying that I can't see what changed. – Chris Prince Feb 07 '14 at 16:46
  • Christian, you rock. I finally got some time to test your solution, and it works. Thanks! – KyleAlbert95 Feb 08 '14 at 01:09
  • I also could not find any other solution or explanation as to why this was occurring. Your fix has worked perfectly. Great code. – Rhys Lewis Feb 01 '15 at 17:32
0

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];
János
  • 32,867
  • 38
  • 193
  • 353
LawrencePires
  • 88
  • 1
  • 5
  • 1
    Hi, thanks, but I'm not sure I understand ... you are suggesting I call the viewWillAppear method from my purchasing view? But the problem isn't that the method isn't getting called -- when I pop back to the original view, the viewWillAppear method get's called. It's just that the ad banner doesn't go away. I'm not sure how your suggestion would make any difference. – KyleAlbert95 Dec 18 '13 at 21:23