启动页加载远程广告的简单实现
2015-12-31 14:13
621 查看
启动页在加载完毕会自动执行AppDelegate didFinishLaunchingWithOptions 函数
所以直接将加载广告代码写在这个函数里面就对了:
AppDelegate.m
效果图:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. return YES; }
所以直接将加载广告代码写在这个函数里面就对了:
AppDelegate.m
// // AppDelegate.m // TBTest // // Created by addcn591 on 15/12/30. // Copyright © 2015年 Addcn. All rights reserved. // #import "AppDelegate.h" @interface AppDelegate () //启动页view @property (strong, nonatomic) UIView *LaunchView; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //创建window self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; //获取启动页 UIStoryboard *LaunchScreen = [UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil]; UIViewController *LaunchVC = [LaunchScreen instantiateInitialViewController]; self.LaunchView = LaunchVC.view; //加载远程广告 [self loadAD]; //设置rootView self.window.rootViewController = LaunchVC; [self.window makeKeyAndVisible]; return YES; } #pragma mark 加载远程广告 - (void)loadAD { //远程图片地址 NSURL *url = [NSURL URLWithString:@"http://images.weiphone.net/attachments/photo/Day_120201/57319_b74b13280821486dc66ef44cea6c5.png"]; //异步加载图片 dispatch_queue_t queue = dispatch_queue_create("loadImage", NULL); dispatch_async(queue, ^{ //图片资源 NSData *imgData = [NSData dataWithContentsOfURL:url]; UIImage *img = [UIImage imageWithData:imgData]; dispatch_async(dispatch_get_main_queue(), ^{ //创建imageView UIImageView *adView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)]; adView.image = img; //添加图片点击手势 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAD)]; adView.userInteractionEnabled = YES; [adView addGestureRecognizer:singleTap]; [self.LaunchView addSubview:adView]; //LaunchView前置于window [self.window bringSubviewToFront:self.LaunchView]; //延迟5秒后删除广告 [self performSelector:@selector(removeAd) withObject:nil afterDelay:5]; }); }); } #pragma mark 广告点击 - (void)clickAD { [self removeAd]; } #pragma mark 删除广告 - (void)removeAd { [self.LaunchView removeFromSuperview]; //重新设置rootView UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; self.window.rootViewController = [main instantiateInitialViewController]; [self.window makeKeyAndVisible]; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
效果图:
相关文章推荐
- 转:windows BAT 批处理脚本教程
- iOS -手机号的判断
- Docker php 环境搭建dockerfile
- LOCAL_LDLIBS := -llog
- web前端基本用法(二)
- Android 圆角边框
- iOS图片处理(一)调用系统相机和相册获取图片,给相机添加自定义覆盖物
- linux命令之 jar
- 启动Tomcat提示:指定的服务未安装
- java 设计模式
- 【常用工具类】DensityUtils(dp px 互相转换)
- POI实现Excl文件的导入
- javascript禁止表单输入
- Web Scraping with Python 学习笔记9
- 嵌入式系统WinCE下应用程序GUI界面开发
- mysql 5.7.10使用dbforget Studio 连接异常
- <五>RecycleView+CardView实现瀑布流(类in界面效果)
- CSS3:text-overflow实现文字截取,超出部分显示省略号
- Qt学习(15)
- 递归函数