We were closing in on the release of a new iPhone app, when we realized that the landscape orientation really isn't suited well for an advertisement. If you've dabbled with AdMob, you know by know that there's no such thing as a full-width landscape ad. AdMob serves the add as if it was designed for landscape view, leaving it up to you to position it how you like.

There are some tips on this throughout the web, but as a PhoneGap user, I started to wonder how some users would know where to "drop" the code.

Basically, we are overriding existing methods provided by the app framework. So, although you won't see a willAnimateRotationToInterfaceOrientation method in your MainViewController.m, you can simply add it to override the default.

I've pasted some code below which does a few things:

  1. Forces ads to the bottom
  2. Hides the status bar in landscape mode
  3. Puts ads in "test" mode
  4. Sets the preferred gender for ad targeting to female
  5. Hides ads in landscape mode
  6. Demonstrates how to position ads in landscape mode (commented out)

 

- (void) webViewDidFinishLoad:(UIWebView*) theWebView
{
    /* existing code */

    bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0,
                                            self.view.frame.size.height -
                                            GAD_SIZE_320x50.height,
                                            GAD_SIZE_320x50.width,
                                            GAD_SIZE_320x50.height)];
    bannerView_.adUnitID = MY_BANNER_UNIT_ID;
    bannerView_.rootViewController = self;
    [self.view addSubview:bannerView_];
   
    GADRequest *request = [GADRequest request];
    request.testing = YES;
    request.gender = kGADGenderFemale; // until we have a login with gender info

    // use this if you want to show landscape ads, and center them at the bottom
    //[bannerView_ setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin];
    [bannerView_ loadRequest:request];
   

    /* existing code */
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)newInterfaceOrientation duration:(NSTimeInterval)duration {
    BOOL hide = (newInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || newInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
    [[UIApplication sharedApplication] setStatusBarHidden:hide withAnimation:UIStatusBarAnimationNone];
    CGRect mainFrame = [[UIScreen mainScreen] applicationFrame];
    [self.view setFrame:mainFrame];

    if (newInterfaceOrientation != UIInterfaceOrientationLandscapeLeft && newInterfaceOrientation != UIInterfaceOrientationLandscapeRight) {
        bannerView_.frame=CGRectMake(0.0,
                                         self.view.frame.size.height - GAD_SIZE_320x50.height,
                                         GAD_SIZE_320x50.width,
                                         GAD_SIZE_320x50.height);
    }

        /* keep ads hidden in landscape view for now
        bannerView_.frame=CGRectMake(0.0,
                                         0.0,
                                         480.0,
                                         GAD_SIZE_320x50.height);
        */
}

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *