-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFacebookBannerCustomEvent.m
141 lines (115 loc) · 4.53 KB
/
FacebookBannerCustomEvent.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// FacebookBannerCustomEvent.m
// MoPub
//
// Copyright (c) 2014 MoPub. All rights reserved.
//
#import <FBAudienceNetwork/FBAudienceNetwork.h>
#import "FacebookBannerCustomEvent.h"
#import "MPInstanceProvider.h"
#import "MPLogging.h"
@interface MPInstanceProvider (FacebookBanners)
- (FBAdView *)buildFBAdViewWithPlacementID:(NSString *)placementID
size:(FBAdSize)size
rootViewController:(UIViewController *)controller
delegate:(id<FBAdViewDelegate>)delegate;
@end
@implementation MPInstanceProvider (FacebookBanners)
- (FBAdView *)buildFBAdViewWithPlacementID:(NSString *)placementID
size:(FBAdSize)size
rootViewController:(UIViewController *)controller
delegate:(id<FBAdViewDelegate>)delegate
{
FBAdView *adView = [[FBAdView alloc] initWithPlacementID:placementID
adSize:size
rootViewController:controller];
adView.delegate = delegate;
[adView disableAutoRefresh];
return adView;
}
@end
@interface FacebookBannerCustomEvent () <FBAdViewDelegate>
@property (nonatomic, strong) FBAdView *fbAdView;
@end
@implementation FacebookBannerCustomEvent
- (BOOL)enableAutomaticImpressionAndClickTracking
{
return NO;
}
- (void)requestAdWithSize:(CGSize)size customEventInfo:(NSDictionary *)info
{
/**
* Facebook Banner ads can accept arbitrary widths for given heights of 50 and 90. We convert these sizes
* to Facebook's constants and set the fbAdView's size to the intended size ("size" passed to this method).
*/
FBAdSize fbAdSize;
if (CGSizeEqualToSize(size, kFBAdSize320x50.size)) {
fbAdSize = kFBAdSize320x50;
} else if (size.height == kFBAdSizeHeight250Rectangle.size.height) {
fbAdSize = kFBAdSizeHeight250Rectangle;
} else if (size.height == kFBAdSizeHeight90Banner.size.height) {
fbAdSize = kFBAdSizeHeight90Banner;
} else if (size.height == kFBAdSizeHeight50Banner.size.height) {
fbAdSize = kFBAdSizeHeight50Banner;
} else {
MPLogError(@"Invalid size for Facebook banner ad");
[self.delegate bannerCustomEvent:self didFailToLoadAdWithError:nil];
return;
}
if (![info objectForKey:@"placement_id"]) {
MPLogError(@"Placement ID is required for Facebook banner ad");
[self.delegate bannerCustomEvent:self didFailToLoadAdWithError:nil];
return;
}
MPLogInfo(@"Requesting Facebook banner ad");
self.fbAdView =
[[MPInstanceProvider sharedProvider] buildFBAdViewWithPlacementID:[info objectForKey:@"placement_id"]
size:fbAdSize
rootViewController:[self.delegate viewControllerForPresentingModalView]
delegate:self];
if (!self.fbAdView) {
[self.delegate bannerCustomEvent:self didFailToLoadAdWithError:nil];
return;
}
/*
* Manually resize the frame of the FBAdView due to a bug in the Facebook SDK that sets the ad's width
* to the width of the device instead of the width of the container it's placed in.
* (Confirmed in email with a FB Solutions Engineer)
*/
CGRect fbAdFrame = self.fbAdView.frame;
fbAdFrame.size = size;
self.fbAdView.frame = fbAdFrame;
[self.fbAdView loadAd];
}
- (void)dealloc
{
_fbAdView.delegate = nil;
}
#pragma mark FBAdViewDelegate methods
- (void)adView:(FBAdView *)adView didFailWithError:(NSError *)error;
{
MPLogInfo(@"Facebook banner failed to load with error: %@", error.description);
[self.delegate bannerCustomEvent:self didFailToLoadAdWithError:error];
}
- (void)adViewDidLoad:(FBAdView *)adView;
{
MPLogInfo(@"Facebook banner ad did load");
[self.delegate trackImpression];
[self.delegate bannerCustomEvent:self didLoadAd:adView];
}
- (void)adViewDidClick:(FBAdView *)adView
{
MPLogInfo(@"Facebook banner ad was clicked");
[self.delegate trackClick];
[self.delegate bannerCustomEventWillBeginAction:self];
}
- (void)adViewDidFinishHandlingClick:(FBAdView *)adView
{
MPLogInfo(@"Facebook banner ad did finish handling click");
[self.delegate bannerCustomEventDidFinishAction:self];
}
- (UIViewController *)viewControllerForPresentingModalView
{
return [self.delegate viewControllerForPresentingModalView];
}
@end