Ios app implements hot Update (no new version is required to add new features to the app) and iosapp

Source: Internet
Author: User

Ios app implements hot Update (no new version is required to add new features to the app) and iosapp


Currently, there are three methods to achieve hot update:

1. Use FaceBook's open-source framework reactive native and js to write native ios applications

Ios apps can pull the latest js files from the server to the local device at runtime, and then execute them, because js is a dynamic

Script language, so you can directly read js files and execute them at runtime, so it can achieve ios hot update.


2. Use the lua script. Like JavaScript, lua scripts can also be used dynamically. Used by Angry Birds

The lua script is a plug-in wax that can be used to write ios applications. When a hot update occurs, the lua script is pulled from the server.

Then the dynamic execution is fine. Unfortunately, wax is no longer updated.


The above is the hot update method that can be found on the Internet.

After xcode 6, Apple opened the dynamic library compilation permission for ios. The so-called dynamic library can be loaded at runtime.

This feature is used for ios hot updates.

1.

Create a dynamic library,


The dynamic library contains the viewCOntroller to be used. Of course, it can contain any custom ui and logic to be used.

The dynamic library entry is a jkDylib class. Its. h and. m files are as follows:

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

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

The above code intent is very obvious,

This is when the dynamic library is called.

-(void)showViewAfterVC:(id)fromVc inBundle:(NSBundle*)bundle
In this function, create a viewController and use the navigationController of mainBundler to push the newly created viewController to display the ui interface of the dynamic library.

The JKViewController content in the dynamic library can be defined as needed.


2. After compiling the above dynamic library, what needs to be done now is to write a piece of code to load the dynamic library in the main project.

The main project directory is as follows:


In the most important viewCotrooler, the method for loading a dynamic library is defined:

//// 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 = [AFH TTPRequestOperationManager alloc] init]; manager. responseSerializer = [Response serializer]; NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: @ "http://www.baidu.com"]; [manager response: request success: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSLog (@ "request success");} failure: ^ (AFHTTPRequestOperation * operation, N SError * 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) lastO Bject]; 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 = [AFJSONResponseSeria Lizer serializer]; // NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: @ "http://www.baidu.com"]; // [manager condition: request success: ^ (AFHTTPRequestOperation * operation, id responseObject) {// NSLog (@ "request success"); //} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {// NSLog (@ "request failure"); //}]; NSString * documentDirecto Ry = [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 implements mselector: @ 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) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end

There is a button in the viewController view. After clicking the button, find the dynamic library under the document directory (although there is no dynamic library under the document at this time). The name conventions of the dynamic library are delicious.

JKDylib. framework

Then use NSBundle to load the dynamic library. For details, see the code.

Call the method implemented in the dynamic library after the load is successful.

 [bundleObj performSelector:@selector(showViewAfterVC:inBundle:) withObject:self withObject:bundle];    
Compile the project, run it on your phone, and then exit the program.


3. Open itunes and synchronize the dynamic library to the test project directory just now.


4. Open the test project and click the button. The ui defined in the dynamic library is displayed.


The reference code for the above project is in

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


Thoughts on dynamic updates:


There is still a problem in implementing hot updates using dynamic libraries, that is, how to share and build between the main project and dynamic libraries

Such as network components and other third-party components.

At present, I have not found a good method. I can only add and compile the dynamic library and the main project separately.




Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.