forked from minima/mopub-ios-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPInterstitialViewController.m
248 lines (198 loc) · 7.55 KB
/
MPInterstitialViewController.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//
// MPInterstitialViewController.m
// MoPub
//
// Copyright (c) 2012 MoPub, Inc. All rights reserved.
//
#import "MPInterstitialViewController.h"
#import "MPGlobal.h"
#import "MPLogging.h"
#import "Skillz_private.h"
#import "UIButton+MPAdditions.h"
#import "UIApplication+Skillz.h"
#import "UIView+Skillz.h"
static const CGFloat kCloseButtonPadding = 5.0;
static const CGFloat kCloseButtonEdgeInset = 5.0;
static const CGFloat kCloseButtonPaddingForPad = 12.0;
static NSString * const kCloseButtonXImageName = @"MPCloseButtonX.png";
@interface MPInterstitialViewController ()
@property (nonatomic, assign) BOOL applicationHasStatusBar;
- (void)setCloseButtonImageWithImageNamed:(NSString *)imageName;
- (void)setCloseButtonStyle:(MPInterstitialCloseButtonStyle)style;
- (void)closeButtonPressed;
- (void)dismissInterstitialAnimated:(BOOL)animated;
- (void)setApplicationStatusBarHidden:(BOOL)hidden;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation MPInterstitialViewController
@synthesize closeButton = _closeButton;
@synthesize closeButtonStyle = _closeButtonStyle;
@synthesize orientationType = _orientationType;
@synthesize applicationHasStatusBar = _applicationHasStatusBar;
@synthesize delegate = _delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
}
#pragma mark - Public
- (void)presentInterstitialFromViewController:(UIViewController *)controller
{
if (self.presentingViewController) {
MPLogWarn(@"Cannot present an interstitial that is already on-screen.");
return;
}
[self willPresentInterstitial];
self.applicationHasStatusBar = !([UIApplication sharedApplication].isStatusBarHidden);
[self setApplicationStatusBarHidden:YES];
[self layoutCloseButton];
[controller presentViewController:self animated:NO completion:^{
[self didPresentInterstitial];
}];
}
- (void)willPresentInterstitial
{
}
- (void)didPresentInterstitial
{
}
- (void)willDismissInterstitial
{
}
- (void)didDismissInterstitial
{
}
- (BOOL)shouldDisplayCloseButton
{
return YES;
}
#pragma mark - Close Button
- (UIButton *)closeButton
{
if (!_closeButton) {
_closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
_closeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleBottomMargin;
UIImage *closeButtonImage = [UIImage imageFromResource:MPResourcePathForResource(kCloseButtonXImageName)];
[_closeButton setImage:closeButtonImage forState:UIControlStateNormal];
[_closeButton sizeToFit];
[_closeButton addTarget:self
action:@selector(closeButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
_closeButton.accessibilityLabel = @"Close Interstitial Ad";
}
return _closeButton;
}
- (void)layoutCloseButton
{
[self.view addSubview:self.closeButton];
NSInteger padding = isPad() ? kCloseButtonPaddingForPad : kCloseButtonPadding;
CGFloat originX = self.view.bounds.size.width - padding - self.closeButton.bounds.size.width;
self.closeButton.frame = CGRectMake(originX,
padding,
self.closeButton.bounds.size.width,
self.closeButton.bounds.size.height);
self.closeButton.mp_TouchAreaInsets = UIEdgeInsetsMake(kCloseButtonEdgeInset, kCloseButtonEdgeInset, kCloseButtonEdgeInset, kCloseButtonEdgeInset);
[self setCloseButtonStyle:self.closeButtonStyle];
if (@available(iOS 11.0, *)) {
self.closeButton.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:@[
[self.closeButton.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor constant:padding],
[self.closeButton.trailingAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.trailingAnchor constant:-padding],
]];
}
[self.view bringSubviewToFront:self.closeButton];
}
- (void)setCloseButtonImageWithImageNamed:(NSString *)imageName
{
UIImage *image = [UIImage imageFromResource:imageName];
[self.closeButton setImage:image forState:UIControlStateNormal];
[self.closeButton sizeToFit];
}
- (void)setCloseButtonStyle:(MPInterstitialCloseButtonStyle)style
{
_closeButtonStyle = style;
switch (style) {
case MPInterstitialCloseButtonStyleAlwaysVisible:
self.closeButton.hidden = NO;
break;
case MPInterstitialCloseButtonStyleAlwaysHidden:
self.closeButton.hidden = YES;
break;
case MPInterstitialCloseButtonStyleAdControlled:
self.closeButton.hidden = ![self shouldDisplayCloseButton];
break;
default:
self.closeButton.hidden = NO;
break;
}
}
- (void)closeButtonPressed
{
[self dismissInterstitialAnimated:YES];
}
- (void)dismissInterstitialAnimated:(BOOL)animated
{
if (UIInterfaceOrientationIsLandscape([NSUserDefaults currentSDKOrientation]) && hasNotchedDisplay()) {
dispatch_async(dispatch_get_main_queue(), ^{
[[Skillz skillzInstance].navigationController updateControllerForPortraitOrientation];
});
}
[self setApplicationStatusBarHidden:!self.applicationHasStatusBar];
[self willDismissInterstitial];
UIViewController *presentingViewController = self.presentingViewController;
// TODO: Is this check necessary?
if (presentingViewController.presentedViewController == self) {
[presentingViewController dismissViewControllerAnimated:MP_ANIMATED completion:^{
[self didDismissInterstitial];
}];
} else {
[UIView animateWithDuration:.3 animations:^{
[self.view setAlpha:0];
} completion:^(BOOL finished) {
[self.view removeFromSuperview];
[self removeFromParentViewController];
[self didDismissInterstitial];
}];
}
}
#pragma mark - Hidding status bar (pre-iOS 7)
- (void)setApplicationStatusBarHidden:(BOOL)hidden
{
[[UIApplication sharedApplication] mp_preIOS7setApplicationStatusBarHidden:hidden];
}
#pragma mark - Hidding status bar (iOS 7 and above)
- (BOOL)prefersStatusBarHidden
{
return YES;
}
#pragma mark - Autorotation (iOS 6.0 and above)
- (BOOL)shouldAutorotate
{
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
if ([[NSUserDefaults gameOrientation] isEqualToString:SKZ_LANDSCAPE_ORIENTATION]) {
return [[[[UIApplication sharedApplication] reliableKeyWindow] rootViewController] preferredInterfaceOrientationForPresentation];
} else {
return UIInterfaceOrientationPortrait;
}
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
// Need to return a conversation of preferredInterfaceOrientationForPresentation from our rootViewController or all.
// Trying all for now.
return UIInterfaceOrientationMaskAll;
}
#pragma mark - Autorotation (before iOS 6.0)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//Keeping this the same for the moment as it seems to work well.
if ([[NSUserDefaults gameOrientation] isEqualToString:SKZ_LANDSCAPE_ORIENTATION]) {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
} else {
return interfaceOrientation == UIInterfaceOrientationPortrait;
}
}
@end