动态更新,iOS有三种处理方案
1,开源框架reactive native,但是编程语言是js
iOS app可以在运行时从服务器拉去最新的js文件到本读,然后执行,因为js是一门动态的脚本语言
所以可以在运行时直接读取js文件执行,也因此能够实现iOS的热更新
2, lua脚本
Lua脚本如同js一样,也能在动态时被。之前愤怒的小鸟使用lua脚本做的一个插件wax,
可以实现使用lua写iOS应用。热更新时,从服务器拉去lua脚本然后
动态的执行就可以了。遗憾的是wax目前已经不更新了。
3, 使用oc语言的动态库framework。用oc进行热更新
http://blog.csdn.net/jianrenbubai/article/details/50351507
===================
步骤:
1,创建framework工程
iOS----framework&library----cocoa touch framework
2,代码处理
写一个controller的控制工具类
- #import "HotUpdateControl.h"
- #import "AController.h"
- #import "BViewController.h"
- #import "CViewController.h"
- #import "DViewController.h"
- #import "EViewController.h"
- @implementation HotUpdateControl
- -(NSArray *)getVcs {
- return @[
- [[AController alloc]init],
- [[BViewController alloc]init],
- [[CViewController alloc]init],
- [[DViewController alloc]init],
- [[EViewController alloc]init]];
- }
- @end
好了,开始打包framework,为了避免打包出来的framework,在真机上面运行不了,我们使用一个脚本来进行打包,目的是多型号CPU核心的合成,就是打出一个通用的包。
# Sets the target folders and the final framework product.
# 如果工程名称和Framework的Target名称不一样的话,要自定义FMKNAME
# 例如: FMK_NAME = "MyFramework"
FMK_NAME=${PROJECT_NAME}
# Install dir will be the final output to the framework.
# The following line create it in the root folder of the current project.
INSTALL_DIR=${SRCROOT}/Products/${FMK_NAME}.framework
# Working dir will be deleted after the framework creation.
WRK_DIR=build
DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.framework
SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework
# -configuration ${CONFIGURATION}
# Clean and Building both architectures.
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphoneos clean build
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator clean build
# Cleaning the oldest.
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
mkdir -p "${INSTALL_DIR}"
cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"
# Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
lipo -create "${DEVICE_DIR}/${FMK_NAME}" "${SIMULATOR_DIR}/${FMK_NAME}" -output "${INSTALL_DIR}/${FMK_NAME}"
rm -r "${WRK_DIR}"
open "${INSTALL_DIR}"
点击1,添加一个脚本2----HotUpdateScript,然后点击3,产生4的Run Script,然后把脚本复制进去,再点击5,选择脚本打包编译
复制到另一个项目的沙箱里面去,就可以给另外的项目使用
33,建立一个主项目,就是使用这些动态库的工程
现在进行读取离线包的测试,只要这个项目,能够从沙箱里面读取到代码文件,就意味着可以在线更新代码,远程升级,
我修改了UITabBarConroller加载板块的初始化方法,如果沙箱有framework动态库,就加载framework动态库上面的版块,令到项目可以模块化
- //
- // TabController.m
- // HotUpdate
- //
- // Created by wukong on 15/12/18.
- // Copyright © 2015年 lhc. All rights reserved.
- //
- #import "TabController.h"
- //#import <HotUpdateMudel/HotUpdateControl.h>
- @interface TabController ()
- @end
- @implementation TabController
- -(instancetype)initWithCoder:(NSCoder *)aDecoder{
- if (self = [super initWithCoder:aDecoder]) {
- NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
- NSArray* arrFramework = [self getFilenamelistOfType:@"framework" fromDirPath:documentDirectory];
- NSLog(@"%@",arrFramework);
- if (arrFramework.count==0) {
- NSArray * arrTitle = @[@"首页",@"广场",@"朋友圈",@"我的",@"设置"];
- NSMutableArray * arrVcs = @[].mutableCopy;
- for (int i=0; i<arrTitle.count; i++) {
- UIViewController * vcRoot = [[UIViewController alloc]init];
- vcRoot.title = arrTitle[i];
- vcRoot.view.backgroundColor = [UIColor whiteColor];
- UINavigationController * navi = [[UINavigationController alloc]initWithRootViewController:vcRoot];
- [arrVcs addObject:navi];
- }
- [self setViewControllers:arrVcs animated:YES];
- }else{
- NSString *bundlePath = [NSString stringWithFormat:@"%@/%@",documentDirectory,[arrFramework lastObject]];
- if (![[NSFileManager defaultManager] fileExistsAtPath:bundlePath]) {
- NSLog(@"file not exist ,now return");
- return self;
- }
- NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
- if (!bundle || ![bundle load]) {
- NSLog(@"bundle load error");
- }
- Class loadClass = [bundle classNamed:@"HotUpdateControl"];
- if (!loadClass) {
- NSLog(@"get bundle class fail");
- return self;
- }
- NSObject *bundleObj = [loadClass new];
- NSArray * arrVc = [bundleObj performSelector:@selector(getVcs)];
- NSMutableArray * arrVcs = @[].mutableCopy;
- for (int i=0; i<arrVc.count; i++) {
- UIViewController * vcRoot =arrVc[i];
- vcRoot.view.backgroundColor = [UIColor whiteColor];
- UINavigationController * navi = [[UINavigationController alloc]initWithRootViewController:vcRoot];
- [arrVcs addObject:navi];
- }
- [self setViewControllers:arrVcs animated:YES];
- }
- }
- return self;
- }
- -(NSArray *) getFilenamelistOfType:(NSString *)type fromDirPath:(NSString *)dirPath
- {
- NSArray *fileList = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:nil]
- pathsMatchingExtensions:[NSArray arrayWithObject:type]];
- return fileList;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- }
- @end
NANANANANANANNANANANANANNANNANANANANANNANANANANANANANANANANANA
如果 是本地的默认版本,应该是
@[@"首页",@“广场”,@“朋友圈”,@“我的”,@“设置”]的模块
但如果是沙箱里面的模块,那么就应该是ABCDE
===================================================
Lua热更新
==========================================
Cocos2d-x已经封装了用于实现热更新功能的类,就是assetsmanager
api说明:
// 检测是否有版本更新
virtual bool checkUpdate();
// 下载更新的资源包并解压到下载路径
virtual void update();
// 获取当前客户端版本号
std::string getVersion();
// 删除客户端版本号
void deleteVersion();
// 设置下载回调(AssetsManagerDelegateProtocol)
void setDelegate(AssetsManagerDelegateProtocol *delegate);
// 设置连接超时时间(单位:秒)
void setConnectionTimeout(unsigned int timeout);
// 设置从服务端下载资源包的url
void setPackageUrl(const char* packageUrl);
// 设置服务端获取版本号的url
void setVersionFileUrl(const char* versionFileUrl);
// 设置资源保存路径
void setStoragePath(const char* storagePath);
因为AssetsManager使用了pthread库,所以需要在win32工程中需要包含pthread库所在目录。
VS在工程属性——C/C++——常规——附加包含目录中添加:$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\pthread
来自CODE的代码片
UpdateLayer.cpp
<a target=_blank id="L1" href="http://blog.csdn.net/linchaolong/article/details/42321767#L1" rel="#L1" style="text-decoration: none; color: rgb(12, 137, 207);"> 1</a> <a target=_blank id="L2" href="http://blog.csdn.net/linchaolong/article/details/42321767#L2" rel="#L2" style="text-decoration: none; color: rgb(12, 137, 207);"> 2</a> <a target=_blank id="L3" href="http://blog.csdn.net/linchaolong/article/details/42321767#L3" rel="#L3" style="text-decoration: none; color: rgb(12, 137, 207);"> 3</a> <a target=_blank id="L4" href="http://blog.csdn.net/linchaolong/article/details/42321767#L4" rel="#L4" style="text-decoration: none; color: rgb(12, 137, 207);"> 4</a> <a target=_blank id="L5" href="http://blog.csdn.net/linchaolong/article/details/42321767#L5" rel="#L5" style="text-decoration: none; color: rgb(12, 137, 207);"> 5</a> <a target=_blank id="L6" href="http://blog.csdn.net/linchaolong/article/details/42321767#L6" rel="#L6" style="text-decoration: none; color: rgb(12, 137, 207);"> 6</a> <a target=_blank id="L7" href="http://blog.csdn.net/linchaolong/article/details/42321767#L7" rel="#L7" style="text-decoration: none; color: rgb(12, 137, 207);"> 7</a> <a target=_blank id="L8" href="http://blog.csdn.net/linchaolong/article/details/42321767#L8" rel="#L8" style="text-decoration: none; color: rgb(12, 137, 207);"> 8</a> <a target=_blank id="L9" href="http://blog.csdn.net/linchaolong/article/details/42321767#L9" rel="#L9" style="text-decoration: none; color: rgb(12, 137, 207);"> 9</a> <a target=_blank id="L10" href="http://blog.csdn.net/linchaolong/article/details/42321767#L10" rel="#L10" style="text-decoration: none; color: rgb(12, 137, 207);"> 10</a> <a target=_blank id="L11" href="http://blog.csdn.net/linchaolong/article/details/42321767#L11" rel="#L11" style="text-decoration: none; color: rgb(12, 137, 207);"> 11</a> <a target=_blank id="L12" href="http://blog.csdn.net/linchaolong/article/details/42321767#L12" rel="#L12" style="text-decoration: none; color: rgb(12, 137, 207);"> 12</a> <a target=_blank id="L13" href="http://blog.csdn.net/linchaolong/article/details/42321767#L13" rel="#L13" style="text-decoration: none; color: rgb(12, 137, 207);"> 13</a> <a target=_blank id="L14" href="http://blog.csdn.net/linchaolong/article/details/42321767#L14" rel="#L14" style="text-decoration: none; color: rgb(12, 137, 207);"> 14</a> <a target=_blank id="L15" href="http://blog.csdn.net/linchaolong/article/details/42321767#L15" rel="#L15" style="text-decoration: none; color: rgb(12, 137, 207);"> 15</a> <a target=_blank id="L16" href="http://blog.csdn.net/linchaolong/article/details/42321767#L16" rel="#L16" style="text-decoration: none; color: rgb(12, 137, 207);"> 16</a> <a target=_blank id="L17" href="http://blog.csdn.net/linchaolong/article/details/42321767#L17" rel="#L17" style="text-decoration: none; color: rgb(12, 137, 207);"> 17</a> <a target=_blank id="L18" href="http://blog.csdn.net/linchaolong/article/details/42321767#L18" rel="#L18" style="text-decoration: none; color: rgb(12, 137, 207);"> 18</a> <a target=_blank id="L19" href="http://blog.csdn.net/linchaolong/article/details/42321767#L19" rel="#L19" style="text-decoration: none; color: rgb(12, 137, 207);"> 19</a> <a target=_blank id="L20" href="http://blog.csdn.net/linchaolong/article/details/42321767#L20" rel="#L20" style="text-decoration: none; color: rgb(12, 137, 207);"> 20</a> <a target=_blank id="L21" href="http://blog.csdn.net/linchaolong/article/details/42321767#L21" rel="#L21" style="text-decoration: none; color: rgb(12, 137, 207);"> 21</a> <a target=_blank id="L22" href="http://blog.csdn.net/linchaolong/article/details/42321767#L22" rel="#L22" style="text-decoration: none; color: rgb(12, 137, 207);"> 22</a> <a target=_blank id="L23" href="http://blog.csdn.net/linchaolong/article/details/42321767#L23" rel="#L23" style="text-decoration: none; color: rgb(12, 137, 207);"> 23</a> <a target=_blank id="L24" href="http://blog.csdn.net/linchaolong/article/details/42321767#L24" rel="#L24" style="text-decoration: none; color: rgb(12, 137, 207);"> 24</a> <a target=_blank id="L25" href="http://blog.csdn.net/linchaolong/article/details/42321767#L25" rel="#L25" style="text-decoration: none; color: rgb(12, 137, 207);"> 25</a> <a target=_blank id="L26" href="http://blog.csdn.net/linchaolong/article/details/42321767#L26" rel="#L26" style="text-decoration: none; color: rgb(12, 137, 207);"> 26</a> <a target=_blank id="L27" href="http://blog.csdn.net/linchaolong/article/details/42321767#L27" rel="#L27" style="text-decoration: none; color: rgb(12, 137, 207);"> 27</a> <a target=_blank id="L28" href="http://blog.csdn.net/linchaolong/article/details/42321767#L28" rel="#L28" style="text-decoration: none; color: rgb(12, 137, 207);"> 28</a> <a target=_blank id="L29" href="http://blog.csdn.net/linchaolong/article/details/42321767#L29" rel="#L29" style="text-decoration: none; color: rgb(12, 137, 207);"> 29</a> <a target=_blank id="L30" href="http://blog.csdn.net/linchaolong/article/details/42321767#L30" rel="#L30" style="text-decoration: none; color: rgb(12, 137, 207);"> 30</a> <a target=_blank id="L31" href="http://blog.csdn.net/linchaolong/article/details/42321767#L31" rel="#L31" style="text-decoration: none; color: rgb(12, 137, 207);"> 31</a> <a target=_blank id="L32" href="http://blog.csdn.net/linchaolong/article/details/42321767#L32" rel="#L32" style="text-decoration: none; color: rgb(12, 137, 207);"> 32</a> <a target=_blank id="L33" href="http://blog.csdn.net/linchaolong/article/details/42321767#L33" rel="#L33" style="text-decoration: none; color: rgb(12, 137, 207);"> 33</a> <a target=_blank id="L34" href="http://blog.csdn.net/linchaolong/article/details/42321767#L34" rel="#L34" style="text-decoration: none; color: rgb(12, 137, 207);"> 34</a> <a target=_blank id="L35" href="http://blog.csdn.net/linchaolong/article/details/42321767#L35" rel="#L35" style="text-decoration: none; color: rgb(12, 137, 207);"> 35</a> <a target=_blank id="L36" href="http://blog.csdn.net/linchaolong/article/details/42321767#L36" rel="#L36" style="text-decoration: none; color: rgb(12, 137, 207);"> 36</a> <a target=_blank id="L37" href="http://blog.csdn.net/linchaolong/article/details/42321767#L37" rel="#L37" style="text-decoration: none; color: rgb(12, 137, 207);"> 37</a> <a target=_blank id="L38" href="http://blog.csdn.net/linchaolong/article/details/42321767#L38" rel="#L38" style="text-decoration: none; color: rgb(12, 137, 207);"> 38</a> <a target=_blank id="L39" href="http://blog.csdn.net/linchaolong/article/details/42321767#L39" rel="#L39" style="text-decoration: none; color: rgb(12, 137, 207);"> 39</a> <a target=_blank id="L40" href="http://blog.csdn.net/linchaolong/article/details/42321767#L40" rel="#L40" style="text-decoration: none; color: rgb(12, 137, 207);"> 40</a> <a target=_blank id="L41" href="http://blog.csdn.net/linchaolong/article/details/42321767#L41" rel="#L41" style="text-decoration: none; color: rgb(12, 137, 207);"> 41</a> <a target=_blank id="L42" href="http://blog.csdn.net/linchaolong/article/details/42321767#L42" rel="#L42" style="text-decoration: none; color: rgb(12, 137, 207);"> 42</a> <a target=_blank id="L43" href="http://blog.csdn.net/linchaolong/article/details/42321767#L43" rel="#L43" style="text-decoration: none; color: rgb(12, 137, 207);"> 43</a> <a target=_blank id="L44" href="http://blog.csdn.net/linchaolong/article/details/42321767#L44" rel="#L44" style="text-decoration: none; color: rgb(12, 137, 207);"> 44</a> <a target=_blank id="L45" href="http://blog.csdn.net/linchaolong/article/details/42321767#L45" rel="#L45" style="text-decoration: none; color: rgb(12, 137, 207);"> 45</a> <a target=_blank id="L46" href="http://blog.csdn.net/linchaolong/article/details/42321767#L46" rel="#L46" style="text-decoration: none; color: rgb(12, 137, 207);"> 46</a> <a target=_blank id="L47" href="http://blog.csdn.net/linchaolong/article/details/42321767#L47" rel="#L47" style="text-decoration: none; color: rgb(12, 137, 207);"> 47</a> <a target=_blank id="L48" href="http://blog.csdn.net/linchaolong/article/details/42321767#L48" rel="#L48" style="text-decoration: none; color: rgb(12, 137, 207);"> 48</a> <a target=_blank id="L49" href="http://blog.csdn.net/linchaolong/article/details/42321767#L49" rel="#L49" style="text-decoration: none; color: rgb(12, 137, 207);"> 49</a> <a target=_blank id="L50" href="http://blog.csdn.net/linchaolong/article/details/42321767#L50" rel="#L50" style="text-decoration: none; color: rgb(12, 137, 207);"> 50</a> <a target=_blank id="L51" href="http://blog.csdn.net/linchaolong/article/details/42321767#L51" rel="#L51" style="text-decoration: none; color: rgb(12, 137, 207);"> 51</a> <a target=_blank id="L52" href="http://blog.csdn.net/linchaolong/article/details/42321767#L52" rel="#L52" style="text-decoration: none; color: rgb(12, 137, 207);"> 52</a> <a target=_blank id="L53" href="http://blog.csdn.net/linchaolong/article/details/42321767#L53" rel="#L53" style="text-decoration: none; color: rgb(12, 137, 207);"> 53</a> <a target=_blank id="L54" href="http://blog.csdn.net/linchaolong/article/details/42321767#L54" rel="#L54" style="text-decoration: none; color: rgb(12, 137, 207);"> 54</a> |