简体   繁体   English

iAd无法加载时不会隐藏吗?

[英]iAd doesn't hide when it fails to load?

This seems like the dumbest question ever, but after wading through all of Apple's documentation and the useless online tutorials, I still can't figure out how to properly implement iAds into my application. 这似乎是有史以来最愚蠢的问题,但是在仔细阅读了所有Apple文档和无用的在线教程之后,我仍然不知道如何在应用程序中正确实现iAds So, my app starts off in a table view controller, and I have an iAd object underneath the navigation bar and above the table. 因此,我的应用程序在表格视图控制器中启动,并且在导航栏下方和表格上方都有一个iAd对象。

Now, in my code: (I also have the iAd framework added) 现在,在我的代码中:(我还添加了iAd框架)

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>

@interface MasterTableViewController : UITableViewController <ADBannerViewDelegate>
{
    IBOutlet ADBannerView *iAd;
}
@property(nonatomic, readonly, getter=isBannerLoaded) BOOL bannerLoaded;
@end

then in the .m file 然后在.m文件中

#import "MasterTableViewController.h"

@interface MasterTableViewController ()
@end

@implementation MasterTableViewController
@synthesize bannerLoaded;

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    if (!willLeave)
    {
        // nothing in this case thanks to ARC 
    }
    return YES;
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    iAd.hidden = NO;
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{   
    iAd.hidden = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    for (int i = 0; i > 0; i++)
    {
        if (bannerLoaded)
        {
            iAd.hidden = NO;
        }
        else
        {
            iAd.hidden = YES;
        }
    }
}

Now, the problem is, when I test the app without internet connection the iAd does not load (obviously) BUT it also does not hide. 现在,问题是,当我在没有互联网连接的情况下测试应用程序时, iAd无法加载(显然),但是它也不会隐藏。 So, at the top of the screen I'm left with a big white rectangle. 因此,在屏幕顶部,我剩下了一个大的白色矩形。 Otherwise, the ad works fine when a connection is available. 否则,当连接可用时,广告将正常运行。 Does anyone have any ideas? 有人有什么想法吗? Also - I just added the endless loops to see if they made a difference, those were completely on purpose lol. 另外-我只是添加了无限循环,看看它们是否有所作为,这些完全是故意的。

I'm assuming you have added your ADBannerView via the Storyboard as I can't see where you initialise the banner position. 我假设您已通过情节提要添加了ADBannerView,因为我看不到初始化横幅位置的位置。

  1. In storyboard, set the initial location just off screen. 在情节提要中,将初始位置设置为不在屏幕上。
  2. In "bannerViewDidLoadAd", animate the banner into view. 在“ bannerViewDidLoadAd”中,将横幅设置为动画。
  3. In "bannerView: didFailToReceiveAdWithError:", animate the banner out of view. 在“ bannerView:didFailToReceiveAdWithError:”中,使横幅动画离开视线。

There is a good example here https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/iAd_Guide/WorkingwithBannerViews/WorkingwithBannerViews.html 这里有一个很好的例子https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/iAd_Guide/WorkingwithBannerViews/WorkingwithBannerViews.html

Hope this helps. 希望这可以帮助。 I have just implemented this but don't have access to my code at the moment. 我刚刚实现了此功能,但目前无法访问我的代码。

It does bring up some errors in the simulator but works fine on a device. 它的确在模拟器中带来了一些错误,但在设备上运行良好。

I have tested this on iOS6 and works ok even if an iAd is displayed then the user loses the connection (so it looks like "bannerView: didFailToReceiveAdWithError:" is being called for me). 我已经在iOS6上对此进行了测试,即使显示了iAd,然后用户也失去了连接(所以看起来像“ bannerView:didFailToReceiveAdWithError:”正在为我调用),它可以正常工作。

You have a property bannerLoaded for saving the state of your Ad, which is good. 您有一个属性bannerLoaded用于保存广告状态,这很好。

At the very first, in your viewDidLoad method, iAd can't be loaded, so you have to set your property accordingly : self.bannerLoaded = NO; 首先,在您的viewDidLoad方法中,无法加载iAd,因此您必须相应地设置属性: self.bannerLoaded = NO;

Then, when you receive / fail to receive your ad, you need to update this property. 然后,当您收到/未能收到您的广告时,您需要更新此属性。

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    if (!self.bannerLoaded) {
        iAd.hidden = NO;
        self.bannerLoaded = YES;
    }
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{   
    if (self.bannerLoaded) {
        iAd.hidden = YES;
        self.bannerLoaded = NO;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM