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

Source: Internet
Author: User
Tags lua vars

At present, the method of heat update can be realized, and the following three kinds are summed up

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

The iOS app can pull the latest JS files from the server to local and execute them at runtime, because JS is a dynamic

scripting language, so you can read the JS file directly at run time, and thus be able to implement hot updates for iOS

2. Use the Lua script. Lua scripts, like JS, can also be animated. Before Angry Birds to use

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

Then the dynamic execution is done. Unfortunately, wax is not updated at this time.

Above is a hot Update method that can now be found on the Internet.

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

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

1.

Create a dynamic library,

The dynamic library contains the Viewcontroller you need to use, and of course it can contain any custom UI and logic that you need to use.

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

[OBJC]View PlainCopy
  1. //
  2. JKDylib.h
  3. Jkdylb
  4. //
  5. Created by Wangdan on 15/7/5.
  6. Copyright (c) 2015 Wangdan. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @interface Jkdylib:nsobject
  10. -(void) SHOWVIEWAFTERVC: (ID) fromvc inbundle: (nsbundle*) bundle;
  11. @end


. m file

[OBJC]View PlainCopy
  1. //
  2. Jkdylib.m
  3. Jkdylb
  4. //
  5. Created by Wangdan on 15/7/5.
  6. Copyright (c) 2015 Wangdan. All rights reserved.
  7. //
  8. #import "JKDylib.h"
  9. #import "JKViewController.h"
  10. @implementation Jkdylib
  11. -(void) SHOWVIEWAFTERVC: (ID) fromvc inbundle: (nsbundle*) bundle
  12. {
  13. if (Fromvc = = nil) {
  14. return;
  15. }
  16. Jkviewcontroller *VC = [[Jkviewcontroller alloc] init];
  17. Uiviewcontroller *PREVC = (Uiviewcontroller *) Fromvc;
  18. if (Prevc. Navigationcontroller) {
  19. [Prevc. Navigationcontroller pushviewcontroller:vc animated:YES];
  20. }
  21. else {
  22. Uinavigationcontroller *navi = [[Uinavigationcontroller alloc] init];
  23. [Navi PUSHVIEWCONTROLLER:VC animated:YES];
  24. }
  25. }
  26. @end


The above code intent is very obvious,

is when you call the dynamic library.

[OBJC]View PlainCopy
    1. -(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 casually as needed.

2. After compiling the above dynamic library, all that is needed now is to write the code that loads the dynamic library in the main project.

The main project directory is as follows:

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

[OBJC]View PlainCopy
  1. //
  2. Viewcontroller.m
  3. Dylibtest
  4. //
  5. Created by Wangdan on 15/7/5.
  6. Copyright (c) 2015 Wangdan. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. #import "AFNetWorking.h"
  10. @interface Viewcontroller ()
  11. @end
  12. @implementation Viewcontroller
  13. -(void) Viewdidload {
  14. [Super Viewdidload];
  15. self. View. backgroundcolor = [Uicolor whitecolor];
  16. self. title = @ "bundle test";
  17. Afhttprequestoperationmanager *manager = [[Afhttprequestoperationmanager alloc] init];
  18. Manager. Responseserializer = [Afjsonresponseserializer serializer];
  19. nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:@ "http://www.baidu.com"]  ;
  20. [Manager Httprequestoperationwithrequest:request success:^ (afhttprequestoperation *operation, ID Responseobject) {
  21. NSLog (@ "request Success");
  22. } failure:^ (afhttprequestoperation *operation, nserror *error) {
  23. NSLog (@ "request Failure");
  24. }];
  25. UIButton *btn = [[UIButton alloc] initwithframe:cgrectmake (0, 100, 100,   50)];
  26. BTN. backgroundcolor = [Uicolor bluecolor];
  27. [Btn addTarget: Self
  28. Action:@selector (btnhandler)
  29. Forcontrolevents:uicontroleventtouchupinside];
  30. [self. View addsubview:btn];
  31. nsstring *document = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES)   Lastobject];
  32. BOOL Writeresult =
  33. [@ "Hellow" writetofile:[nsstring stringWithFormat:@ "%@/%@", document,@ "Hello.plist"]   atomically:YES encoding:nsutf8StringEncoding error: nil];
  34. additional setup after loading the view, typically from a nib.
  35. }
  36. -(void) Btnhandler
  37. {
  38. //afhttprequestoperationmanager *manager = [[Afhttprequestoperationmanager alloc] init];
  39. //manager.responseserializer = [Afjsonresponseserializer serializer];
  40. //Nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:@ "http://www.baidu.com"]];
  41. //[Manager httprequestoperationwithrequest:request success:^ (afhttprequestoperation *operation, ID Responseobject) {
  42. //NSLog (@ "request Success");
  43. //} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
  44. //NSLog (@ "request Failure");
  45. //}];
  46. nsstring *documentdirectory = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask,   YES) Lastobject];
  47. nsstring *bundlepath = [NSString stringwithformat:@ "%@/%@", Documentdirectory,@ "Jkdylb.framework"];
  48. if (![ [Nsfilemanager Defaultmanager] Fileexistsatpath:bundlepath]) {  
  49. NSLog (@ "File not exist, now return");
  50. return;
  51. }
  52. nsbundle *bundle = [NSBundle Bundlewithpath:bundlepath];
  53. if (!bundle | |![ Bundle Load]) {
  54. NSLog (@ "Bundle load Error");
  55. }
  56. Class loadclass = [bundle Principalclass];
  57. if (!loadclass) {
  58. NSLog (@ "Get bundle class fail");
  59. return;
  60. }
  61. nsobject *bundleobj = [loadclass new];
  62. [Bundleobj Performselector:@selector (showviewaftervc:inbundle:) withobject:self withobject:  Bundle];
  63. nsstring *framepath = [[NSBundle Mainbundle] Privateframeworkspath];
  64. NSLog (@ "Framepath is%@", Framepath);
  65. NSLog (@ "file Attri \ n%@", bundle. localizations);
  66. [Bundleobj showviewaftervc:self Inbundle:bundle];
  67. }
  68. -(void) didreceivememorywarning {
  69. [Super didreceivememorywarning];
  70. //Dispose of any resources, can be recreated.
  71. }
  72. @end


Viewcontroller View has a button, click on the button, from the document directory to find the dynamic library (although there is no dynamic library under document), the name of the dynamic library contract good taste

Jkdylib.framework

Then use NSBundle to load the dynamic library, see Code.

When the load succeeds, the method that is implemented in the dynamic library is called

[OBJC]View PlainCopy
    1. [Bundleobj Performselector:@selector (showviewaftervc:inbundle:) withobject:self withobject:  Bundle];

Compile the project, then run it to the phone, and then quit the program

3. Open itunes and sync the dynamic library to the test project catalog you just tested.

4. Open the test engineering program here, click on the button, you will find the ability to enter the dynamic library defined in the UI interface.

The reference code for the above project

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

Thinking about dynamic updates:

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

such as network components and other third-party components, etc.

I have not found a good method at the moment, can only be added and compiled separately between the dynamic library and the main project.

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

Related Article

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.