1

I have integated Google IMA sdk to my project using cocoapods (pod 'GoogleAds-IMA-iOS-SDK', '~> 3.2.1'). I have a separate class called VideoPresenter to interact with the sdk. This presenter is a property of a collectionview cell called VideoCell which uses AVPlayer to play some video content (I am trying to add preroll videos to this video content). The ads loader always fails with the error message "IMA SDK loading timed out". How do I fix this? The following is the code for presenter:

#import "VideoAdPresenter.h"
#import <GoogleInteractiveMediaAds/GoogleInteractiveMediaAds.h>

@interface VideoAdPresenter () <IMAAdsLoaderDelegate,      IMAAdsManagerDelegate>
@property (nonatomic, strong) IMAAVPlayerContentPlayhead *contentPlayhead;
@property (nonatomic, strong) IMAAdsLoader *adsLoader;
@property (nonatomic, strong) IMAAdsManager *adsManager;

@end

NSString *const kTestAppAdTagUrl =
@"https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ct%3Dlinear&correlator=";


@implementation VideoAdPresenter

- (id)initWithAvPlayer:(AVPlayer *)anAvPlayer
{
    self = [super init];
    if(self) {
        self.contentPlayhead = [[IMAAVPlayerContentPlayhead alloc]initWithAVPlayer:anAvPlayer];
        [self setupAdsLoader];
    }
    return self;
}

- (void)setupAdsLoader
{
   self.adsLoader = [[IMAAdsLoader alloc] initWithSettings:nil];
   self.adsLoader.delegate = self;
}

- (void)requestAds {
   if(!self.videoAdTagUrl || [self.videoAdTagUrl isEqualToString:@""]) {
    NSLog(@"Nil or empty video ad tag url (%@) passed to video ad presenter: %@", self.videoAdTagUrl, [self description]);
    return;
}
    IMAAdDisplayContainer *adDisplayContainer =      [[IMAAdDisplayContainer alloc] initWithAdContainer:self.adContainerView companionSlots:nil];
    IMAAdsRequest *request = [[IMAAdsRequest alloc]initWithAdTagUrl:kTestAppAdTagUrl adDisplayContainer:adDisplayContainer contentPlayhead:self.contentPlayhead userContext:nil];

    [self.adsLoader requestAdsWithRequest:request];
}

#pragma mark - ads loader delegate

- (void)adsLoader:(IMAAdsLoader *)loader adsLoadedWithData: (IMAAdsLoadedData *)adsLoadedData {
    // Grab the instance of the IMAAdsManager and set ourselves as the delegate.
    self.adsManager = adsLoadedData.adsManager;
    self.adsManager.delegate = self;

    IMAAdsRenderingSettings *adsRenderingSettings =  [[IMAAdsRenderingSettings alloc] init];
    adsRenderingSettings.webOpenerPresentingController = self.presentingController;

    [self.adsManager    initializeWithAdsRenderingSettings:adsRenderingSettings];
}

- (void)adsLoader:(IMAAdsLoader *)loader failedWithErrorData: (IMAAdLoadingErrorData *)adErrorData {
    // Something went wrong loading ads. Log the error and play the   content.
    NSLog(@"Error loading ads: %@", adErrorData.adError.message);
    if(self.delegate && [self.delegate respondsToSelector:@selector(videoAdPresenterRequestedContentResume:)]) {
        [self.delegate videoAdPresenterRequestedContentResume:self];
    }
}




#pragma mark - ads manager delegate
- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdEvent: (IMAAdEvent *)event {
    // When the SDK notified us that ads have been loaded, play them.
    if (event.type == kIMAAdEvent_LOADED) {
        [adsManager start];
    }
}

- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdError:(IMAAdError *)error {
    // Something went wrong with the ads manager after ads were loaded. Log the error and play the
    // content.
    NSLog(@"AdsManager error: %@", error.message);
    if(self.delegate && [self.delegate respondsToSelector:@selector(videoAdPresenterRequestedContentResume:)]) {
        [self.delegate videoAdPresenterRequestedContentResume:self];
    }
}

- (void)adsManagerDidRequestContentPause:(IMAAdsManager *)adsManager   {
    // The SDK is going to play ads, so pause the content.
    if(self.delegate && [self.delegate respondsToSelector:@selector(videoAdPresenterRequestedContentPause:)]) {
        [self.delegate videoAdPresenterRequestedContentPause:self];
    }
}

- (void)adsManagerDidRequestContentResume:(IMAAdsManager *)adsManager {
    // The SDK is done playing ads (at least for now), so resume the content.
    if(self.delegate && [self.delegate respondsToSelector:@selector(videoAdPresenterRequestedContentResume:)]) {
        [self.delegate videoAdPresenterRequestedContentResume:self];
    }
}
@end
unosahj
  • 99
  • 5

1 Answers1

1

The reason was that the presenter was being invoked from a background thread. Doing the same from the main thread fixed the issue.

unosahj
  • 99
  • 5