0

New to iOS development and I have a tabbed app with 4 tabs. I have an iAd showing on one tab and I don't want to regurgetate the code on every view controller and I know this is not the correct way to implement anyway. I have looked at the Apple code here and I'm struggling to be honest.

https://developer.apple.com/library/ios/samplecode/iAdSuite/Introduction/Intro.html

I have included the BannerViewController.m and BannerViewController.h in my application but I'm not fully sure how to get it to run the ad on each view controller.

Yours confused

JKX

rmaddy
  • 314,917
  • 42
  • 532
  • 579
JKX
  • 177
  • 2
  • 15
  • add the banner to UIWindow – Rajan Maheshwari Apr 25 '15 at 18:46
  • Thanks for the reply, can you elaborate slightly. where do I add it to UI window? – JKX Apr 25 '15 at 18:51
  • currently you might be adding the banner view by writing the code self.view addSubview I guess..... If you add anything on UIWindow which has a reference made in appdelegate then that view will appear forever and you dont have to rewrite any code in various viewcontrollers separately. – Rajan Maheshwari Apr 25 '15 at 18:53
  • you can take reference of UIWindow in any of the view controllers or even in appDelegate and add the banner to UIWindow. – Rajan Maheshwari Apr 25 '15 at 18:55
  • I had self.view addSubview what is the method for UIWindow – JKX Apr 25 '15 at 18:56
  • i have just posted in detail – Rajan Maheshwari Apr 25 '15 at 19:04
  • JKX, I wrote a tutorial on iAd implementation and use this for one of my apps with a tab bar. You might want to check it out. http://thefizixgroup.blogspot.com/p/iad-implementation_6.html?m=1 – Douglas Apr 25 '15 at 23:03
  • @Douglas thank you, although not the perfect answer for the question it did put me on the right track to achieving my goal! – JKX Apr 26 '15 at 08:57
  • Glad it got you going in the right direction!! – Douglas Apr 26 '15 at 12:29
  • Here's an example of a Shared Banner implemented in Swift. http://stackoverflow.com/a/28639200/2108547 It's easily translated to Obj-C. – Daniel Storm Apr 26 '15 at 13:42

1 Answers1

0

Just grab a reference of UIWindow in any of your ViewController Class Go to .m of your ViewController

#import "AppDelegate.h"
@interface ViewController ()
{
  //make object of AppDelegate
    AppDelegate *appDelegate;
}

Now in your viewDidLoad or ViewDidAppear grab the reference of window.

appDelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate.window addSubview:yourBannerView];
Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98
  • 1
    Doesn't work ..throws following error 'NSInternalInconsistencyException', reason: 'ADBannerView must be part of a view hierarchy managed by a UIViewController' – JKX Apr 25 '15 at 19:07
  • just do one thing make any simple UIView and add it to the window just like the above code and see whether that uiview is available on all view controllers – Rajan Maheshwari Apr 25 '15 at 19:09
  • I'm trying to implement the shared ad using the BannerViewController.m and BannerViewController.h, have you any answer for the question as described above? – JKX Apr 25 '15 at 19:15