122

Are dynamic libraries supported on iOS (iPhone/iPad)?

In Xcode, I tried to create a New project -> Framework & Library -> Cocoa Library (dynamic). In the project settings, I set the Base SDK to iOS device 4.1 and target to iOS4.1, but it has a build error:

target specifies product type 'com.apple.product-type.library.dynamic', but there's no such product type for the 'iphonesimulator' platform".

The build i selected is Simulator -> Debug -> i386.

4
  • No, but you can build and link a static library Commented Dec 14, 2013 at 15:43
  • 4
    iOS8+ supports frameworks based on shared library. Commented Jun 21, 2014 at 4:06
  • 1
    @Eonil, can you elaborate on that? I would like to find out more about it, an article or link to some info would be much appreciated. Commented Aug 6, 2014 at 14:20
  • ddeville.me/2014/04/dynamic-linking Commented Mar 18, 2018 at 1:40

3 Answers 3

169

I'm not really disagreeing with DarkDust's answer, but it does depend on how you define supported.

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.

The Lost knowledge: Screenshots below are related to links not working now. Build and use dylib on iOS enter image description here enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

As of XCode 4.5, these files can be found at (eg.) /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Specifications
@ChrisDevereux, thanks! I put in a note to check your comment for up-to-date paths ... which of course, might change again in a future version :)
This answer needs more upvotes (and should really be the accepted answer, but I doubt user510951 will be back after being away from SO for 2 years). Very nice answer Nate!
There should be some kind of master override where if @chown says that an answer is correct... it's damn-well correct.
@Panagiotis, please post a new question, so this can be addressed properly. Show the code you're using, and all the error messages. If you want, you can also link back to this question/answer. If you add the "iphone-privateapi" tag, I'll see it. You should state that this is not for the app store, or people may vote to close the question.
113

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"

12 Comments

Anybody know why that is? To me it just seems completely insane.
@Erik de Castro Lopo: The reason is security: since a dynamic library can be loaded and unloaded at runtime you could download additional executable code and load it (think plug-in). This could get compromised by a hacker and then having malicious code executing on your phone is a very bad thing. It would also make it possible to add unapproved features to an approved app. In short: in this environment, Apple considers dynamic linking to be a Pandoras box that must be strictly controlled, otherwise it could compromise security and I agree that it does make sense on the phone.
I am developing an in-house app which will not be distributed via AppStore, so I don't care about Apple's restrictions for AppStore. Is it technically possible to create a dynamic library for iOS app?
@Aliaksei: Technically yes, or else you wouldn't be able to link to Apple's libraries. AFAIK dynamic library support is about the same like on Mac OS X. However, Xcode doesn't support it, but it seem you can use bundles. See this article.
Not supported is not the same as not allowed.
|
0

Starting from Xcode 11.4.1 dynamic libraries are not allowed (your project won't compile for all destinations). The new way to use libs/frameworks is the create-xcframework from xcodebuild.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.