Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you build dynamic libraries for iOS and load them at runtime?

Tags:

xcode

ios

dylib

People also ask

What is dynamic loading in iOS?

When an app is launched, the OS X kernel loads the app's code and data into the address space of a new process. The kernel also loads the dynamic loader ( /usr/lib/dyld ) into the process and passes control to it. The dynamic loader then loads the app's dependent libraries.

How are dynamic libraries loaded?

Dynamic loading is a mechanism by which a computer program can, at run time, load a library (or other binary) into memory, retrieve the addresses of functions and variables contained in the library, execute those functions or access those variables, and unload the library from memory.

What is the difference between static and dynamic library iOS?

statically linked modules are fastest to load (loading non-system dynamic frameworks is pretty expensive while system frameworks are optimized). When using static linking, all the symbols are within the same module, so the app start is fast. dynamically linked modules are slower to load, especially on iOS.

How do I create a dynamic library in xcode?

Create C Dynamic Library in XcodeOn the "Welcome to Xcode" page, select "Create a new Xcode project". On the template page, select Framework & Libraries >> C/C++ Library. Click Next. On the options page, enter the name of static library.


I'm not really disagreeing with DarkDust's answer, but if I may channel my inner Bill Clinton, it depends on what the meaning of supported is :)

Apple doesn't want you doing this for App Store apps, but the operating system certainly allows it. Jailbreak apps use this technique all the time. You basically use a standard UNIX technique to dynamically open a framework/library, and then use stuff in it. The dlopen function allows you to open the library by passing in the path to that framework, or dylib. From some docs for building jailbreak apps, here's an example of calling an init() function implemented inside your own, separate dylib:

#include <dlfcn.h>

initWrapper() {
    char *dylibPath = "/Applications/myapp.app/mydylib2.dylib";

    void *libHandle = dlopen(dylibPath, RTLD_NOW);
    if (libHandle != NULL) {
        // This assumes your dylib’s init function is called init, 
        //    if not change the name in "".
        void (*init)() = dlsym(libHandle, "init");
        if (init != NULL)  {
            init();
        }
        dlclose(libHandle);
    }
}

Furthermore, the default restriction against allowing you to build a dynamic library project for iOS is something in Xcode that you have the ability to override by editing some XCode xml files:

Build and use dylib on iOS

Once you do this, you can build a normal iOS .dylib library, and use it per the sample code above. (yes, you probably will have to unlock this capability again whenever you install a new XCode version).

So, it's not a technical limitation, but an App Store policy limitation. If you're not limited to the App Store, then you can do it. Note that this technique does not require jailbreaking, although if the app is sandboxed, it may limit where dylibs can be loaded from.

Edit: in order to make sure this information isn't lost to future link rot, here is the content of the link I provided on how to enable iOS dylibs in Xcode. (Note: this process still works on Xcode 4, but see comment(s) below for updates to paths, etc.) Source is the iOS Place blog:


Xcode does not allow you to build dylib for iOS. App will be rejected if it’s not single binary. But I have an application that has plug-in architecture to load optional modules. I just want a quick prototype to prove concept before fully port it to iOS. It’s faster to do if dylib could simply work. So, this post shows how to build and use dylib but be aware it won’t be approved to App Store. (Tested with Xcode 3.2.4 on 10.6.4)

1. Open these files in the Property List Editor: /Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Specifications/MacOSX Product Types.xcspec and /Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Specifications/iPhone Simulator ProductTypes.xcspec

2. Locate the item in the “MacOSX Product Types.xcspec” that has the product type com.apple.product-type.library.dynamic and drag it to the “iPhone Simulator ProductTypes.xcspec”.

Xcode screenshot 1

3. Open “MacOSX Package Types.xcspec” and “iPhone Simulator PackageTypes.xcspec” found at the same places.

4. Locate the item in the “MacOSX Product Types.xcspec” that has the package type com.apple.package-type.mach-o-dylib and drag it to the “iPhone Simulator PackageTypes.xcspec”.

Xcode screenshot 2

5. Repeat the steps for the “iPhoneOS.platform” and relaunch Xcode if it was running.

Now, lets build a dylib. Start out with the “Cocoa Touch Static Library” Template. That should included the Foundation.framework in the project. Here are the changes I made on top of the template to build dylib.

1. Open the file project.pbxproj (found inside the Xcode project file bundle) in a Text Editor. Search for string “producttype”, change it’s value to com.apple.product-type.library.dynamic;

Now, open the project with Xcode, go to Project->Edit Project Settings

2.Installation Directory” set to @executable_path/ because I plan to put the dylib in the same directory as the app’s executable.

3.Mach-O Type” set to Dynamic Library

4.Executable Extension” set to dylib

5.Executable Prefix” set to empty

6. Add one or two simple methods to the library and build it.

Now, create an app to test it. This time, I choose the View-based Application. Hook up a UIButton and a UILabel to call the lib and showing return message. You can download the complete project TestApp and play with it.


At the time this question was asked, Dynamic libraries were not supported by iOS and will result in your app getting rejected. Only static libraries are allowed.

However, in iOS8 you can use dynamic libraries and frameworks. It should "just work"