一弛矛、廣告頁功能思路
APP第一次安裝或版本更新時,有引導(dǎo)頁的時候不展示廣告得问,第二次開始展示廣告囤攀。
創(chuàng)建一個展示廣告的Imageview。
添加跳過廣告的倒計時按鈕宫纬。
添加廣告頁面點擊事件抚岗,點擊廣告圖片跳轉(zhuǎn)到相應(yīng)頁面;廣告鏈接地址也需要用NSUserDefaults存儲哪怔。注意:廣告詳情頁面是從首頁push進(jìn)去的宣蔚。
加載:在啟動頁展示的時間里,開始加載廣告认境,先將圖片通過SD異步下載到本地胚委,并保存圖片名,每次打開app時先根據(jù)本地存儲的圖片名查找沙盒中是否存在該圖片叉信,如果存在亩冬,則顯示廣告頁。在啟動頁加載完成之后應(yīng)該去判斷廣告是否有加載出來硼身。
判斷廣告圖片是否更新:無論本地是否存在廣告圖片硅急,每次啟動都需要重新調(diào)用廣告接口,根據(jù)圖片名稱或者圖片id等方法判斷廣告是否更新佳遂,如果獲取的圖片名稱或者圖片id跟本地存儲的不一致营袜,則需要重新下載新圖片,并刪除舊圖片丑罪。
廣告頁面的底部和啟動圖的底部一般都是相同的荚板,給我們的感覺就是啟動圖加載完之后把廣告圖放在了啟動圖上,而且不能有偏差吩屹,比如下圖淘寶啟動畫面跪另,美工在制作廣告圖的時候要注意這點。
APP在后臺:如果app在后臺待機(jī)太久煤搜,再次進(jìn)來前臺的時候也應(yīng)該展示廣告免绿,所以在applicationDidEnterBackground的時候應(yīng)該把時間存起來。在applicationWillEnterForeground的時候?qū)Ρ葧r間差擦盾,判斷是否顯示嘲驾。
增加廣告顯示開關(guān):后臺在開發(fā)廣告api的時候可以增加一個字段來判斷是否啟用廣告,如果后臺關(guān)閉了廣告厌衙,將沙盒中的圖片刪除即可距淫。
添加統(tǒng)計:看廣告詳情有多少人觀看,有多少人會跳過等婶希。
二榕暇、步驟
1、首先判斷APP是否是第一次進(jìn)入或者版本更新喻杈,如果是彤枢,啟動引導(dǎo)頁;如果不是筒饰,再判斷是否需要顯示廣告缴啡。
if ([Manager isFirstLaunch]) {
//一句代碼啟動引導(dǎo)頁
[GuidePageView sharedWithImages:@[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg"] buttonImage:@"post_normal" buttonFrame:CGRectMake(0, -10, SCREEN_WIDTH, SCREEN_HEIGHT)];`
}else {
//顯示廣告頁
//后臺在開發(fā)廣告api的時候增加一個字段來判斷是否啟用廣告
if (self.isShowAdPage) {}
//一句代碼調(diào)用廣告頁
[ADPageView showAdPageView];
}
2、創(chuàng)建一個展示廣告的Imageview和添加跳過廣告的倒計時按鈕
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
//廣告頁
_adView = [[UIImageView alloc] initWithFrame:frame];
_adView.userInteractionEnabled = YES;
_adView.contentMode = UIViewContentModeScaleAspectFit;
_adView.clipsToBounds = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToAd)];
[_adView addGestureRecognizer:tap];
//跳過按鈕
_skipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_skipBtn.backgroundColor = RGBAColor(38, 38, 38, 0.6);
_skipBtn.layer.cornerRadius = 4;
[_skipBtn setTitle:[NSString stringWithFormat:@"跳過%d",showTime] forState:UIControlStateNormal];
[_skipBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_skipBtn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
[_skipBtn addTarget:self action:@selector(dismissAdView) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_adView];
[self addSubview:_skipBtn];
_skipBtn.sd_layout
.widthIs(60)
.heightIs(30)
.topSpaceToView(self, 20)
.rightSpaceToView(self, 20);
[self showAdView];
}
return self;
}
3瓷们、廣告頁加載业栅,先從沙盒查找秒咐,有,則顯示碘裕,并下載更新携取,刪除舊圖片;沒有帮孔,下載雷滋,保存。
- (void)showAdView {
//先出沙盒讀取路徑
NSString *filePath = [self getFilePathWithImageName:UserDefaultObjectForKey(adImageName)];
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (isExist) {
[self startTimer];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window addSubview:self];
[_skipBtn setTitle:[NSString stringWithFormat:@"跳過%d",showTime] forState:UIControlStateNormal];
_adView.image = [UIImage imageWithContentsOfFile:filePath];
_adView.contentMode = UIViewContentModeScaleAspectFill;
_adUrl = UserDefaultObjectForKey(adUrl);
}
//無論沙盒中是否存在廣告圖片文兢,都需要重新調(diào)用廣告接口晤斩,判斷廣告是否更新
[self getAdImage];
}
4、圖片下載姆坚,保存圖片名和跳轉(zhuǎn)的廣告鏈接
- (void)downLoadAdImageWithUrl:(NSString *)imageUrl imageName:(NSString *)imageName {
NSURL *url = [NSURL URLWithString:imageUrl];
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
//開始存儲圖片
NSString *filePath = [self getFilePathWithImageName:imageName];
if ([UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]) {
[self deleteOldImage];
//存新圖片和新廣告鏈接
UserDefaultSetObjectForKey(imageName, adImageName);
UserDefaultSetObjectForKey(imageUrl, adUrl);
UserDefaultSynchronize;
}
}];
}