1 year ago

#23867

test-img

ImShrey

Access ObjC/C++ classes in Swift static library

I'm building this Swift static library: OxfordLibrary that I ultimately want to distribute using XCFramework.

I have some ObjC/C++ code that want to access from Swift. Since We can't use ObjC bridging header in Static libraries (Throws errors when creating XCFramework), So I'm trying to use modulemap files.

So idea was keeping ObjC/C++ code base internal / private to library and only expose APIs via Swift.

So I created following:

/// FILE: module.modulemap

module OxfordLibrary {
    export *
}
/// FILE: module.private.modulemap

module OxfordLibrary_Private {
    umbrella header "OxfordLibrary-objc-umbrella.h"
    export *
}

I have set the Defines Module: YES and also set the swift Seach Path in build settings.

Now in my Main Swift file:

/// FILE: OxfordLibrary.swift

#import OxfordLibrary_Private

open OxfordLibrary{
    public static func foo() {
        SomeObjcClass.someObjcMehtod()    // Final aim
    }
}

Now without importing OxfordLibrary_Private I can't access SomeObjcClass in swift. Fine. After adding import it works.

This even builds correctly.

But when I create XCFramework from this and import that XCFramework to client app project, and I do import OxfordLibrary then it throws error OxfordLibrary_Private module not found / not defined.

After doing some research I found I need to copy modulemap files and headers to XCFramwork either in Modules folder or using -headers flag. It works if I do that.

But question is: Its private.modulemap supposed to be private and not avaible outside of the library?

Because now I can do import OxfordLibrary_Private in client app and access any ObjC method there.

ios

swift

objective-c

static-libraries

module-map

0 Answers

Your Answer

Accepted video resources