-
Notifications
You must be signed in to change notification settings - Fork 97
iOS Gettingstarted
zhangyanqiang edited this page Apr 26, 2018
·
1 revision
小程序启动会有两种情况,一种是「冷启动」,一种是「热启动」。 假如用户已经打开过某小程序,然后在一定时间内再次打开该小程序,此时无需重新启动,只需将后台态的小程序切换到前台,这个过程就是热启动;冷启动指的是用户首次打开,此时小程序会重新加载启动。
小程序冷启动时回去本地资源包中查询version文件,根据version文件中的base_version 的版本来获取本地的base包和所需也业务包,查询包的过程会先在本地文件路径中查询,如果没查到回去资源包中查询,如果都没有会异步的从服务器下载,下载后的zip文件,会解压到本地的文件夹,下次启动可以直接在本地获取资源,
小程序是基于viewcontroller 来创建的,所以小程序的生命周期和该 ViewController 相同
-
创建 QIYIMPViewNavController
QIYIMPViewNavController* vc = [QIYIMPViewNavController alloc];
-
实现 QIYIAsset 接口,实现 QIYIMPViewControllerDelegate
_asset = [[QIYIAssetsImpl alloc] init]; _delegateImpl = [[QIYIDelegateImpl alloc] init];
-
跳转到 Lite-App ViewController
[self presentViewController:vc animated:YES completion:^{ }];
-
获取所需的资源文件包(两种方式静态-本地缓存 或者 动态-网络获取)
self.updateSuccessCallback = complement; __weak QIYIConstitute* ws = self; if ([ws compareVersion]) { [ws comparePackage]; }else{ [ws request:self.project parameter:__QIYI_LOCAL_VERSION callback:^(BOOL callback) { }]; }
-
加载manifest文件,根据manifest文件加载 Tabbar、Navigationbar,Webview等UI
-
Tabbar
QIYIManifestTabbarBundle* tabbar = [self.manifest obtainTabbarBundle]; NSUInteger showCount = 0; NSString* name = [self.manifest ontainMainPage]; if (tabbar && tabbar.items && self.asset) { pagesCount = tabbar.items.count; if (pagesCount > 1) { tabbarView = [[QIYITabbar alloc] init]; for (NSUInteger i = 0; i < pagesCount; ++i) { QIYIManifestTabbarBundleItem* bundleItem = tabbar.items[i]; if (bundleItem) { QIYITabbarItem* item = [[QIYITabbarItem alloc] init]; item.title = bundleItem.title; item.titleSelectedColor = bundleItem.titleSelectedColor; item.titleUnSelectedColor = bundleItem.titleUnSelectedColor; item.selectedIcon = [UIImage imageWithData:[self.asset obtainFile:bundleItem.selectedIcon]]; item.unselectedIcon = [UIImage imageWithData:[self.asset obtainFile:bundleItem.unselectedIcon]]; [item construct]; [tabbarView addItem:item]; if ([bundleItem.name isEqualToString:name]) { showCount = i; item.selected = true; } } } } } if (nil != tabbarView) { tabbarViewHeight = tabbarView.frame.size.height; float tabbarViewTop = __SCREEN_HEIGHT - tabbarViewHeight; tabbarView.frame = CGRectMake(0, tabbarViewTop, __SCREEN_WIDTH, tabbarViewHeight); [self.view addSubview:tabbarView]; __weak QIYIMPMainViewController* ws = self; tabbarView.bOnTabbarSelected = ^(NSUInteger index) { if (ws && self.container) { [self.container show:index]; } }; }
-
NavigationBar
QIYINavigationBar* bar = [[QIYINavigationBar alloc] init]; self.navigationbarView = bar; [self.view addSubview:self.navigationbarView]; if ([self.navigationbarView conformsToProtocol:@protocol(QIYINavigationBarBase)]) { self.navigationbar = bar; }
-
Webview
self.container = [[QIYIContainerCombine alloc] init:pagesCount<1 ? 1 : pagesCount]; float contianerHeight = __SCREEN_HEIGHT - navigationBarHeight - tabbarViewHeight; self.container.frame = CGRectMake(0, navigationBarHeight, __SCREEN_WIDTH, contianerHeight); if (tabbar && tabbar.items && self.asset) { for (NSUInteger i = 0; i < pagesCount; ++i) { QIYIManifestTabbarBundleItem* bundleItem = tabbar.items[i]; if (bundleItem) { QIYIContainer* container = [self.container at:i]; if (container) { container.name = bundleItem.name; [self configContainer:container]; } } } if (pagesCount == 0) { QIYIContainer* container = [self.container at:0]; if (container) { container.name = name; [self configContainer:container]; } } } [self.view addSubview:self.container]; [self.container show:showCount];
-
-
注入 bundle.js 到自定义的js执行器中
NSString* json = @"{}"; json = [@"__page__data =" stringByAppendingString:json]; NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding]; [container evaluateScript:data];
-
展示最终页面