Hide Objective C in mixed Swift Cocoapod library

224 views Asked by At

I am developing a Swift Cocoapod library which actually is a wrapper around some Objective C code. My goal is to expose only the Swift part of the library, while making the Objective C as private as possible. However, as the Swift part is a wrapper around the Objective C code, it needs to access it, although just internally.

I have searched and tried various approaches, but none of them worked at all.


First, I tried to address it via the use of a modulemap file. The idea was to create a Swift module called something like MyLibrary containing all the Swift code, while the Objective C code would be on the MyLibrary.Private module (I know the module would still be accessible, but this separation would be enough for me). The modulemap would look something like:

//MyLibrary.modulemap

framework module MyLibrary {
    module Private {
        header "MyObjectiveC.h"
        export *
    }
}

And adding the following line in the MyLibrary.podspec file

s.module_map = 'path/to/MyLibrary.modulemap'

However, by using this approach, even if I only import the MyLibrary module would still get access to all the Objective C classes, without having to import the MyLibrary.Private module to do so.


A second approach I tried was to make use of a private.modulemap file. So I would end up with two different modulemap files:

//MyLibrary.modulemap

framework module MyLibrary {}
//MyLibrary.private.modulemap

module MyLibrary.Private {
    header "MyObjectiveC.h"
    export *
}

I would then add the following line in the MyLibrary.podspec file

s.pod_target_xcconfig = { 'MODULEMAP_PRIVATE_FILE' => '$(PODS_ROOT)/MyLibrary/path/to/MyLibrary.private.modulemap' }

However, I get a failed build stating that it can not find the MyLibrary.Private module.


Is there anything I am doing wrong in these approaches? Or is there any other way to allow my library's Swift classes access the Objective C classes privately without exposing those, at least explicitly, requiring to import an specific *.Private module for it?

0

There are 0 answers