Basic usage of ADBannerView

Source: Internet
Author: User

This example only focuses on how to use ADBannerView and does not have the relevant iAD settings and application process. For other mobile ads, you can refer to the mobile ads Banner! Make a lot of money! Article. First, import the iAD Framework and reference its header file. For more information about how to import the Xcode 4 Framework, see the new Framework method in Xcode 4. C code # import <iAd/iAd. h>, set the proxy on the category of the object to be used, and create an object of the ADBannerView type. C code @ interface AdBannerViewViewController: UIViewController {ADBannerView * bannerView;} in terms of functions, we hope that the ADBannerView can be displayed in the same way as the image (upright and tilting ), when ADBannerView is ready, the display is displayed automatically. You can also switch whether to display this ADBannerView. The code for initializing ADBannerView is as follows. C code // custom function Banner initialization (portrait on screen)-(void) initializeBanner {// set Banner to bannerView = [[ADBannerView alloc] initWithFrame: CGRectMake (0.0, 430.0, self. view. frame. size. width, 50.0)]; // The bannerView type supported by this Banner. requiredContentSizeIdentifiers = [NSSet setWithObjects: ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil]; // The current Banner type bannerView. currentContentSizeI Dentifier = ADBannerContentSizeIdentifierPortrait; // sets the proxy bannerView. delegate = self; // The advertisement bannerView cannot be triggered. userInteractionEnabled = NO; // you can specify the bannerView offset. frame = CGRectOffset (bannerView. frame, 0, 50); [self. view addSubview: bannerView];} Now we have an ADBannerView, but he cannot see it because of the Offset, therefore, the following code uses an animation to move the ADBannerView to the screen to produce a similar pop-up effect. C code-(void) bannerViewAnimation {// set the animation [UIView beginAnimations: @ "BannerViewAnimation" context: NULL]; // determine whether the bannerView is in the image by sorting the userInteractionEnabled status. if (bannerView. userInteractionEnabled) {bannerView. frame = CGRectOffset (bannerView. frame, 0, 50);} else {bannerView. frame = CGRectOffset (bannerView. frame, 0,-50);} // starts the animation [UIView commitAnimations]; // sets userInteractionEnabled in reverse order to bannerView. userIn TeractionEnabled =! BannerView. userInteractionEnabled;} the above Code uses the userInteractionEnabled parameter as a basis for judging whether the ADBannerView exists in the screen, and uses a similar switch technique, every time you execute this function, a reverse result is returned. We hope that ADBannerView will automatically pop up when it is ready, and users can switch it by themselves, so we must call the above function in the following two events. C code // user button event-(IBAction) onButtonPress :( id) sender {[self bannerViewAnimation];} // event triggered when ADBannerView completes ad reading-(void) bannerViewDidLoadAd :( ADBannerView *) banner {[self bannerViewAnimation];} The last function is available now, that is, the ADBannerView can be consistent with the display method of the screen (upright and tilting ), we can use the following function to reset the location and type of the ADBannerView. C code // built-in call function-(void) willRotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation duration :( NSTimeInterval) duration {// restore bannerView default value bannerView. userInteractionEnabled = NO; bannerView. frame = CGRectOffset (bannerView. frame, 0, 0); // determines whether the image is tilted if (UIInterfaceOrientationIsLandscape (toInterfaceOrientation) {bannerView. currentContentSizeIdentifier = ADBannerContentSizeIdentif IerLandscape; bannerView. frame = CGRectMake (0.0, 288.0, self. view. frame. size. width, 32.0);} else {bannerView. currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait; bannerView. frame = CGRectMake (0.0, 430.0, self. view. frame. size. width, 50.0);} // reset the offset bannerView. frame = CGRectOffset (bannerView. frame, 0, 50);} finally, do not forget to reference the ADBannerView initialization function at the program entry point and release its memory in the corresponding function. C code-(void) viewDidLoad {[super viewDidLoad]; [self initializeBanner];} C code-(void) dealloc {[bannerView release]; [super dealloc];} in addition, the following provides several built-in functions related to ADBannerView. C code // when an error occurs in ADBannerView, the-(void) bannerView :( ADBannerView *) banner didFailToReceiveAdWithError :( NSError *) is triggered *) error {} C code // The function triggered when an advertisement is pressed-(BOOL) bannerViewActionShouldBegin :( ADBannerView *) banner willLeaveApplication :( BOOL) willLeave {// place the code "return YES" for pausing your application here;} the function triggered when the return application button is pressed in C code // (void) bannerViewActionDidFinish :( ADBannerView *) banner {// here you can place the code to restart your application} ps: You must connect to the Internet to test ADBannerView. Network, sometimes the iAD used for testing will be unable to obtain the test advertisement due to busy connections and other issues, and may need to be executed several times.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.