1 | #import <EXAdsFacebook/EXInterstitialAdManager.h>
|
2 | #import <UMCore/UMUtilities.h>
|
3 |
|
4 | #import <FBAudienceNetwork/FBAudienceNetwork.h>
|
5 |
|
6 | @interface EXInterstitialAdManager () <FBInterstitialAdDelegate>
|
7 |
|
8 | @property (nonatomic, strong) UMPromiseResolveBlock resolve;
|
9 | @property (nonatomic, strong) UMPromiseRejectBlock reject;
|
10 | @property (nonatomic, strong) FBInterstitialAd *interstitialAd;
|
11 | @property (nonatomic, strong) UIViewController *adViewController;
|
12 | @property (nonatomic) bool didClick;
|
13 | @property (nonatomic) bool isBackground;
|
14 | @property (nonatomic, weak) UMModuleRegistry *moduleRegistry;
|
15 |
|
16 | @end
|
17 |
|
18 | @implementation EXInterstitialAdManager
|
19 |
|
20 | UM_EXPORT_MODULE(CTKInterstitialAdManager)
|
21 |
|
22 | - (void)setModuleRegistry:(UMModuleRegistry *)moduleRegistry
|
23 | {
|
24 | _moduleRegistry = moduleRegistry;
|
25 | }
|
26 |
|
27 | UM_EXPORT_METHOD_AS(showAd,
|
28 | showAd:(NSString *)placementId
|
29 | resolver:(UMPromiseResolveBlock)resolve
|
30 | rejecter:(UMPromiseRejectBlock)reject)
|
31 | {
|
32 | if (_resolve != nil || _reject != nil) {
|
33 | reject(@"E_NO_CONCURRENT", @"Only one `showAd` can be called at once", nil);
|
34 | return;
|
35 | }
|
36 | if (_isBackground) {
|
37 | reject(@"E_BACKGROUNDED", @"`showAd` can be called only when experience is running in foreground", nil);
|
38 | return;
|
39 | }
|
40 |
|
41 | _resolve = resolve;
|
42 | _reject = reject;
|
43 |
|
44 | _interstitialAd = [[FBInterstitialAd alloc] initWithPlacementID:placementId];
|
45 | _interstitialAd.delegate = self;
|
46 | [UMUtilities performSynchronouslyOnMainThread:^{
|
47 | [self->_interstitialAd loadAd];
|
48 | }];
|
49 | }
|
50 |
|
51 | #pragma mark - FBInterstitialAdDelegate
|
52 |
|
53 | - (void)interstitialAdDidLoad:(__unused FBInterstitialAd *)interstitialAd
|
54 | {
|
55 | [_interstitialAd showAdFromRootViewController:[[_moduleRegistry getModuleImplementingProtocol:@protocol(UMUtilitiesInterface)] currentViewController]];
|
56 | }
|
57 |
|
58 | - (void)interstitialAd:(FBInterstitialAd *)interstitialAd didFailWithError:(NSError *)error
|
59 | {
|
60 | _reject(@"E_FAILED_TO_LOAD", [error localizedDescription], error);
|
61 |
|
62 | [self cleanUpAd];
|
63 | }
|
64 |
|
65 | - (void)interstitialAdDidClick:(FBInterstitialAd *)interstitialAd
|
66 | {
|
67 | _didClick = true;
|
68 | }
|
69 |
|
70 | - (void)interstitialAdDidClose:(FBInterstitialAd *)interstitialAd
|
71 | {
|
72 | _resolve(@(_didClick));
|
73 |
|
74 | [self cleanUpAd];
|
75 | }
|
76 |
|
77 | - (void)bridgeDidForeground:(NSNotification *)notification
|
78 | {
|
79 | _isBackground = false;
|
80 |
|
81 | if (_adViewController) {
|
82 | [[[_moduleRegistry getModuleImplementingProtocol:@protocol(UMUtilitiesInterface)] currentViewController] presentViewController:_adViewController animated:NO completion:nil];
|
83 | _adViewController = nil;
|
84 | }
|
85 | }
|
86 |
|
87 | - (void)bridgeDidBackground:(NSNotification *)notification
|
88 | {
|
89 | _isBackground = true;
|
90 |
|
91 | if (_interstitialAd) {
|
92 | _adViewController = [[_moduleRegistry getModuleImplementingProtocol:@protocol(UMUtilitiesInterface)] currentViewController];
|
93 | [_adViewController dismissViewControllerAnimated:NO completion:nil];
|
94 | }
|
95 | }
|
96 |
|
97 | - (void)cleanUpAd
|
98 | {
|
99 | _reject = nil;
|
100 | _resolve = nil;
|
101 | _interstitialAd = nil;
|
102 | _adViewController = nil;
|
103 | _didClick = false;
|
104 | }
|
105 |
|
106 | @end
|