Objective C classes within an iOS Swift-based dynamic framework Objective C classes within an iOS Swift-based dynamic framework swift swift

Objective C classes within an iOS Swift-based dynamic framework


The quick answer is you can't :) Even if you declare "private" modulemap, it can be always imported by your framework users. Please note, that usually, it is not a concern, especially with open source. You just say "this is an internal module, don't use it".

But (there is always but) - you can have behavior, that effectively works the same - allows you to use your Objective-C classes withing same framework target, without making them public. It works in closed source setup - I have a dynamic framework shipped to external parties, that has a purpose of revealing as less about its construction as possible.

The case a bit too complex to paste everything here. I'm adding a link to my article about the topic. That's a general idea:

  1. Mimic your Objective-C classes with Swift protocols (manual work needed)
  2. In your swift code use these protocols
  3. Expose internally these Swift protocols to Objective-C (manual work needed)
  4. Adopt these protocols by ObjC classes
  5. Create in Swift and expose in ObjC a factory, that will create instances of these protocols
  6. Create in the factory methods, that will allow you to register concrete types, and expose them to ObjC
  7. Register ObjC types from Objective-C code in the Swift factory
  8. Use the factory to instantiate ObjC classes, without actually exposing ObjC to Swift (hurray ;) )

Creating Swift framework with private Objective-C members. The Good, the Bad, and the Ugly

Github example project


You can find some method from this link.

Just create Folder (e.g.PrivateA) with module.modulemap where include all object headers you need to use in your Swift class.e.g.

module $(ModuleName)Private {    header "header.h"    export *}

in Swift,you can use: import $(ModuleName)Private

so, default module exclude these headers when using import $(ModuleName).

From my experiment, test project can also import $(ModuleName)Private, but not found any useful code.

Try this way anyway.