james Burns
james Burns

Reputation: 864

Correct way to implement iAd in iOs 7 iPhone app in iPad

I'm having a problem where my iAds are being zoomed up when shown on an iPad (simulator or real) in both the banner view and, if clicked on, in the iAd view. It works fine in the iPhone, just not in the iPad (when auto zoomed). enter image description here

The iPhone app itself is pretty simple, layout-wise, and only supports a portrait orientation.

I understand that currentContentSizeIdentifier is deprecated, but how does one deal with this in a post iOS 7 world? I've tried using

self.canDisplayBannerAds=YES:

... but haven't figured out how do set the delegate when using this method.

Here's the beginning of my viewDidLoad, where I add the iAd banner and position it to the bottom of the view.

 - (void)viewDidLoad
{
    [super viewDidLoad];
  //add iAd banner
    CGRect myView=self.view.frame;  //get the view frame
  //offset the banner to place it at bottom of view
    CGRect bannerFrame= CGRectOffset(myView, 0, myView.size.height);
  //Create the bannerView
    ADBannerView *adView = [[ADBannerView alloc] initWithFrame:bannerFrame];
  //yes, deprecated, but what to use now?
    adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    [self.view addSubview:adView];
    adView.delegate = self;
...

I appreciate your wisdom in this matter, as always...

Upvotes: 2

Views: 1405

Answers (1)

zero3nna
zero3nna

Reputation: 2918

Since iOS 6, Apple decided to make all iAds to fit the entire screen width. so you have no choice to change that any more. In your viewDidLoad use:

// On iOS 6 ADBannerView introduces a new initializer, use it when available

if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)])
{
    _bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
} else {
    _bannerView = [[ADBannerView alloc] init];
    _bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
}
_bannerView.delegate = self;
[self.view addSubview:_bannerView];

and to resize your view and give your banner the right position use:

- (void)viewDidLayoutSubviews {
   CGRect contentFrame = self.view.bounds, bannerFrame = CGRectZero;

   // All we need to do is ask the banner for a size that fits into the layout area we are using.
   // At this point in this method contentFrame=self.view.bounds, so we'll use that size for the layout.
   bannerFrame.size = [_bannerView sizeThatFits:contentFrame.size];

   if (_bannerView.bannerLoaded) {
      contentFrame.size.height -= bannerFrame.size.height;
      bannerFrame.origin.y = contentFrame.size.height;
   } else {
      bannerFrame.origin.y = contentFrame.size.height;
   }
   _contentView.frame = contentFrame;
   _bannerView.frame = bannerFrame;
}

Upvotes: 1

Related Questions