启动页加载远程广告的简单实现

来源:互联网 发布:unity3d 2017 新功能 编辑:程序博客网 时间:2024/05/02 17:51

启动页在加载完毕会自动执行AppDelegate didFinishLaunchingWithOptions 函数


- (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


效果图:




0 0
原创粉丝点击