1、在AppDelegate启动方法中切换根控制器为广告展示控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ADVC *vc = [[ADVC alloc] init];
self.window.rootViewController = vc;
return YES;
}
2、广告图控制器
#import "ADVC.h"
@interface ADVC ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *skipButton;
@property (strong, nonatomic) NSTimer *skipTimer;
@end
static NSInteger kSkipTimeout = 5;
static NSString *const kNeedShowADYES = @"YES";
static NSString *const kNeedShowADKey = @"isNeedShowAD";
static NSString *const kADImagePathKey = @"ADImagePath";
@implementation ADVC
- (void)viewDidLoad {
[super viewDidLoad];
[self setupADImage];
}
#pragma mark - UI交互
- (IBAction)skipButtonClicked:(id)sender {
[self endADShow];
}
- (IBAction)ADButtonClicked:(id)sender {
// 用 NSUserDefaults 配置值来标识用户在广告业是否点击了广告,此方法比采用通知的方法更稳定
[[NSUserDefaults standardUserDefaults] setValue:kNeedShowADYES forKey:kNeedShowADKey];
[[NSUserDefaults standardUserDefaults] synchronize];
[self endADShow];
}
#pragma mark - UI设置
- (void)setupADImage {
NSString *imagePath = (NSString *)[[NSUserDefaults standardUserDefaults] valueForKey:kADImagePathKey];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
if (image) {
self.imageView.image = image;
[self setupSkipButtonTitle];
self.skipButton.hidden = NO;
// 开启倒计时
self.skipTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(skipTimerAction) userInfo:nil repeats:YES];
}else{
[self endADShow];
}
}
- (void)setupSkipButtonTitle {
NSString *title = [NSString stringWithFormat:@"%ldS 跳过", kSkipTimeout];
[self.skipButton setTitle:title forState:UIControlStateNormal];
}
#pragma mark - otherMethods
- (void)skipTimerAction {
if (kSkipTimeout >= 0) {
[self setupSkipButtonTitle];
kSkipTimeout -= 1;
return;
}
[self endADShow];
}
- (void)endADShow {
[self.skipTimer invalidate];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabBarVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"tabbarvc"];
[UIApplication sharedApplication].delegate.window.rootViewController = tabBarVC;
}
@end
3、在需要启动广告业详情的控制器中
#import "ViewController.h"
#import "ADDetailsVC.h"
@interface ViewController ()
@end
static NSString *const kNeedShowADKey = @"isNeedShowAD";
static NSString *const kADImagePathKey = @"ADImagePath";
static NSString *const kADImageName = @"launchImageName";
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated {
// 检查是否有点击广告,如有则打开广告详情页并删除有点击广告的标记
if ([[NSUserDefaults standardUserDefaults] valueForKey:kNeedShowADKey]) {
ADDetailsVC *vc = [[ADDetailsVC alloc] init];
[self.navigationController pushViewController:vc animated:YES];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kNeedShowADKey];
}
// 无论如何都在此重新加载广告信息,并存储广告图片和广告链接,还要记得删除旧的图片(这步其实可以交给SDWebImage)
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic143.nipic.com/file/20171016/3046502_091455265001_2.jpg"]];
NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = (NSString *)[filePaths objectAtIndex:0];
NSString *imageSavePath = [path stringByAppendingPathComponent:kADImageName];
[imageData writeToFile:imageSavePath atomically:YES];
// 存储图片路径
[[NSUserDefaults standardUserDefaults] setValue:imageSavePath forKey:kADImagePathKey];
}
@end
4、注意在启动广告详情的控制器中,最好把push详情控制器放在 - (void)viewDidAppear:(BOOL)animated 方法中,避免转场动画冲突造成push不成功。
网友评论