How to import private framework headers in a Swift framework? How to import private framework headers in a Swift framework? objective-c objective-c

How to import private framework headers in a Swift framework?


You need to modify framework A, So that it export a private module.

  1. Create a private module map file in A project. This would be something like this:

    A/private.modulemap:

    explicit module A.Private {    // Here is the list of your private headers.    header "Private1.h"    header "Private2.h"    export *}
  2. In the "Build Settings" of framework A target, search "Private Module Map File" line, and make that:

    $(SRCROOT)/A/private.modulemap
  3. Do not include private.modulemap file in "Compile Sources". That causes unnecessary warnings.

  4. Clean and Build framework A target.

  5. In framework B Swift files. you can import the private module like this:

    import Aimport A.Private


It is some time since this question was posted. The accepted answer is very good, and as far as I'm concerned it is a common approach.

The problem is, it is not really "private". You can do this inside your framework to access the "private" part:

// Framework A Swift fileimport A.Private

But If you use framework A in an app (or you ship it to your client), he can still do:

// Client App Swift fileimport Aimport A.Private// access "private" framework methods and classes

I was trying to resolve that, as I had recently a situation when I needed to hide it from users (closed source framework) - I just could not let anyone access it, as it was a threat to SDK integrity.

I found a solution to that problem, but a bit too complex to paste it here as a whole.

I made a post about it no medium. Maybe it will help someone checking that problem, that's the link to my article:

https://medium.com/@amichnia_31596/creating-swift-framework-with-private-objective-c-members-the-good-the-bad-and-the-ugly-4d726386644b


As noticed by Andrzej Michnia in his answer the problem with "private module map" solution is that it is not really completely private and those "private" headers still can be seen by someone as they are still included in our framework. If someone opens compiled framework with such "private" module he will still see all .h files that you hidden.

If we need to hide some objective-c headers in our swift framework completely from other users then another possible method to do that will be just to make them public and remove them from our framework after building the framework manually or with a bash script.

You could create a separate header file for example "InternalHeaders.h" where you import all headers that you do not want to expose. Then import this InternalHeaders.h in public umbrella header of your framework. Make all headers public so that you can compile everything. After you build your framework simply remove "import InternalHeaders.h" from public umbrella header and remove all headers that you do not want to expose manually or with a bash script or in run script build phase and thats it.

Still not a perfect solution but in some cases it might be much easier and faster that writing protocols in swift to match every objective-c interface as proposed in other answer.