您的位置:首页 > 移动开发 > IOS开发

IOS 热更新,实时代码更新,动态更新,动态库framework

2016-10-28 23:29 302 查看
2015-12-18 14:47 2660人阅读 评论(1) 收藏 举报

版权声明:本文为博主原创文章,未经博主允许不得转载。

  允许转载!
demo

下载

  只要审核通过后,无须再次审核,就可以动态更新,iOS有三种处理方案:
   一、开源框架 reactive native,但是编程语言是js
  二、lua 脚本
  三、使用oc语言的动态库framework。前两者,我不打算细讲,我主要介绍怎么用oc进行热更新
      1、创建framework工程



      2、代码处理:



写一个controller的控制工具类:

[objc] view
plain copy

 print?





//  

//  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"  

@implementation HotUpdateControl  

  

-(NSArray *)getVcs {  

      

    return @[  

             [[AController alloc]init],  

             [[BViewController alloc]init],  

             [[CViewController alloc]init],  

             [[DViewController alloc]init],  

             [[EViewController alloc]init]];  

      

}  

@end  

好了,开始打包framework,为了以免打包出来的framework,在真机上面运行不了,我们使用一个脚本来进行打包,目的是多型号CPU核心的合成,就是打出一个通用的包。

[python] view
plain copy

 print?





# 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动态库上面的版块,令到项目可以模块化

[objc] view
plain copy

 print?





//  

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

看结果了,如果是本地的默认版本,应该是

@[@"首页",@"广场",@"朋友圈",@"我的",@"设置"];的模块

但是如果是沙箱里面的模块
那么久应该ABCDE



good luck
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: