Search code examples
iosobjective-cswiftframeworksbridge

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


I have an umbrella header called

MyApp-Swift.h

I've imported the following class "AFHTTPSessionManager.h" in the umbrella header.

I made the umbrella header public in build phases and I also made sure to set Define modules to yes.

However, in my swift framework I am still getting the following error

Use of undeclared type 'AFHTTPSessionManager'

I don't know why this is continuing. I thought swift would automatically pick up my Objective-C import. Any tips or suggestions are appreciated.

I tried following this source

https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_82

,but it didn't work for me. This is very fusterating.

This is the swift code that causes the error.

import Foundation

   class MyApp {
        private var manager: AFHTTPSessionManager?
   }

Solution

  • 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).