I cannot get either AdMob or iAd banner ads to work in an iOS ActionExtension or ShareExtension -- has anyone gotten either of these to successfully work, and if so, how? Both AdMob and iAd work fine in the "container" portion of the ActionExtension app (running in either the Simulator or on an actual device), but neither seem to work in the Extension. (Running in the Extension portion of the app, none of the events are fired and no ad appears but there is no sign of any errors, testing in either iOS 8 or iOS 9.)
(Note that I'm not trying to run these both together; I started with AdMob and couldn't get that to work so I replaced it with iAd, but no luck there either.)
If neither of these work, has anyone successfully used any ad-based component in an ActionExtension or ShareExtension? (Unfortunately, all the functionality of this app is in the Extension portion so the only chance to monetize anything is in the Extension portion.)
Update 1:
I also tried AdMob Interstitial ads, but same problem. Objects get instantiated, but events never get fired.
Latest iteration of code samples:
Note that while the first code samples are in C# using Xamarin.iOS, the tiny bit of information I've found via massive Googling shows similar questions (most without answers, unfortunately) in Objective-C and in Swift, so the problem seems to be in the Extension handling itself rather than in the Xamarin wrappers. Also, both AdMob SDK v7.2.2 and v7.5.0 showed the same problem.
"Update #2" contains code samples in Objective-C -- these also fail in the exact same way.
iAd C# Version:
public partial class ActionViewController : UIViewController {
ADBannerView _adBannerView;
public override void ViewDidLoad () {
base.ViewDidLoad ();
// Hook in the iAd component
_adBannerView = new ADBannerView (iAd.ADAdType.Banner) {
Hidden = true
};
adContainerView.AddSubview (_adBannerView);
_adBannerView.FailedToReceiveAd += (object sender, AdErrorEventArgs e) => {
Console.WriteLine ("********** Failed to load ad: " + e.Error.LocalizedDescription);
_adBannerView.Hidden = true;
};
_adBannerView.AdLoaded += (sender, args) => {
Console.WriteLine ("********** Successfully loaded ad.");
_adBannerView.Hidden = false;
};
}
}
Google AdMob C# Version:
public partial class ActionViewController : UIViewController {
const string AdmobID = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxxx";
BannerView adView;
bool _viewOnScreen = false;
public override void ViewDidLoad () {
base.ViewDidLoad ();
// === banner ad ===
adView = new BannerView (size: AdSizeCons.Banner, origin: new CGPoint (0, 0)) {
AdUnitID = AdmobID,
RootViewController = this
};
adView.AdReceived += (object sender, EventArgs e) => {
Console.WriteLine ("********** Banner Ad received");
if (! _viewOnScreen) {
adContainerView.AddSubview(adView);
_viewOnScreen = true;
}
};
adView.ReceiveAdFailed += (sender, e) => {
Console.WriteLine ("********** BANNER AD FAILED");
};
Request request = Request.GetDefaultRequest();
#if DEBUG
request.TestDevices = new string[] { Request.GetSimulatorId().ToString() };
#endif
adView.LoadRequest (request);
// === AdMob Interstitial Ads ===
Interstitial adInterstitial = new Interstitial("ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxxx");
Request requestInterstitial = Request.GetDefaultRequest();
adInterstitial.AdReceived += (sender, args) =>
{
Console.WriteLine ("********** INTERSTITAL: Successfully received ad");
};
adInterstitial.ReceiveAdFailed += (sender, e) => {
Console.WriteLine ("********** INTERSTITAL: 'Receive Ad' FAILED");
};
#if DEBUG
requestInterstitial.TestDevices = new string[] { Request.GetSimulatorId().ToString() };
#endif
adInterstitial.LoadRequest(requestInterstitial);
}
}
Update 2:
I also tried iAds (ADBannerView
) in a ShareExtension in both C# and Objective-C. Same results as above in both languages: the components are created, but no events are fired.
Also, proxying the HTTP & HTTPS traffic through another machine running Fiddler confirms that no HTTP or HTTPS request are made by the iAd component that is running in the Extension portion of the app.
iAd Objective-C Version (showing only iAd-related code):
TestViewController.h:
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
@protocol ExtensionContextHolder <NSObject>
- (NSExtensionContext *)extensionContext;
@end
@interface TestViewController : UIViewController<ADBannerViewDelegate>
- (instancetype)initWithExtensionContextHolder:(id<ExtensionContextHolder>)extensionContextHolder;
@end
TestViewController.m:
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)];
adView.delegate = self;
[self.view addSubview:adView];
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
return YES;
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
NSLog(@"********** LOADED AN AD **********");
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
NSLog(@"********** FAILED TO RECEIVE AN AD **********");
}
@end