Obj-C CocoaPods + Swift Framework

2019-01-21 23:04发布

问题:

I'm creating a Swift framework in the latest Xcode 6 DP4. I need to internally use AFNetworking in my framework, but can't figure out a way to compile it.

I know how to use bridging headers, but those aren't supported in Swift frameworks.


My framework, Core.framework, looks like this:

> Core.h
    #import <UIKit/UIKit.h>
    #import <AFNetworking/AFNetworking.h>

    //! Project version number for Core.
    FOUNDATION_EXPORT double CoreVersionNumber;

    //! Project version string for Core.
    FOUNDATION_EXPORT const unsigned char CoreVersionString[]; 

> SomeFileThatNeedsAFNetworking.swift

And I'm using a podfile to bring in AFNetworking. However, when I compile, I get the following error:

<unknown>:0: error: ~/Core/Source/Core.h:2: include of non-modular header inside framework module 'Core'

because of the AFNetworking import. But if I don't include that, then I get compilation errors everywhere referencing AFNetworking.

Has anyone figured out the right combination?

回答1:

Well, based on what I'm seeing here and here I don't think it is possible to compile anything from a pod file just yet. I have tried to do this with the SCLAlertView-Swift library with similar errors. I think you will just have to wait for the code to get developed by the cocoapods team.



回答2:

So I have something working. My goal was to use a shared code framework across multiple targets with cocoapod libraries for support in sharedCode. Problems I had were the non-modular header issue and the duplicate symbols issue.

MainTarget -swift files

SharedCode -swift files -objc files -cocoapods lib

Add a 'Run Script Phase' to the top of the 'framework' target

set -e

RESOURCES_TO_COPY=${PODS_ROOT}/headers-to-copy-${TARGETNAME}.txt
> "$RESOURCES_TO_COPY"

ls -d -1 ${PODS_ROOT}/Headers/Public/**/*.h >> "$RESOURCES_TO_COPY"

rsync -avr --copy-links --no-relative --exclude '*/.svn/*' --files-from="$RESOURCES_TO_COPY" / "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/Headers"

rm -f "$RESOURCES_TO_COPY"

In the framework main header put

#import <{SHARED_FRAMEWORK_NAME}/PixateFreestyle.h>
#import <{SHARED_FRAMEWORK_NAME}/Lockbox.h>
#import <{SHARED_FRAMEWORK_NAME}/etc.h>

Voila, the classes are now available to your framework code and to your framework dependencies if needed. This does expose all of the cocoapods headers and the copy script is not smart so you may run into a path issue. So far so good for me though. This is certainly a work-around since the non-modular header thing blocks the compiler.