I'm trying to 开发者_开发百科display iAds in my application that is built against the iOS 4.1 SDK, but I am unable to see these ads in the iPhone Simulator. In the videos that I have seen about integrating iAds in applications, the OS used was iOS 4.0.
Is there a problem with iOS 4.1 for displaying iAds, or what else could be going wrong in my application?
iAd is for iOS 4.0 and above, so 4.1 should be fine.
Did you miss one of the step below...
- Add iAd.framework to your project
- #import <iAd/iAd.h> in your view controller .h file
- Drag and drop iAdBannerView to your interface
- Link adView variable and delegate using the interface builder
- Implement the delegate bannerViewDidLoadAd that show the ad
- Implement the delegate didFailToReceiveAdWithError that hide the ad
You can see the code below and adapt to your own need...
// RootView.h
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
@interface RootView : UIViewController {
BOOL bannerIsVisible;
ADBannerView *adView;
}
@property (nonatomic) BOOL bannerIsVisible;
@property (nonatomic, retain) IBOutlet ADBannerView *adView;
@end
// RootView.m
#pragma mark -
#pragma mark iAd Banner
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
YourAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
UINavigationController *navigationController = (UINavigationController *)[appDelegate navigationController];
if (self.bannerIsVisible == NO) {
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[navigationController view].frame = CGRectMake(0, 0, 320, 410);
self.bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
YourAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
UINavigationController *navigationController = (UINavigationController *)[appDelegate navigationController];
if (self.bannerIsVisible) {
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[navigationController view].frame = CGRectMake(0, 0, 320, 460);
self.bannerIsVisible = NO;
}
}
精彩评论