115

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.

Nate
  • 30,589
  • 12
  • 76
  • 201
user510951
  • 1,225
  • 3
  • 10
  • 10

3 Answers3

163

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.

Cœur
  • 32,421
  • 21
  • 173
  • 232
Nate
  • 30,589
  • 12
  • 76
  • 201
  • 2
    As of XCode 4.5, these files can be found at (eg.) /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Specifications – Chris Devereux Sep 24 '12 at 23:48
  • @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 :) – Nate Sep 25 '12 at 00:00
  • 3
    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! – chown Nov 11 '12 at 07:14
  • 1
    There should be some kind of master override where if @chown says that an answer is correct... it's damn-well correct. – Tim Apr 24 '13 at 00:36
  • @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. – Nate Aug 27 '13 at 19:13
108

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"

Rog
  • 16,650
  • 9
  • 48
  • 73
DarkDust
  • 85,893
  • 19
  • 180
  • 214
  • 16
    Anybody know why that is? To me it just seems completely insane. – Erik de Castro Lopo Apr 29 '11 at 03:24
  • 75
    @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*. – DarkDust Apr 29 '11 at 06:25
  • 7
    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 Sep 09 '11 at 21:12
  • 3
    @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](http://www.macosxguru.net/article.php?story=20110712175443515&mode=print). – DarkDust Sep 10 '11 at 17:35
  • @DarkDust: _"…and I agree that it does make sense on the phone."_ I also agree if they do this on the iPad: it's made to be simple for everybody, just like the iPhone. I have my doubts for the Mac, but what the hey… Anyway, thanks for your very helpful explanation. :-) – Constantino Tsarouhas Oct 23 '11 at 14:50
  • 3
    Not supported is not the same as not allowed. – dtech Apr 28 '14 at 12:49
  • This is no longer true in iOS 8 :) https://developer.apple.com/library/prerelease/ios/documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_6_0.html – NachoSoto Aug 13 '14 at 18:25
  • Just need a confirm: Does that mean we could download & load a framework from network in iOS8 or later? How about the security issue? Will the AppStore app reviewers pass an app like that? – Itachi Oct 22 '14 at 05:39
  • 1
    @Itachi: I think you better ask a new question (referencing this one) because _this_ question was about _"Can you do it?"_ while your question is more like _"Are we allowed to?"_ And I don't know the answer, BTW. – DarkDust Oct 22 '14 at 07:18
  • don't think it does "just work" given the error mentioned in the question – Max MacLeod Feb 18 '16 at 11:31
  • @DarkDust: How can I create for iOS.\ – Vineesh TP May 25 '16 at 09:12
  • For future people reading this, to address @Aliaksei's concern, there is no need to use the App Store for in-house apps. You just need to sign up for an Enterprise account. With an enterprise account, you can distribute iOS apps without needing to add the device to the provisioning profile, but an app signed with an enterprise certificate cannot be put in the app store. – aeskreis Jul 15 '16 at 23:50
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.

Carla Camargo
  • 449
  • 4
  • 10