Obj-C Cocoapods + Swift Framework

How to mix objective-c and swift to make a cocoapods framework

I realize it may be a bit late to answer your question, but I recently had to do the same thing as you - create a cocoapod of a Swift Framework with some Objective C code mixed in it.

When you create a Swift-based framework in Xcode (8.2 or higher I used) it generates what is called an umbrella header file (.h) which is the same name as the target. So if your framework target is called 'MyFrameworkTarget', the umbrella header that Xcode adds is called 'MyFrameworkTarget.h'. It is important to include this header in your cocoapod, along with the other Swift and Objective C source code files.

As an example, here is a working podspec file which combines Swift and Objective C source files into a framework which targets both iOS and macOS:

Pod::Spec.new do |s|

s.name = "MyFramework"
s.version = "1.0.0"
s.summary = "Example cocoapod spec for a mixed language framework"
s.description = <<-DESC
Example cocoapod spec for a mixed language framework that targets both iOS and macOS.
DESC

s.homepage = "https://github.com/username/MyFramework"

s.license = "Apache License, Version 2.0"

s.author = { "My Name" => "info@code.myname.com" }

s.ios.deployment_target = "9.0"
s.osx.deployment_target = "10.11"

s.source = { :git => "https://github.com/username/MyFramework.git", :tag => "#{s.version}" }

s.source_files = 'Shared/*.{swift,h,m}'
s.ios.source_files = 'MyFrameworkMobile/*.h'
s.osx.source_files = 'MyFrameworkMac/*.h'

end

In this particular example, I have the Swift and Objective C files that constitute the framework as built for both iOS and macOS in the folder 'Shared' and referenced in the podspec by 's.source_files'. Then the framework umbrella files that Xcode generated and I edited for my two framework targets iOS (which I named MyFrameworkMobile) and macOS (which I named MyFrameworkMac) are referenced in 's.ios.source_files' and 's.osx.source_files', respectively. In my example, they are separate for iOS and macOS because I have two targets. The actual headers are in folders 'MyFrameworkMobile' and 'MyFrameworkMac'.

Here's my actual on-disk project folder layout to help visualize what the podspec is pointing to:

MyFramework.podspec
MyFramework.xcodeproj

MyFrameworkMac
MyFrameworkMac.h
Info.plist

MyFrameworkMobile
MyFrameworkMobile.h
Info.plist

Shared
MyFramework.swift
MyFramework.h
MyFramework.m

I hope this information helps. There's a fine sample project that demos how to create a pod file for a mixed project of Swift and Objective C files on GitHub at https://github.com/axl411/NVMDummyTestPod

problem integrating ObjC pod into a Swift framework

The common way to use objective-c code in swift is using bridging headers, take a look at this:

Importing Objective-C into Swift

you need to create a bridging header and add it to your project then inside the .h file you created simply add :

#import <Lottie/Lottie.h>

Cocoapod With Mix and Match Swift and Objective-c code

I have a (private) pod that contains Obj-C and Swift code. In the podspec I've simply added them like this

  s.source_files        = 'Project/*.{swift}',
'Project/legacy/*.{h,m}'

Technically in Xcode this project is a framework.

Here you can find the technical details how to combine them in a Project.
Technically if you create a framework you'll need to include the Obj-C headers in the umbrella header of the framework
If you want to use Swift methods in your Obj-C classes you'll need to include them as

#import <ProductName/ProductModuleName-Swift.h>

if you're building a framework or simply

#import "ProductModuleName-Swift.h"

if you're working on a regular project

How to import and use Swift Pod Framework in Objective-C Project

I think you have to declare the swift class as public, otherwise it is treated as an internal class and can be only be seen within the same module, and this could be the reason why adding it to the same project as files work, but as a framework doesn't. Other thing that occurs to me is that the framework may need to add @objc in front of the class declaration so that it can be seen within objective-c classes. Also reading Apple's guide of Mix and Match between objective c and swift it says that when you import an external framework, you need to make sure the Defines Module build setting for the framework you’re importing is set to Yes. Have you checked with any of those options?

Import Objective-C Framework (CocoaPod) into Swift?

Bridge

1. Create a xxx-Bridging-Header

Add a bridging header to your project using the method of your choice, the easiest one being creating a single .m file and answering Create Bridging Header to this dialog:

Create Bridging Header

2. Reference your Pod in the bridging header

Include your files as so:

//
// Use this file to import your target's public headers that
// you would like to expose to Swift.

#import "RTCICEServer.h"

3. Objective-C exposed to Swift

Once in the bridging header, you need not import the Obj-C classes in Swift. Use these directly:

let uri = URL(fileURLWithPath: "")
let rtc:RTCICEServer = RTCICEServer(uri: uri, username: "", password: "")
print(rtc)

Another example is described here.


► Find this solution on GitHub and additional details on Swift Recipes.



Related Topics



Leave a reply



Submit