IOS 热更新,实时代码更新,动态更新,动态库framework
只要审核通过后,无须再次审核,就可以动态更新,iOS有三种处理方案:
一、开源框架 reactive native,但是编程语言是js
二、lua 脚本
三、使用OC语言 的动态库framework。
前两者,我不打算细讲,我主要介绍怎么用oc进行热更新
1、创建framework工程
创建framework工程2、代码处理:
处理代码写一个controller的控制工具类:
// HotUpdateControl.m
// HotUpdateMudel
//
// Created by wukong on 15/12/18.
// Copyright © 2015年 lhc. All rights reserved.
//
#import "HotUpdateControl.h"
#import "AController.h"
#import "BViewController.h"
#import "CViewController.h"
#import "DViewController.h"
#import "EViewController.h"
@implementationHotUpdateControl
-(NSArray*)getVcs {
return@[
[[AControlleralloc]init],
[[BViewControlleralloc]init],
[[CViewControlleralloc]init],
[[DViewControlleralloc]init],
[[EViewControlleralloc]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}"
3、建立一个主项目,就是使用这些动态库的工程
现在进行读取离线包的测试,只要这个项目,能够从沙箱里面读取到代码文件,就意味着可以在线更新代码,远程升级!!!
动态库已经加载到了沙箱~~~
我修改了UITabBarController加载版块的初始化方法,如果沙箱有framework动态库,就加载framework动态库上面的版块,令到项目可以模块化
// TabController.m
// HotUpdate
//
// Created by wukong on 15/12/18.
// Copyright © 2015年 lhc. All rights reserved.
//
#import "TabController.h"
//#import
@interfaceTabController ()
@end
@implementationTabController
-(instancetype)initWithCoder:(NSCoder*)aDecoder{
if(self= [superinitWithCoder:aDecoder]) {
NSString*documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES)lastObject];
NSArray* arrFramework = [selfgetFilenamelistOfType:@"framework"fromDirPath:documentDirectory];
NSLog(@"%@",arrFramework);
if(arrFramework.count==0) {
NSArray* arrTitle = @[@"首页",@"广场",@"朋友圈",@"我的",@"设置"];
NSMutableArray* arrVcs = @[].mutableCopy;
for(inti=0; i
UIViewController* vcRoot = [[UIViewControlleralloc]init];
vcRoot.title= arrTitle[i];
vcRoot.view.backgroundColor= [UIColorwhiteColor];
UINavigationController* navi = [[UINavigationControlleralloc]initWithRootViewController:vcRoot];
[arrVcsaddObject:navi];
}
[selfsetViewControllers:arrVcsanimated:YES];
}else{
NSString*bundlePath = [NSStringstringWithFormat:@"%@/%@",documentDirectory,[arrFrameworklastObject]];
if(![[NSFileManagerdefaultManager]fileExistsAtPath:bundlePath]) {
NSLog(@"file not exist ,now return");
returnself;
}
NSBundle*bundle = [NSBundlebundleWithPath:bundlePath];
if(!bundle || ![bundleload]) {
NSLog(@"bundle load error");
}
Class loadClass = [bundleclassNamed:@"HotUpdateControl"];
if(!loadClass) {
NSLog(@"get bundle class fail");
returnself;
}
NSObject*bundleObj = [loadClassnew];
NSArray* arrVc = [bundleObjperformSelector:@selector(getVcs)];
NSMutableArray* arrVcs = @[].mutableCopy;
for(inti=0; i
UIViewController* vcRoot =arrVc[i];
vcRoot.view.backgroundColor= [UIColorwhiteColor];
UINavigationController* navi = [[UINavigationControlleralloc]initWithRootViewController:vcRoot];
[arrVcsaddObject:navi];
}
[selfsetViewControllers:arrVcsanimated:YES];
}
}
returnself;
}
-(NSArray*)getFilenamelistOfType:(NSString*)typefromDirPath:(NSString*)dirPath
{
NSArray*fileList = [[[NSFileManagerdefaultManager]contentsOfDirectoryAtPath:dirPatherror:nil]
pathsMatchingExtensions:[NSArrayarrayWithObject:type]];
returnfileList;
}
- (void)viewDidLoad {
[superviewDidLoad];
}
@end
看结果了,如果是本地的默认版本,应该是
@[@"首页",@"广场",@"朋友圈",@"我的",@"设置"];的模块
但是如果是沙箱里面的模块
那么就应该ABCDE
good luck!
原文链接http://blog.csdn.net/jianrenbubai/article/details/50351507
网友评论