开发者

how to add an adbannerview to a UISplitviewController

开发者 https://www.devze.com 2023-02-05 05:54 出处:网络
I have made an ipad application using an UISplitViewController, which functions in both orientation. Now I want to add banners to this application. How do I do this? In interface builder I can only ad

I have made an ipad application using an UISplitViewController, which functions in both orientation. Now I want to add banners to this application. How do I do this? In interface builder I can only add a portait banner to the detailView, which works more or less, but when I turn the iPad and click the banner it opens in portrait mode instead of landscape mode. And the banner can never get the prescribed width for ipad-landscape mode.

Trying to do it pro开发者_开发技巧grammatically, it tells me that the parent of the adbannerview should be a UIViewController.


This same problem drove me nuts for ages until I found the iAdSuite sample. So, to expand on Erran's answer: Use the iAdSuite sample code from Apple.

Get yourself a working split view app using storyboards.

Include the iAd Framework.

Copy the BannerViewController.h and .m files to your app. Then in AppDelegate.m in "application didFinishLaunching" copy the line from iAdSuite's AppDelegate as per the last line here:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
    UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
    splitViewController.delegate = (id)navigationController.topViewController;
    UINavigationController *masterNavigationController = splitViewController.viewControllers[0];

    _bannerViewController = [[BannerViewController alloc] initWithContentViewController:splitViewController];

In the section for iPhone you need this line:

_bannerViewController = [[BannerViewController alloc] initWithContentViewController:navigationController];

Just before the return statement add this

self.window.rootViewController = _bannerViewController;

Add this at the top of the .m

@implementation AppDelegate{
BannerViewController *_bannerViewController;}

#import "BannerViewController.h"

Or create the bannerViewController property any way you prefer.

Amend the .h as follows:

#import <UIKit/UIKit.h> 
@class BannerViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>

That was enough to get it all working. The whole split view app now operates inside the BannerView. That handles all the AdBannerDelegate functions.

Good luck :-)


In the iAdSuite Apple sample code there is a split view controller iAd implementation which you can easily add into your application. :^)


i've taken @ipwnstuff's answer a bit further.

first caveat: iAd only allows you to display in "portrait" or "landscape", and on iPad, this means 768x66 or 1024x66 respectively. this means there's no possibility of confining the ADBannerView to only the UISplitViewController detail view unless you want to roll your own that has a slightly wider view (and thus slightly narrower masterViewController view. i wanted to stick with storyboards, so i didn't want to go this route.

second caveat, the iAdSuite stuff @ipwnstuff pointed to is not immediately storyboard friendly. it creates the UISplitViewController programmatically, and you have to supply the master and detail either via .XIB or programmatically. since i had a working storyboard into which i wanted to integrate iAds, i wanted to extend that. also, the iAdSuite solution does not hide the master view in portrait mode, and i still wanted that.

so … starting an existing iPad.storyboard file, and then integrating with the SplitBanner sample from with iAdSuite as follows:

    UISplitViewController *splitViewController = (id)self.window.rootViewController;
    splitViewController.delegate = (id)splitViewController.detailUIViewController;
    CGRect splitViewFrame = splitViewController.view.frame;
        splitViewFrame.origin.y -= application.statusBarFrame.size.height;
        splitViewFrame.size.height += application.statusBarFrame.size.height;
    splitViewController.view.frame = splitViewFrame;

    // initWithContentViewController: the thing that's in the iAdSuite SplitViewBanner example
    self.bannerViewController
      = [[BannerViewController alloc] initWithContentViewController:splitViewController];
    self.window.rootViewController = self.bannerViewController;

i was thus able to use a storyboard UISplitViewController as the childViewController of the BannerViewController provided by iAdSuite.

ok, third caveat: there's one glitch, and that's that if you have the normal bar-button setting via splitViewControllerDelegate set up, the delegate won't get called if you rotate during the ad, and so the button will temporarily show up when it's not supposed to or not show up when it's supposed to.


At first it looks a bit like you forgot to uncomment or implement the shouldAutorotateToInterfaceOrientation variable... But I'm not quite sure. What happens when you tap on the banner? Does it open a new View and an UIWebView or something? Or something else? And when we are talking about AD banners right now, you should probably think about implement Apples iAd Service.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号