需求
- 首次啟動(dòng)廣告
- 熱啟動(dòng)廣告(App進(jìn)入后臺(tái)后羊苟,再次進(jìn)入前臺(tái))
- 熱啟動(dòng)時(shí)間間隔控制 (App在后臺(tái)的時(shí)間)
- 每次運(yùn)行期間廣告顯示次數(shù)控制 (單次運(yùn)行期間允許顯示廣告次數(shù))
- 每天總的廣告顯示次數(shù)控制 (每天允許顯示廣告次數(shù))
- 區(qū)分熱啟動(dòng)與App在前臺(tái)解鎖手機(jī)
App在前臺(tái)塑陵,用戶鎖屏相當(dāng)于進(jìn)入后臺(tái),解鎖后App進(jìn)入前臺(tái)蜡励,都會(huì)執(zhí)行UIApplication
對(duì)應(yīng)的代理方法令花。
一些App的非首次啟動(dòng)廣告,解鎖后App在前臺(tái)的情況下也會(huì)顯示凉倚,比如微博兼都。
一般廣告會(huì)有倒計(jì)時(shí)與跳過(guò)按鈕,這里只是一個(gè)簡(jiǎn)易的效果稽寒,只有一張圖片:
實(shí)現(xiàn)與代碼封裝
設(shè)計(jì)
廣告的管理控制屬于一個(gè)單獨(dú)的邏輯模塊扮碧,可以單獨(dú)封裝一個(gè)類,以單例的形式存在杏糙。
相比直接將代碼寫在AppDelegate
的各個(gè)代理方法中慎王,這樣封裝可以更好的復(fù)用代碼,也能讓AppDelegate
不至于太臃腫復(fù)雜宏侍。
解鎖判斷
對(duì)于解鎖狀態(tài)的判斷赖淤,已經(jīng)不能通過(guò)CFNotificationCenter
的com.apple.springboard.lockstate
等通知進(jìn)行判斷了,否則可能在上傳App或者上架過(guò)程被拒谅河。
可以在應(yīng)用即將進(jìn)入前臺(tái)時(shí)漫蛔,可以結(jié)合UIApplicationState
和屏幕亮度來(lái)判斷:
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
CGFloat screenBrightness = [[UIScreen mainScreen] brightness];
BOOL isFromLockScreen = (state == UIApplicationStateInactive) ||
(state == UIApplicationStateBackground && screenBrightness <= 0.0);
統(tǒng)計(jì)控制
后臺(tái)時(shí)間和每次運(yùn)行顯示的廣告次數(shù)可以用變量統(tǒng)計(jì);每天的顯示次數(shù)則可以存入NSUserDefault
旧蛾。
封裝
只將一些控制選項(xiàng)暴露給外部:
@class KMADManager;
typedef BOOL(^ShowADFilter)(KMADManager *manager, BOOL isFirstLaunch, BOOL reachTimeThreshold, BOOL reachMaxSessionTime, BOOL reachMaxDailyTime);
typedef void(^ShowADAction)(KMADManager *manager, BOOL isFirstLaunch);
@interface KMADManager : NSObject
/// should exclude app active from screen unlock or not, default YES
@property (nonatomic, assign) BOOL excludeFromScreenLock;
/// time interval in seconds between background and foreground, default to 300s(5 minus)
@property (nonatomic, assign) NSTimeInterval timeThreshold;
/// current session AD show times
@property (nonatomic, assign, readonly) NSUInteger sessionShowTimes;
/// current daily AD show times
@property (nonatomic, assign, readonly) NSUInteger dailyShowTimes;
/// max time of showing AD per session(APP run one time), default to NSUIntegerMax
@property (nonatomic, assign) NSUInteger maxSessionShowTimes;
/// max time of showing AD per day, default to NSUIntegerMax
@property (nonatomic, assign) NSUInteger maxDailyShowTimes;
/// if exists, will use the return value of this block to determine should show a AD or not.
@property (nonatomic, copy) ShowADFilter showADFilter;
/// execute this block when an AD should be shown
@property (nonatomic, copy) ShowADAction showADAction;
+ (KMADManager *)sharedInstace;
@end
這樣外部在調(diào)用時(shí),只需做簡(jiǎn)單的設(shè)置和顯示廣告即可:
KMADManager *mag = [KMADManager sharedInstace];
mag.maxDailyShowTimes = 5;
mag.maxSessionShowTimes = 3;
mag.excludeFromScreenLock = NO;
mag.timeThreshold = 3 * 60; // 3 minus
mag.showADAction = ^(KMADManager *manager, BOOL isFirstLaunch) {
// here, code to show your AD
// e.g.
NSString *imageName = [NSString stringWithFormat:@"ad%@.jpg",@(manager.dailyShowTimes)];
UIImage *image = [UIImage imageNamed:imageName];
if (image) {
[ADHelper showADWithImage:image];
}
};
集成pod
上面的代碼已經(jīng)做成了pod
pod 'KMADManager', '~> 0.1.0'
如果還不知道怎么創(chuàng)建自己的pod蠕嫁,可以參考 CocoaPods創(chuàng)建自己的開源庫(kù)和私有庫(kù)
廣告顯示
可以單獨(dú)封裝一個(gè)類锨天,里面使用UIWindow
或者UIView
來(lái)顯示廣告。