Skip to content

Writing Custom Events

Itamar Nabriski edited this page May 30, 2016 · 3 revisions

Banner

You must extend the MobFoxCustomEvent class.

//
//  MobFoxCustomEvent.h
//  MobFoxSDKCore
//
#ifndef MobFoxCustomEvent_h
#define MobFoxCustomEvent_h

#import <UIKit/UIKit.h>

@class MobFoxCustomEvent;

@protocol MobFoxCustomEventDelegate <NSObject>

- (void)MFCustomEventAd:(MobFoxCustomEvent *)event didLoad:(UIView *)ad;

- (void)MFCustomEventAdDidFailToReceiveAdWithError:(NSError *)error;

@optional

- (void)MFCustomEventAdClosed;

- (void)MFCustomEventMobFoxAdClicked;

- (void)MFCustomEventMobFoxAdFinished;

@end


@interface MobFoxCustomEvent : NSObject

- (void)requestAdWithSize:(CGSize)size networkID:(NSString*)nid customEventInfo:(NSDictionary *)info;

@property (nonatomic, weak) id<MobFoxCustomEventDelegate> delegate;

@end

#endif /* MobFoxCustomEvent_h */

If the name of the network you are writing the custom event to is XXX your custom event class must be named MobFoxCustomEventXXX.

Delegate

You must call the methods of the MobFoxCustomEventDelegate where appropriate

MoPub Custom Event Example

MobFoxCustomEventMoPub.h

#ifndef MobFoxCustomEventMoPub_h
#define MobFoxCustomEventMoPub_h

#import <MobFoxSDKCore/MobFoxSDKCore.h>
#import "MPAdView.h"

@interface MobFoxCustomEventMoPub : MobFoxCustomEvent<MPAdViewDelegate>

@property (nonatomic) MPAdView *adView;

- (void)requestAdWithSize:(CGSize)size networkID:(NSString*)nid customEventInfo:(NSDictionary *)info;

- (UIViewController *)viewControllerForPresentingModalView;

- (void)adViewDidLoadAd:(MPAdView *)view;

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

- (void)willLeaveApplicationFromAd:(MPAdView *)view;

@end


#endif /* MobFoxCustomEventMoPub_h */

MobFoxCustomEventMoPub.m

#import "MobFoxCustomEventMoPub.h"


@implementation MobFoxCustomEventMoPub

{
    CGSize requstedSize;
}

- (void)requestAdWithSize:(CGSize)size networkID:(NSString*)nid customEventInfo:(NSDictionary *)info{
   
    requstedSize = size;
    self.adView = [[MPAdView alloc] initWithAdUnitId:nid size:CGSizeMake(size.width, size.height)];
    self.adView.delegate = self;
    self.adView.frame = CGRectMake(0,0,size.width, size.height);
    
    [self.adView loadAd];
    
}

- (UIViewController*) getUIViewController:(UIView*)view  {
    
    id nextResponder = [view nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        return nextResponder;
    } else if ([nextResponder isKindOfClass:[UIView class]]) {
        return [self getUIViewController:(UIView*)nextResponder];
    } else {
        return nil;
    }
}

#pragma mark - <MPAdViewDelegate>
- (UIViewController *)viewControllerForPresentingModalView{
    return [self getUIViewController:self.adView];
}

- (void)adViewDidLoadAd:(MPAdView *)view{
   
    CGSize actualSize = [view adContentViewSize];
    if(requstedSize.width < actualSize.width || requstedSize.height < actualSize.height){
        NSDictionary* ui = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"returned ad size different from requested size", @"message" ,nil];
        NSError* err = [NSError errorWithDomain:@"MoPubFailed"
                                           code:40402
                                       userInfo:ui];
        [self.delegate MFCustomEventAdDidFailToReceiveAdWithError:err];
        return;
    }
    [self.delegate MFCustomEventAd:self didLoad:self.adView];
  
}

#pragma mark - <MPAdViewDelegate>
- (void)adViewDidFailToLoadAd:(MPAdView *)view{
    NSError* err = [NSError errorWithDomain:@"MoPubFailed"
                                       code:40401
                                   userInfo:nil];
    [self.delegate MFCustomEventAdDidFailToReceiveAdWithError:err];
}

#pragma mark - <MPAdViewDelegate>
- (void)willLeaveApplicationFromAd:(MPAdView *)view{
    
}

@end

Interstitial

You must extend the MobFoxInterstitialCustomEvent class.

//
//  MobFoxInterstitialVideo.h
//  MobFoxSDKSource
//

#ifndef MobFoxSDKSource_MobFoxInterstitialVideo_h
#define MobFoxSDKSource_MobFoxInterstitialVideo_h

#include "MobFoxAd.h"
#import "MobFoxInterstitialCustomEvent.h"


@class MobFoxInterstitialAd;

@protocol MobFoxInterstitialAdDelegate <NSObject>

@optional

- (void)MobFoxInterstitialAdDidLoad:(MobFoxInterstitialAd *)interstitial;

- (void)MobFoxInterstitialAdDidFailToReceiveAdWithError:(NSError *)error;

- (void)MobFoxInterstitialAdWillShow:(MobFoxInterstitialAd *)interstitial;

- (void)MobFoxInterstitialAdClosed;

- (void)MobFoxInterstitialAdClicked;

- (void)MobFoxInterstitialAdFinished;

@end

@interface MobFoxInterstitialAd : NSObject<MobFoxAdDelegate,MobFoxInterstitialCustomEventDelegate>

@property (nonatomic, weak) id<MobFoxInterstitialAdDelegate> delegate;
@property (nonatomic, weak) UIViewController* rootViewController;

@property BOOL ready;
@property (weak, nonatomic) MobFoxAd* ad;

-(id) init:(NSString*)invh;
-(id) init:(NSString*)invh withRootViewController:(UIViewController*)root;
-(void) loadAd;
-(void) show;

@end

#endif

If the name of the network you are writing the custom event to is XXX your custom event class must be named MobFoxInterstitialCustomEventXXX.

Delegate

You must call the methods of the MobFoxCustomEventDelegate where appropriate

MoPub Custom Interstitial Event Example

MobFoxInterstitialCustomEventMoPub.h

#ifndef MoPubInterstitialAdapterMobFox_h
#define MoPubInterstitialAdapterMobFox_h

#import <MobFoxSDKCore/MobFoxSDKCore.h>

#if __has_include(<MoPub/MoPub.h>)
#import <MoPub/MoPub.h>
#else
#import "MPInterstitialCustomEvent.h"
#endif


@interface MoPubInterstitialAdapterMobFox : MPInterstitialCustomEvent<MobFoxInterstitialAdDelegate>

@property (strong, nonatomic) MobFoxInterstitialAd* mobFoxInterAd;

- (void)requestInterstitialWithCustomEventInfo:(NSDictionary *)info;

- (void)showInterstitialFromRootViewController:(UIViewController *)rootViewController;

@end

#endif

MobFoxInterstitialCustomEventMoPub.m

#import "MoPubInterstitialAdapterMobFox.h"

@implementation MoPubInterstitialAdapterMobFox

- (void)requestInterstitialWithCustomEventInfo:(NSDictionary *)info{

    NSLog(@"MoPub inter >> MobFox >> init");
    NSLog(@"MoPub inter >> MobFox >> data: %@",[info description]);
    self.mobFoxInterAd = [[MobFoxInterstitialAd alloc] init:[info valueForKey:@"invh"]];
    self.mobFoxInterAd.delegate = self;
    [self.mobFoxInterAd loadAd];
        
}

- (void)showInterstitialFromRootViewController:(UIViewController *)rootViewController{
    NSLog(@"MoPub inter >> MobFox >> set root");
    self.mobFoxInterAd.rootViewController = rootViewController;
    [self.mobFoxInterAd show];
}

- (void)MobFoxInterstitialAdDidLoad:(MobFoxInterstitialAd *)interstitial{
    NSLog(@"MoPub inter >> MobFox >> ad loaded");
    [self.delegate interstitialCustomEvent:self didLoadAd:interstitial];
   
}

- (void)MobFoxInterstitialAdDidFailToReceiveAdWithError:(NSError *)error{
    NSLog(@"MoPub inter >> MobFox >> ad error: %@",[error description]);
    [self.delegate interstitialCustomEvent:self didFailToLoadAdWithError:error];
}

- (void)MobFoxInterstitialAdWillShow:(MobFoxInterstitialAd *)interstitial{
    [self.delegate interstitialCustomEventWillAppear:self];
    
}

- (void)MobFoxInterstitialAdClosed{
    [self.delegate interstitialCustomEventDidDisappear:self];
}

- (void)MobFoxInterstitialAdClicked{
    [self.delegate interstitialCustomEventWillLeaveApplication:self];
}

- (void)MobFoxInterstitialAdFinished{
}

- (void)dealloc {
    
    self.mobFoxInterAd.ad.bridge = nil;
    self.mobFoxInterAd.ad        = nil;
    self.mobFoxInterAd           = nil;

}

@end