眼下可以实现热更新的方法,总结起来有下面三种

1. 使用FaceBook 的开源框架 reactive native,使用js写原生的ios应用

ios app能够在执行时从server拉取最新的js文件到本地。然后执行,由于js是一门动态的

脚本语言。所以可以在执行时直接读取js文件执行,也因此可以实现ios的热更新

2. 使用lua 脚本。lua脚本如同js 一样,也能在动态时被。之前愤慨的小鸟使用

lua脚本做的一个插件 wax,能够实现使用lua写ios应用。热更新时,从server拉去lua脚本

然后动态的运行就能够了。遗憾的是 wax眼下已经不更新了。

上面是网上如今可以搜到的热更新方法。

xcode 6 之后,苹果开放了 ios 的动态库编译权限。所谓的动态库。事实上就是能够在执行时载入。

正好利用这一个特性,用来做ios的热更新。

1.

建立一个动态库。如图:

动态库包括须要使用的viewCOntroller,当然能够包括不论什么须要使用的自己定义ui和逻辑。

动态库的入口是一个jkDylib的类。它的.h和.m文件分别例如以下:

//
// JKDylib.h
// JKDylb
//
// Created by wangdan on 15/7/5.
// Copyright (c) 2015年 wangdan. All rights reserved.
// #import <Foundation/Foundation.h> @interface JKDylib : NSObject -(void)showViewAfterVC:(id)fromVc inBundle:(NSBundle*)bundle; @end

.m文件

//
// JKDylib.m
// JKDylb
//
// Created by wangdan on 15/7/5.
// Copyright (c) 2015年 wangdan. All rights reserved.
// #import "JKDylib.h"
#import "JKViewController.h" @implementation JKDylib -(void)showViewAfterVC:(id)fromVc inBundle:(NSBundle*)bundle
{
if (fromVc == nil ) {
return;
} JKViewController *vc = [[JKViewController alloc] init];
UIViewController *preVc = (UIViewController *)fromVc; if (preVc.navigationController) {
[preVc.navigationController pushViewController:vc animated:YES];
}
else {
UINavigationController *navi = [[UINavigationController alloc] init];
[navi pushViewController:vc animated:YES];
} }
@end

上述代码意图很明显,

就是调用该动态库的时候

-(void)showViewAfterVC:(id)fromVc inBundle:(NSBundle*)bundle

在该函数中,创建一个viewController 然后使用mainBundler 的navigationController  push 新建的viewController,显示动态库的ui界面。

而动态库中的JKViewController 内容则能够依据须要随便定义。

2. 完毕上述动态库的编译工作后,如今须要做的就是在主project中。写一段载入该动态库的代码。

主project文件夹例如以下:

在最重要的viewCotrooler里面,定义了载入动态库的方法:

//
// ViewController.m
// DylibTest
//
// Created by wangdan on 15/7/5.
// Copyright (c) 2015年 wangdan. All rights reserved.
// #import "ViewController.h"
#import "AFNetWorking.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"bundle test"; AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
[manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"request success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"request failure");
}]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 100, 50)];
btn.backgroundColor = [UIColor blueColor]; [btn addTarget:self
action:@selector(btnHandler)
forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; BOOL writeResult =
[@"hellow" writeToFile:[NSString stringWithFormat:@"%@/%@",document,@"hello.plist"] atomically:YES encoding:NSUTF8StringEncoding error:nil]; // Do any additional setup after loading the view, typically from a nib.
} -(void)btnHandler
{ //AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
//manager.responseSerializer = [AFJSONResponseSerializer serializer];
// NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
// [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
// NSLog(@"request success");
// } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// NSLog(@"request failure");
//}]; NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *bundlePath = [NSString stringWithFormat:@"%@/%@",documentDirectory,@"JKDylb.framework"]; if (![[NSFileManager defaultManager] fileExistsAtPath:bundlePath]) {
NSLog(@"file not exist ,now return");
return;
}
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath]; if (!bundle || ![bundle load]) {
NSLog(@"bundle load error");
} Class loadClass = [bundle principalClass];
if (!loadClass) {
NSLog(@"get bundle class fail");
return;
}
NSObject *bundleObj = [loadClass new];
[bundleObj performSelector:@selector(showViewAfterVC:inBundle:) withObject:self withObject:bundle]; NSString *framePath = [[NSBundle mainBundle] privateFrameworksPath];
NSLog(@"framePath is %@",framePath); NSLog(@"file attri \n %@",bundle.localizations); // [bundleObj showViewAfterVC:self inBundle:bundle];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

viewController视图中有一个button,点击button后,从 document文件夹以下找到动态库(尽管此时document下并没有动态库),动态库的名称约定好味

JKDylib.framework

然后使用NSBundle 载入该动态库,详细见代码。

载入成功后。调用在动态库中实现的方法

 [bundleObj performSelector:@selector(showViewAfterVC:inBundle:) withObject:self withObject:bundle];
    

编译该project。然后执行到手机上,然后退出该程序

3. 打开itunes 然后将动态库同步到刚才的測试project文件夹下。

4.在此打开測试project程序。点击button,则会发现可以进入在动态库中定义的ui界面了。

上面project的參考代码 在

http://download.csdn.net/detail/j_akill/8891881

关于动态更新的思考:

採用动态库方式实现热更新事实上还是有一个问题。就是怎样在主project和动态库之间共享组建

比方网络组件以及其它等等第三方组件。

眼下我没发现好方法,仅仅能在动态库和主project之间分别加入而且编译。

最新文章

  1. Oracle Database 12c Release 1下载安装(自身经历)
  2. Azure PowerShell (7) 使用CSV文件批量设置Virtual Machine Endpoint
  3. 关于jQuery事件绑定
  4. Html标签第一课
  5. C# 时间函数相减
  6. R语言多重共现性的检测
  7. Spring aop 实现异常拦截
  8. 【JavaScript】JS跨域设置和取Cookie
  9. linux centos 配置 svn 服务器
  10. 黑马程序员_&lt;&lt;StringBuffer,包装类&gt;&gt;
  11. Delegate(委托)
  12. 数据结构系列(4)之 B 树
  13. 设计模式总结篇系列:策略模式(Strategy)
  14. MongoDB关键指标意义&amp;各数值区间意义&amp;部署
  15. Centos7 安装并配置redis
  16. Mybatis-generator生成Service和Controller
  17. 后端程序猿怎能不会的linux命令
  18. elasticsearch RTF版本介绍
  19. MyEclipse中jquery.js文件报missing semicolon的错误解决
  20. 谷歌浏览器使用IE内核

热门文章

  1. java web过滤器实际应用(解决中文乱码 html标签转义功能 敏感字符过滤功能)
  2. Jmeter-Maven-Plugin高级应用:Proxy Configuration
  3. 基于XMPP 协议的开发 android
  4. LoadRunner录制:关联
  5. iOS 应用上传所需 Icon图片大小
  6. iOS Dev (50)用代码实现图片加圆角
  7. Java 基础【13】 I/O流概念分析整理
  8. 〖Linux〗zigbee实验之cc2430移植tinyos2.x的步骤(Ubuntu13.10)
  9. 为什么WEB-INF外的jsp无法根据cookie享受国际化
  10. IP共享重新验证