CocoaPods project depend on a local static C library - ios

I have locally built C library (.h and .a files) that I want to include in a Swift-based CocoaPods pod. How do I configure the podspec to depend on the .a files and the module.map? With a normal non-CocoaPods Xcode project, I simply drag in the directory that contains include and lib and then add a module.map. With CocoaPods I can't do this because pod install will overwrite the Xcode project file. s.library won't work because the the static library isn't hosted anywhere. I tried s.vendored_libraries but module.map still remains unknown to Xcode, the end result being that import foo from my Swift files is an error.
Edit: I tried using preserve_paths, vendored_libraries and xcconfig as answered here. The issue is still how to import the module from Swift.
Edit 2: I also tried using module_map to point to my module.map file as documented here, but sadly CocoaPods 1.1.1 crashes ([!] Oh no, an error occurred.).

I got it working. In my case I'm depending the libtiff C library which is prebuilt for iOS (x86 and arm) using https://github.com/ashtons/libtiff-ios.
I used a subspec as outline here. Here's the podspec subspec snippet, assuming the static library lives at libtiff off the root of the pod module.
s.subspec 'libtiff' do |libtiff|
libtiff.source_files = 'libtiff/include/*.h'
libtiff.public_header_files = 'libtiff/include/*.h'
libtiff.preserve_paths = 'libtiff/include/*.h'
libtiff.vendored_libraries = 'libtiff/lib/libjpeg.a', 'libtiff/lib/libpng.a', 'libtiff/lib/libtiff.a', 'libtiff/lib/libtiffxx.a'
libtiff.xcconfig = { 'HEADER_SEARCH_PATHS' => "${PODS_ROOT}/#{s.name}/libtiff/include/**" }
# you can't specify "libz" here, must specify "z", see https://github.com/CocoaPods/CocoaPods/issues/3232
libtiff.library = 'z'
end

Related

cocoa podspec subspec swift compatibility header file not found

Pod lib could not find mymodule-swift.h header.
One of my objc.m file imports following code.
#import <devillogin/devillogin-Swift.h>
Xcode build is success. Everything is ok.
But when I'm trying to distribute with pod, following error printed.
pod lib lint mymodule.podspec
fatal error: 'devillogin/devillogin-Swift.h' file not found
My mylib.podpec is as following
Pod::Spec.new do |s|
...
s.subspec 'DevilLogin' do |devilLogin|
devilLogin.source_files = 'devillogin/devillogin/source/**/*.*'
devilLogin.public_header_files = 'Pod/Headers/*.h'
devilLogin.dependency 'devil/DevilCore'
devilLogin.dependency 'KakaoSDK'
end
end
Is there any syntax in podspec for mylib-swift.h ?
I'm answering to myself.
I found that the issue only occur when it is in the subspec.
No problem with root podspec.
Consequently I couldn't find way to let my pod-subspec import "XXX-swift" header.
But I found workaround.
I referenced firebase frameworks which use many pod-subspecs.
https://github.com/firebase/firebase-ios-sdk
Just watched root pod spect and 1 subspec.
It distributes sub-specs as root pod spec which is success with 'XXX-swift.h' header
And each subspecs depend on it above.
There is Firebase.podspec and FirebaseDynamicLinks.podspec.
FirebaseDynamicLinks is standalone independent frameworks.
But it is actually subspec.
Firebase podspec defines subspec like followings.
This is workaround
s.subspec 'DynamicLinks' do |ss|
ss.dependency 'Firebase/CoreOnly'
ss.ios.dependency 'FirebaseDynamicLinks', '~> 7.9.0'
end
It defines DynamicLinks as subspec and define dependency on independent FirebaseDynamicLinks.
Done

Importing GoogleWebRTC pod fails: module 'GoogleWebRTC' not found

I am currently working on a Kotlin multiplatform and am trying to interface with the GoogleWebRTC pod.
The pod exists, I was able to import it in a separate project (without Kotlin multiplatform). I can see the .framework is created in my build directory, however when doing a gradle sync or building from Xcode, I get the following error:
Exception in thread "main" java.lang.Error: /var/folders/hv/9cx28nxx4gz9hj_m86bp5rx40000gn/T/tmp362966650322311128.m:1:9: fatal error: module 'GoogleWebRTC' not found
at org.jetbrains.kotlin.native.interop.indexer.UtilsKt.ensureNoCompileErrors(Utils.kt:152)
at org.jetbrains.kotlin.native.interop.indexer.ModuleSupportKt.getModulesASTFiles(ModuleSupport.kt:67)
at org.jetbrains.kotlin.native.interop.indexer.ModuleSupportKt.getModulesInfo(ModuleSupport.kt:13)
at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.buildNativeLibrary(main.kt:499)
at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.processCLib(main.kt:264)
at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.interop(main.kt:72)
at org.jetbrains.kotlin.cli.utilities.InteropCompilerKt.invokeInterop(InteropCompiler.kt:45)
at org.jetbrains.kotlin.cli.utilities.MainKt.mainImpl(main.kt:19)
at org.jetbrains.kotlin.cli.utilities.MainKt.main(main.kt:37)
Execution failed for task ':shared:cinteropGoogleWebRTCIosArm64'.
> Process 'command '/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
AFNetworking seems to work fine, but adding the WebRTC pod seems to break the project.
Am I missing a limitation of Kotlin multiplatform or something?
This is my build.gradle.kts:
cocoapods {
// Configure fields required by CocoaPods.
summary = "Some description for a Kotlin/Native module"
homepage = "Link to a Kotlin/Native module homepage"
// You can change the name of the produced framework.
// By default, it is the name of the Gradle project.
frameworkName = "shared"
ios.deploymentTarget = "10.0"
pod("GoogleWebRTC", "~> 1.1")
pod("AFNetworking", "~> 4.0.0")
}
I was able to reproduce this issue but I didn't figure out the reason why.
It seems a there's something wrong with GoogleWebRTC pod and the Kotlin-native/cocoapods integration.
I've opened an issue here.
We have an official help.
It seems that the frameworks name is different from the pod name. So a possible solution could be that:
kotlin {
cocoapods {
[...]
pod("GoogleWebRTC", moduleName = "WebRTC", version = "~> 1.1")
[...]
}
[...]
}
I tried it and it works.

Private Swift Pod with ObjC static libraries

I am trying to create a private pod that ships with two static libraries.
Source of the pod is in swift however it depends upon two third party static libraries on which I have no control.
Linting my podspec, I get the following error just for the second lib (with a modulemap)
- ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information.
- ERROR | xcodebuild: TestKit/TestKit/SSAIPolicy.swift:11:8: error: no such module 'SecondLib'
And, here is the source part of my podspec file:
spec.source_files = 'TestKit/*', 'Vendor/**/*.{h,modulemap}'
spec.vendored_libraries = 'Vendor/FirstLib/libService.a', 'Vendor/SecondLib/libManagement.a'
spec.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '-ObjC' }
Note: I am able to use this framework just fine by compiling and dropping it manually in the main app
In case anyone needs to know, I got it working by changing 'Target Membership'
of header files in my library from 'Project' to 'Public'

Include Pod's Bridging-Header to Build Settings of Project's Target?

I created an objective-c pod with two files:
Source/SomeViewController.h
Source/SomeViewController.m
I also created a bridging header in the pod:
Source/Bridging-Header.h
with the content:
#import "SomeViewController.h"
My podspec looks like this:
Pod::Spec.new do |s|
s.name = 'TestLib'
s.version = '0.0.1'
s.license = 'MIT'
s.ios.deployment_target = '7.0'
s.source_files = 'Source/*.{h,m}'
s.requires_arc = true
s.xcconfig = { 'SWIFT_OBJC_BRIDGING_HEADER' => 'Source/Bridging-Header.h' }
end
I created a demo project did pod init and inserted my pod. Then after pod install I get the following output:
Installing TestLib 0.0.1 (was 0.0.1)
Generating Pods project
Integrating client project
[!] The `TestLibProject [Debug]` target overrides the `SWIFT_OBJC_BRIDGING_HEADER` build setting defined in `Pods/Target Support Files/Pods-TestLibProject/Pods-TestLibProject.debug.xcconfig'. This can lead to problems with the CocoaPods installation
- Use the `$(inherited)` flag, or
- Remove the build settings from the target.
[!] The `TestLibProject [Release]` target overrides the `SWIFT_OBJC_BRIDGING_HEADER` build setting defined in `Pods/Target Support Files/Pods-TestLibProject/Pods-TestLibProject.release.xcconfig'. This can lead to problems with the CocoaPods installation
- Use the `$(inherited)` flag, or
- Remove the build settings from the target.
When I open my TestLibProject.xcworkspace file I see that the pod was installed correctly but the bridging header from the pod is not installed correctly. I my Swift project I tried to do:
let vc: SomeViewController
This gives an error because the bridging header from the pod ist not installed.
How do I have to configure the podspec in order to get the bridging header of the pod being installed correctly?
Podspecs build frameworks, and frameworks cannot include bridging headers. If you want to import non-modular code into a Swift framework, you'll need to use a custom module map, instead, e.g. at MyLib/module.modulemap:
framework module MyLib {
umbrella header "MyLib.h"
// Load an SDK header, e.g. CommonCrypto.h
header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/CommonCrypto/CommonCrypto.h"
export *
module * { export * }
}
Once there, you can specify the custom module map both in your Xcode project (either as the MODULEMAP_FILE setting in an .xcconfig file, or as the Module Map File of your target's Build Settings.
Now, the final piece of the puzzle: the podspec. You need to set the module_map:
Pod::Spec.new do |s|
# …
s.module_map = 'MyLib/module.modulemap'
end
The above is how SQLite.swift distributes itself, both as a general framework and as a pod.
Edit: Seems like I missed the point of the original question, as was clarified in this thread. The OP wanted to use a pod framework's bridging header to automatically load itself into the installing project's Swift code. This isn't possible. Even if Swift frameworks did support bridging headers, they would only be able to load Objective-C/C code (i.e. private framework code) into the framework's Swift code.

Cocoapods importing a framework to source code

In my cocoapod for iOS, I have a essentially items:
Open-source classes (.m & .h files)
MyFramework.framework (.framework directory, header files, and .bundle for resources)
One of the open-source classes calls import <MyFramework.MyFramework.h> so it can use the components of MyFramework in its implementation. But because of this, I'm having trouble getting the podspec to pass the spec lint test (pod spec lint MyCocoapod.podspec). When I run the spec lint validation, it says:
ERROR | [iOS] [xcodebuild] .../MyFile.h:54:9: fatal error: 'MyFramework/MyFramework.h' file not found
While investigating, I noticed that the podspec does pass the spec lint validation if I remove that open-source class in the podspec's source_files section, s.source_files = 'MyFiles.{h,m}'. Any idea why my class can't import my custom framework during the spec lint validation?
The relevant code in the podspec looks like this:
s.preserve_paths = 'myframework/MyFramework.framework'
s.frameworks = 'Foundation', 'MyFramework'
s.xcconfig = { 'FRAMEWORK_SEARCH_PATHS' => '$(SRCROOT)/myframework/' }
s.public_header_files = 'MyFramework.framework/Headers/*.h', 'SourceCode/*.h'
s.source_files = 'SourceCode/*.{h,m}' # Crashes here - Source_file imports MyFramework.h. If I take this out, it passes spec lint validation
EDIT This process is now entirely handled by the vendored_frameworks option. This handles preserving the paths, the framework search paths, and linking to the project.
To include a framework you can use:
s.vendored_frameworks = 'path/to/SomeFramework.framework'
To include bundle files do:
s.resources ='path/to/SomeBundle.bundle'

Resources