larsacus / LARSAdController

Lightweight ad mediation for iOS to properly manage multiple ad networks dynamically including iAd and Google ads.
http://theonlylars.com/blog/2013/01/10/stupid-easy-ads-with-larsadcontroller-3-dot-0/
MIT License
269 stars 60 forks source link

MoPub Adapter #46

Closed Fred10932 closed 11 years ago

Fred10932 commented 11 years ago

MoPub (and other networks through mediation) was very easy to implement. Here's the code if anyone's interested:

TOLAdAdapterMoPubAds.h


#import  <Foundation/Foundation.h>

#import "LARSAdController.h"
#import "TOLAdAdapter.h"
#import "MPAdView.h"

@interface TOLAdAdapterMoPubAds : NSObject  <TOLAdAdapter, MPAdViewDelegate>

@property (weak, nonatomic) id <LARSAdControllerDelegate> adManager;
@property (nonatomic) BOOL adVisible;
@property (strong, nonatomic) MPAdView *bannerView;
@property (weak, nonatomic) UIViewController *parentViewController;

@end

TOLAdAdapterMoPubAds.m

@implementation TOLAdAdapterMoPubAds

#pragma mark - Required Adapted Implementation

- (UIView *)bannerView{
        if (_bannerView == nil) {

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            self.bannerView = [[MPAdView alloc] initWithAdUnitId:@"1234567890"
                                                         size:MOPUB_BANNER_SIZE];
            //Use AdUnitID for banner set up with size 728x90

        } else {
            self.bannerView = [[MPAdView alloc] initWithAdUnitId:@"1234567890"
                                                        size:MOPUB_BANNER_SIZE];
            //Use AdUnitID for banner setup with size 320x50
        }

            self.bannerView.delegate = self;
            self.bannerView.frame = CGRectMake(0, self.bannerView.superview.bounds.size.height - MOPUB_BANNER_SIZE.height,
                                               MOPUB_BANNER_SIZE.width, MOPUB_BANNER_SIZE.height);

            [self.bannerView loadAd];

    }

    return _bannerView;

}

- (void)layoutBannerForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    //Simply sets the banner view to inherit the same bounds as the container

    [self.bannerView rotateToOrientation:interfaceOrientation];

}

#pragma mark -  required implementation

- (UIViewController *)viewControllerForPresentingModalView {
    return _parentViewController;
}

#pragma mark - Optional MoPub methods

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    CGSize size = [self.bannerView adContentViewSize];
    CGFloat centeredX = (self.bannerView.superview.bounds.size.width - size.width) / 2;
    CGFloat bottomAlignedY = self.bannerView.superview.bounds.size.height - size.height;
    self.bannerView.frame = CGRectMake(centeredX, bottomAlignedY, size.width, size.height);
}

- (void)adViewDidLoadAd:(MPAdView *)view
{
    CGSize size = [view adContentViewSize];
    CGFloat centeredX = (self.bannerView.superview.bounds.size.width - size.width) / 2;
    CGFloat bottomAlignedY = self.bannerView.superview.bounds.size.height - size.height;
    view.frame = CGRectMake(centeredX, bottomAlignedY, size.width, size.height);

        TOLLog(@"Ad loaded successfullly");
        NSLog(@"Ad loaded successfullly");
        if ([self.adManager respondsToSelector:@selector(adSucceededForNetworkAdapterClass:)]) {
            [self.adManager adSucceededForNetworkAdapterClass:[self class]];

            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            CGFloat moPubBannerCount = [defaults floatForKey:[NSStringFromClass(self.class) stringByAppendingString:@"Count"]];
            moPubBannerCount++;
            [defaults setInteger:moPubBannerCount forKey:[NSStringFromClass(self.class) stringByAppendingString:@"Count"]];
            [defaults synchronize];
        }
}

- (void)adViewDidFailToLoadAd:(MPAdView *)view {

    [self.adManager adFailedForNetworkAdapterClass:[self class]];
    NSLog(@"Ad load fail");

}

- (void)dealloc{

    _bannerView = nil;
    self.adManager = nil;

    TOLLog(@"Dealloc");
}

#pragma mark - Optional Adapter Implementation

- (void)setParentViewController:(UIViewController *)viewController{
    _parentViewController = viewController;

    [self layoutBannerForInterfaceOrientation:viewController.interfaceOrientation];
}

+ (BOOL)requiresPublisherId{
    return NO;
}

+ (BOOL)requiresParentViewController{
    return YES;
}

- (NSString *)friendlyNetworkDescription{
    return @"MoPub Ads";
}

@end
larsacus commented 11 years ago

If you're interested, feel free to submit a pull request with this in it.

larsacus commented 11 years ago

Closing for now. If you'd like this to be included in this library, please submit a pull request. Otherwise, this might be a really good case to make your own CocoaPod spec for this ad adapter and simply add LARSAdController/Core as a dependency.