iOS app for hot updates (no need to send new version number to implement new feature)

Source: Internet
Author: User


Now we can implement the method of hot update, summed up the following three kinds of

1. Use Facebook's open source framework reactive native, use JS to write native iOS apps

The iOS app can pull the latest JS file from server to local while executing. Then execute, because JS is a dynamic

scripting language. So you can directly read the JS file execution, and therefore can realize the hot update of iOS


2. Use the Lua script. Lua scripts, like JS, can also be animated. Before the indignant birds used

The Lua script does a plug-in wax that can be implemented using LUA to write iOS apps. When hot-updated, pull the Lua script from the server

Then the dynamic operation is possible. Unfortunately, wax is not updated at this moment.


The above is a hot Update method that can be found online today.

After Xcode 6, Apple opened the dynamic Library compilation permissions for iOS. The so-called dynamic library. The fact is that it can be loaded at execution time.

Just take advantage of this feature to do hot updates for iOS.

1.

Create a dynamic library.


Dynamic libraries include the viewcontroller you need to use, and you can certainly include your own defined UI and logic for whatever needs to be used.

The entry of a dynamic library is a Jkdylib class. Its. h and. m files are, for example, the following:

  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 is intended to be obvious,

is when you call the dynamic library.

-(void) SHOWVIEWAFTERVC: (ID) FROMVC inbundle: (nsbundle*) bundle
In this function, create a viewcontroller and then use the Mainbundler navigationcontroller push new Viewcontroller to display the UI interface of the dynamic library.

The Jkviewcontroller content in the dynamic library can be defined according to the need.


2. After compiling the above dynamic library, what is needed now is in master project. Write the code that loads the dynamic library.

The main project folder is as follows:


In the most important viewcotrooler, a 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 = [[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 \%@", bundle.localizations); [Bundleobj showviewaftervc:self inbundle:bundle];}    -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end

There is a button in the Viewcontroller view, click the button and find the dynamic library from the document folder (although there is no dynamic library under document), the name of the dynamic library has a good taste

Jkdylib.framework

Then use NSBundle to load the dynamic library and see the code in detail.

After the load is successful. Calling methods implemented in a dynamic library

[Bundleobj performselector: @selector (showviewaftervc:inbundle:) withobject:self Withobject:bundle];    
Compile the project. Then execute to the phone and then exit the program


3. Open itunes and sync the dynamic library to the test project folder just now.


4. Open the test project program here. Clicking on the button will reveal that you can enter the UI interface defined in the dynamic library.


The reference code for project above

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


Thinking about dynamic updates:


Using dynamic Library to implement thermal update there is actually a problem. is how to share a build between the main project and the dynamic library

For example, network components and other third-party components.

I don't see a good way to do this right now, only to add and compile between the dynamic library and the master project separately.




iOS app for hot updates (no need to send new version number to implement new feature)

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.