How to import private framework headers in a Swift framework?

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 file
import 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 file
import A
import 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


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 A
    import A.Private