In iOS 4.2, I programmatically created a window, a view controller, a UIView, and two subheadings: EAGLView and ADBannerView. My code is a combination of the standard EAGLView code and Apple BasicAdBanner.
I can see iAd banners, but I can not "click" on them - on the device or in the simulator. The view controller implements ADBannerDelegate and receives bannerViewDidLoadAd messages, etc., but never receives a bannerViewActionShouldBegin message.
The behavior does not change, replacing the EAGL view with a UITextView or only with an ADBannerView, so I assume that my problem is with programmatically creating a window or view controller. Any help would be appreciated.
In addition, my views do not rotate or change automatically, as an example of an advertising banner, but this is a less pressing problem.
The code:
AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.userInteractionEnabled=YES;
window.multipleTouchEnabled=YES;
window.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
window.autoresizesSubviews = YES;
controller = [GLViewController alloc];
window.rootViewController = controller;
[window addSubview:controller.view];
glView = [controller.glView retain];
[window makeKeyAndVisible];
return YES;
}
ViewController:
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
}
-(void)viewDidLoad
{
[super viewDidLoad];
glView = [[EAGLView alloc] initWithFrame:[UIScreen mainScreen].bounds];
glView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
glView.userInteractionEnabled=NO;
[self.view addSubview:glView];
if (self.banner==NULL)
[self createADBannerView];
[self layoutForCurrentOrientation:NO];
}
-(void)createADBannerView
{
NSString *contentSize;
if (&ADBannerContentSizeIdentifierPortrait != nil)
{
contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation)
? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifierLandscape;
}
else
{
contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation)
? ADBannerContentSizeIdentifier320x50 : ADBannerContentSizeIdentifier480x32;
}
CGRect frame;
frame.size = [ADBannerView sizeFromBannerContentSizeIdentifier:contentSize];
frame.origin = CGPointMake(0.0f, 0.0f);
ADBannerView *bannerView = [[ADBannerView alloc] initWithFrame:frame];
bannerView.delegate = self;
bannerView.hidden = YES;
bannerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
bannerView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil];
self.banner = bannerView;
[self.view addSubview:bannerView];
[bannerView release];
}
-(void)layoutForCurrentOrientation:(BOOL)animated
{
CGRect contentFrame = self.view.bounds;
CGPoint bannerOrigin = CGPointMake(CGRectGetMinX(contentFrame), CGRectGetMaxY(contentFrame));
CGFloat bannerHeight = 0.0f;
if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
banner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
else
banner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
bannerHeight = banner.bounds.size.height;
if(banner.bannerLoaded)
{
banner.hidden = NO;
contentFrame.size.height -= bannerHeight;
bannerOrigin.y -= bannerHeight;
NSLog(@"Banner will be %f x %f, content resized to %f x %f\n", banner.frame.size.width,bannerHeight,contentFrame.size.width,contentFrame.size.height);
}
else
{
banner.hidden = YES;
NSLog(@"Banner will be offscreen, content resized to %f x %f\n", contentFrame.size.width,contentFrame.size.height);
}
[UIView animateWithDuration:animationDuration
animations:^{
glView.frame = contentFrame;
[glView layoutIfNeeded];
banner.frame = CGRectMake(bannerOrigin.x, bannerOrigin.y, banner.frame.size.width, banner.frame.size.height);
}];
}
ADBannerViewDelegate:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[self layoutForCurrentOrientation:YES];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"Failed to receive and ad: %s %s %s\n",error.localizedFailureReason.UTF8String, error.localizedDescription.UTF8String, error.localizedRecoverySuggestion.UTF8String);
[self layoutForCurrentOrientation:YES];
}
-(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
NSLog(@"banner clicked: will %s application\n",willLeave?"leave":"cover");
return YES;
}