How to Import Private Framework Headers in a Swift Framework

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

How can I create private module without including private headers into the framework?

Apple doesn't include private modules with their frameworks. Most likely, we shouldn't do that as well.

Import headers from c++ library in swift

You don't import anything in your Swift code when Objective-C headers are imported in the bridging header.
All public interfaces available from the imported files get available in the entire Swift module by default after that.


Sample listing

TDWObject.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface TDWObject : NSObject

- (void)someCPPCode;

@end

NS_ASSUME_NONNULL_END

TDWObject.mm

#include <iostream>

#import "TDWObject.h"

@implementation TDWObject

- (void)someCPPCode {
std::cout << "Hello from CPP cout" << std::endl;
}

@end

Some-Bridging-Header.h

#import "TDWObject.h"

main.swift

TDWObject().someCPPCode()

Provided the main.swift file is the entry point of the program, it will print Hello from CPP cout.

iOS Swift framework: How to import Objective C code into swift framework properly?

The thing is, brigding header doesn't work for Framework targets. Solution is to create module map target to build module map for needed ObjC framework.

Here is example for CommonCrypto: https://stackoverflow.com/a/42852743/1840136; in your case difference should be that script line

header "${SDKROOT}/usr/include/CommonCrypto/CommonCrypto.h"

will be replaced with path to AFNetworking headers (I'm not sure if you're placing 3rd party libraries just within project or getting from pods, so check by yourself).



Related Topics



Leave a reply



Submit