ios iad 和 admob 集成导致内存泄漏?

发布于 2025-01-06 12:57:30 字数 2421 浏览 0 评论 0原文

我使用此代码,如果 iad 加载失败,它会查找 admob。一切似乎都工作正常,除了在仪器中,我注意到每当调用 admob 时就会出现很大的内存峰值。在多次通过仪器进行测试后,我只遇到了一次内存泄漏,我很确定这是在调用 admob 时发生的。我看到有些人谈论 admob 的内存泄漏,但我不确定这是否已修复。

我的代码看起来不错吗?如果是这样,希望这对某人有帮助,但我最终可能会从我的应用程序中删除 admob,因为一段时间后它似乎会大大减慢程序速度。我也没有意识到sdk接近8mb。

-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    if (!self.bannerIsVisible) {
        [bannerView_ removeFromSuperview];
        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
        banner.frame = CGRectOffset(banner.frame, 0.0, -50.0);
        [UIView commitAnimations];
        self.bannerIsVisible = YES;

    }
}

-(void)callAdMob {
    // Create a view of the standard size at the bottom of the screen.
    bannerView_ = [[GADBannerView alloc]
                   initWithFrame:CGRectMake(0.0,
                                            self.view.frame.size.height -
                                            GAD_SIZE_320x50.height,
                                            GAD_SIZE_320x50.width,
                                            GAD_SIZE_320x50.height)];

    // Specify the ad's "unit identifier." This is your AdMob Publisher ID.
    bannerView_.adUnitID = @"";

    // Let the runtime know which UIViewController to restore after taking
    // the user wherever the ad goes and add it to the view hierarchy.
    bannerView_.rootViewController = self;
    [self.view addSubview:bannerView_];

    // Initiate a generic request to load it with an ad.
    [bannerView_ loadRequest:[GADRequest request]];


}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    if (self.bannerIsVisible) {
        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
        banner.frame = CGRectOffset(banner.frame, 0.0, 50.0);
        [UIView commitAnimations];
        self.bannerIsVisible = NO;
        NSLog(@"bannerview did not receive any banner due to %@", error);
        [self callAdMob];

    }
}
- (void)viewDidLoad
{

        [super viewDidLoad];
    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    adView.frame = CGRectOffset(adView.frame, 0.0, 367.0);
    adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
    adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    [self.view addSubview:adView];
    adView.delegate = self;
    self.bannerIsVisible = NO;
}

I use this code where if an iad fails to load it looks for an admob. Everything appears to work fine except in instruments I have noticed a large memory spike anytime an admob gets called. After putting this through instruments multiple times I only got a memory leak once which im pretty sure occurred when an admob was called. I have seen some people talk about memory leaks with admob but i wasn't sure if this was fixed or not.

Does my code look good? If so hopefully this helps someone out but I may end up taking admob out of my app cause it seems to drastically slow down the program after awhile. Also I did not realize the sdk is close to 8mb.

-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    if (!self.bannerIsVisible) {
        [bannerView_ removeFromSuperview];
        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
        banner.frame = CGRectOffset(banner.frame, 0.0, -50.0);
        [UIView commitAnimations];
        self.bannerIsVisible = YES;

    }
}

-(void)callAdMob {
    // Create a view of the standard size at the bottom of the screen.
    bannerView_ = [[GADBannerView alloc]
                   initWithFrame:CGRectMake(0.0,
                                            self.view.frame.size.height -
                                            GAD_SIZE_320x50.height,
                                            GAD_SIZE_320x50.width,
                                            GAD_SIZE_320x50.height)];

    // Specify the ad's "unit identifier." This is your AdMob Publisher ID.
    bannerView_.adUnitID = @"";

    // Let the runtime know which UIViewController to restore after taking
    // the user wherever the ad goes and add it to the view hierarchy.
    bannerView_.rootViewController = self;
    [self.view addSubview:bannerView_];

    // Initiate a generic request to load it with an ad.
    [bannerView_ loadRequest:[GADRequest request]];


}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    if (self.bannerIsVisible) {
        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
        banner.frame = CGRectOffset(banner.frame, 0.0, 50.0);
        [UIView commitAnimations];
        self.bannerIsVisible = NO;
        NSLog(@"bannerview did not receive any banner due to %@", error);
        [self callAdMob];

    }
}
- (void)viewDidLoad
{

        [super viewDidLoad];
    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    adView.frame = CGRectOffset(adView.frame, 0.0, 367.0);
    adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
    adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    [self.view addSubview:adView];
    adView.delegate = self;
    self.bannerIsVisible = NO;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撑一把青伞 2025-01-13 12:57:30

当您在分析器中运行它时,泄漏是否被列为 GeneralBlock-1024 和 GeneralBlock-56?如果是这种情况,听起来像是 iOS 中已经存在了一段时间的 UIWebView 泄漏。它似乎与发出 HTTP 请求或通过 HTTP 发出 XML 请求有关。

苹果应该在 iOS 5 中修复这个问题,但它仍然存在于以前的 iOS 版本中。

When you run it in the profiler, is the leak listed as GeneralBlock-1024 and GeneralBlock-56? If this is the case, sounds like a UIWebView leak that's been in iOS a while. It seems to be tied to making HTTP requests, or XML requests over HTTP.

Apple should have fixed this in iOS 5 but it still exists in previous versions of iOS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文