PM 提了个需求,启动广告界面点击了, 就直接进,不须要闪一下主界面。git
实现的思路,多 window 模式
-github
通常的广告界面就是 Key Window 上面的遮罩(子视图)。缓存
要从广告界面 push,天然广告界面是一个控制器了。
提高广告界面的层级,才能够。网络
这里提高为 window 。而后就是切换展现的窗口了,经过系统单例 AppDelegate , [delegate.window makeKeyAndVisible];
app
若是使用的是控制器, 单 window (就是系统默认建立的), 切换就是 [UIApplication sharedApplication].keyWindow.rootViewController = ...
框架
两点考虑:异步
1, 但愿启动的时候首页早加载,就必须给首页一个 window 容器。动画
若是使用的是控制器, 从广告到首页,切换即从新建立。atom
2,能够在内存上复用。首页转登陆,登陆再转首页spa
以前项目用的是切换 keyWindow 的 rootViewController, 而后首页就丢了,拿不到。登陆逻辑中,新建一个,以为有点惋惜。
使用多 window , 经过系统单例 (AppDelegate *)UIApplication.sharedApplication.delegate
, 拿回首页,能够在内存上复用。
- (void)backToWindow{ AppDelegate * delegate = (AppDelegate *)UIApplication.sharedApplication.delegate; [delegate.window makeKeyAndVisible]; }
项目中采用了三个 window, 一个主流程,一个广告, 一个登陆,
KeyAndVisible 的是广告 window.
怎么办?
由于要等一个网络请求(网络请求广告接口),失败了,就进主界面。 成功了,接着请求图片(使用了两级缓存)
延长启动时间的时候,不能影响体验,不能被系统的 daemon 干掉。
就要用 trick 了。
根控制器是广告控制器。
self.adWindow.rootViewController = [[UINavigationController alloc] initWithRootViewController: [[AdvertiseViewController alloc] init]];
广告控制器的背景图,来一个占位图,就是启动图。
( 项目缘由,广告都是实时请求。不是市面上的异步缓存,下次使用。 华新镇风味)
@interface AppDelegate // 主流程 @property (strong, nonatomic) UIWindow *window; // 广告 @property (strong, nonatomic) UIWindow *adWindow; // 登陆 @property (strong, nonatomic) UIWindow *loginWindow;
@implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] init]; self.window.frame = [UIScreen mainScreen].bounds; FrontTabBarController * tabBarController = [[FrontTabBarController alloc] init]; tabBarController.view.backgroundColor = UIColor.redColor; self.window.rootViewController = tabBarController; self.adWindow = [[UIWindow alloc] init]; self.adWindow.frame = [UIScreen mainScreen].bounds; self.adWindow.rootViewController = [[UINavigationController alloc] initWithRootViewController: [[AdvertiseViewController alloc] init]];; [self.adWindow makeKeyAndVisible]; return YES; }
须要在 Target 的 General 选项卡中, 指定 Main Interface 为空。
建议删除 main.storyboard.
由于启动的时候切换 window, 就不能走 main.storyboard 了.
缺点:
-
1, 没有转场动画 transition。 退出广告界面,刷的一下出现了主界面,没有 pop.
2, 内存管理, 由于用户看到的 window 只有一个。
广告业务处理完后,切换回首页 window, 就应该释放广告 window.
( 广告 window 是 appDelegate 的属性,appDelegate 是单例, 因此 appDelegate 的广告 window 属性,初始化后,会一直都存在)
在这个时机写, appDelegate.adWindow = nil
, 就会黑光一闪。
这确定是不行的。
须要在以后找地方释放,代码会乱一些。
iOS 启动广告界面,通常是在 window 上添加子视图。
UIWindow *window = [UIApplication sharedApplication].delegate.window; [window addSubview: self];
广告界面就是首页 rootViewController ( 或者其子 Controller )上面的遮罩。点击广告页,通常是通知首页的 topViewController push 出广告的内容控制器。
搜到的 App 启动加载广告页面思路都这么写。 这篇不错。
这种思路挺好的,通常的广告界面的 link 是 deep link, 与 app 强交互,先把整个 app 的框架(首页)加载出来,进入业务层的世界。
大方:
看了一下喜马拉雅的效果,也以为挺好的。
华新镇的风味.
相关代码: https://github.com/BoxDengJZ/...
-
能够网络请求异步发起,下次使用,AFNetworking 的事情。
图片缓存策略,SDWebImage 的事情。
倒计时功能, timer 。 广告界面的图片上, 加手势跳转。
(设想过,延长系统的启动时间,开一条新的线程出来,进行网络请求,在其回调中通知主线程继续 。NaN )