Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm creating a swift framework and I want to used an objective-c class in the framework.

I used pod lib create frameworkName to create library.

It's my podspec file:

http://guides.cocoapods.org/syntax/podspec.html

Pod::Spec.new do |s|
s.name             = 'MyFramework'
s.version          = '0.1.0'
s.summary          = 'A short description of RadiantSensorsRSPOS.'

s.description      = <<-DESC
TODO: Add long description of the pod here.
                   DESC
s.license          = { :type => 'MIT', :file => 'LICENSE' }
s.ios.deployment_target = '8.0'

s.source_files = 'MyFramework/Classes/**/*'
s.public_header_files = 'Pod/Headers/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'CocoaAsyncSocket'
end

I have objc.h and objc.m file that contain an Objc class. I have to use this Objc class in MyFramework.swift.

In Standard application (non framework) I made a header bridge and import the objc.h file, so I had access to Objc class.

How can I use the Objc class here?

Where should I put objc.h and objc.m file?

How to make header bridge here?

like image 754
Vahid Avatar asked Mar 09 '23 22:03

Vahid


1 Answers

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" => "[email protected]" }

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

like image 67
Christopher Disdero Avatar answered Apr 06 '23 12:04

Christopher Disdero