在啓動頁後顯示廣告頁對體驗來說不可取的,但是公司以利益爲主這個功能就產生了。
添加廣告頁有兩種思路:1.先下載完成後直接顯示 2.先保存下次打開在顯示;
先下載直接先顯示
我最新想到的就是下載完成後直接顯示,接口失敗或者超時則以假啓動頁過渡顯示主頁。可是我寫到顯示廣告頁就停止了,因爲無論怎麼寫都會出現廣告缺失的情況,而且啓動頁顯示時間加長這兩種都對體驗很不好。所以放棄的這種方式。
先保存下次打開顯示
在網上發現這個朋友的思路很好,參考他的方法完成了我想要的效果,非常感謝。
1.我們的APP用戶第一次打開是顯示引導頁不需要顯示廣告的,這個時候就可以下載廣告相關圖片、鏈接完成以後把廣告圖片保存本地,下次打開APP判斷圖片是否存在以此判斷廣告頁是否顯示,思路很清晰有圖片就顯示沒圖片就不顯示;
2.這種方式沒有啓動圖延遲顯示的問題也不用添加假啓動頁的相關邏輯;
3.對於用戶來說不會出現廣告缺失的情況,不用考慮網絡問題。
代碼
1.一行代碼接入廣告加載
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewController *controller = [[ViewController alloc] init];
self.rootNav = [[UINavigationController alloc] initWithRootViewController:controller];
self.window.rootViewController = self.rootNav;
[self.window makeKeyAndVisible];
//一行代碼接入廣告頁
[[AdvertiseManage sharedManage] advertiseViewShow];
return YES;
}
2.判斷廣告圖片是否存在,存在就顯示廣告頁;無論什麼情況我們都要更新廣告接口- (void)advertiseViewShow {
//1.判斷沙盒中是否存在廣告圖片,如果存在直接顯示
NSString *filePath = [self getFilePathWithImageName:[AdUserDefaults valueForKey:adImageName]];
NSString *adDetails = [AdUserDefaults valueForKey:adUrlString];
BOOL isExist = [self isFileExistWithFilePath:filePath];
if (isExist) { //圖片存在
AdvertiseView *advertiseView = [[AdvertiseView alloc] initWithFrame:[UIScreen mainScreen].bounds];
advertiseView.imageFilePath = filePath;
if (adDetails) {
advertiseView.isAdClick = YES;
}
[advertiseView show];
}
//2.無論沙盒中是否存在廣告圖片,都需要重新調用廣告接口,判斷廣告接口是否更新
[self getAdvertiseImage];
}
異步下載圖片
/**
* 下載新圖片
*/
- (void)downloadAdImageWithUrl:(NSString *)imageUrl imageName:(NSString *)imageName {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//下載圖片
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
UIImage *image = [UIImage imageWithData:data];
//拼接保存文件的名稱
NSString *filePath = [self getFilePathWithImageName:imageName];
if ([UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]) {//保存成功
NSLog(@"保存成功");
[self deleteOldImage];
[AdUserDefaults setValue:imageName forKey:adImageName];
[AdUserDefaults setValue:@"http://baidu.com" forKey:adUrlString];//如果有廣告鏈接,將廣告鏈接也保存下來
[AdUserDefaults synchronize];
}else {
NSLog(@"保存失敗");
}
});
}
/**
* 刪除舊廣告相關
*/
- (void)deleteOldImage {
NSString *imageName = [AdUserDefaults valueForKey:adImageName];
if (imageName) {
NSString *filePath = [self getFilePathWithImageName:imageName];
NSFileManager *fileManage = [NSFileManager defaultManager];
[fileManage removeItemAtPath:filePath error:nil];
//把廣告鏈接也要刪掉
[AdUserDefaults setValue:nil forKey:adUrlString];
[AdUserDefaults synchronize];
}
}
3.添加定時器
- (void)setUpTimer {
_count = showTime;
if (_timer == nil) {
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeCountShow) userInfo:nil repeats:YES];
}
}
- (void)changeCountShow {
_count --;
NSLog(@"count:%ld",(long)_count);
if (_count == 0) {
[self dismiss];
}else {
[_skipButton setTitle:[NSString stringWithFormat:@"跳過 %ld",(long)_count] forState:UIControlStateNormal];
}
}
4.利用通知來push廣告詳情頁//發送通知主頁跳轉到詳情頁
- (void)pushToAd {
[self dismiss];
//添加通知 push廣告詳情頁
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushToAd" object:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToAd) name:@"pushToAd" object:nil];
self.title = @"首頁";
self.view.backgroundColor = [UIColor redColor];
}
- (void)pushToAd {
AdvertiseViewController *next = [[AdvertiseViewController alloc] init];
next.urlString = @"http://www.baidu.com";
[self.navigationController pushViewController:next animated:YES];
}
以上就是最後的代碼邏輯