Loading Objective C code dynamically

邮差的信 提交于 2019-12-07 20:11:07

问题


I am looking for a possibility of loading Obj C based source dynamically and show the view in my iOS application. For example: I have a set of code written for showing a view, i want to dynamically load this code and show this view. Some thing like,

  1. I'll have a service running in the background of my iOS app.
  2. It will get a set of Obj C code from my server in text format
  3. This dynamic Obj C code should get executed dynamically and show the respective iOS view

From Comments Not released in the appstore.. its for internal

Is this possible?


回答1:


Short answer: No

Not so short answer:

You could—in theory—include either the C, or C++ interface to the Clang compiler toolchain in your project, have that library compile the code you download, and then (through either NSBundle or direct interaction with dlopen) link that compiled code into your app.

In practice, if what you want to achieve is submitting to the App Store, this is explicitly prohibited by the Terms and Conditions.




回答2:


You can't do this for deployment to the app store.

You wouldn't use plain text for this, you'd use a bundle (NSBundle). A bundle can contain both file (graphics, NIBs) resources and code so you can create your view classes and any associated NIBs, compile the bundle and then store it on your server. The app can then download the bundle and load it at runtime.




回答3:


You can do it for non-app store apps. I have not tried this approach.

From Apple Docs:

The key to loading code from an external bundle is finding an appropriate entry point into the bundle’s executable file. As with other plug-in schemes, this requires some coordination between the application developer and the plug-in developer. You can publish a custom API for bundles to implement or define a formal plug-in interface. In either case, once you have an appropriate bundle or plug-in, you use the NSBundle class (or the CFBundleRef opaque type) to access the functions or classes implemented by the external code.

Loading Objective-C Classes If you are writing a Cocoa application, you can load the code for an entire class using the methods of NSBundle. The NSBundle methods for loading a class are for use with Objective-C classes only and cannot be used to load classes written in C++ or other object-oriented languages.



来源:https://stackoverflow.com/questions/24324374/loading-objective-c-code-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!