我一直在尝试在不使用 Storyboard或 Xib 文件的情况下使横幅广告出现在我的应用程序的底部。遗憾的是,这是行不通的。我已经尝试按照教程进行操作,只是没有添加 Storyboard ,但无济于事。我的项目编译正常,但广告没有显示在模拟器的底部,也没有显示在任何地方。这是我的 viewcontroller.mm 代码的相关示例:
@implementation ViewController
{
ADBannerView *_bannerView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:(_bannerView)];
self.canDisplayBannerAds = true;
}
header 导入,包含 iAd.framework。标题也有此修改行 @interface ViewController : UIViewController <ADBannerViewDelegate>
最佳答案
您没有初始化 bannerView
,如果您不初始化它,就像任何其他 UI 对象一样,您将看不到任何东西。因此首先对其进行初始化,然后将其添加到 View 中并启用 canDisplayBannerAds
以防止出现任何错误。
@implementation ViewController{
ADBannerView *_bannerView; }
- (void)viewDidLoad{
[super viewDidLoad];
self.canDisplayBannerAds = true;
//_bannerView = [[ADBannerView alloc]initWithFrame:CGRectMake(0,0,320,50)];
//Instead of giving the position when initializing, give the AdType
bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
//Then give the position
[view setFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:(_bannerView)];
}
如@Christophr 所说,还要在 iOS 测试设备上启用 iAd 填充率。
那应该行得通。 希望对您有所帮助。
关于ios - 使用 iAd 展示广告,不使用 Storyboard 或 Xib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30688857/