iOS Dyanmic Framework with Firebase Dependencies - ios

We are developing a framework that is dependent on some firebase dependencies like login, analytics, etc. Once our framework is developed we will distribute it to our customers.
Things to be taken care of are
Code should not be visible(Best suggestion is to create XCFramework)
If possible create a dynamic framework instead of a static framework
Can be distributed via Swift package manager or cocoapods
What we have tried
We have tried to create a dynamic framework using pods and then create a XCFramework. But while importing into client app, pods module are not found
We created static library and add firebase manually(In project directly) instead of pods, in that case XCFramework is not imported
We have tried to create XCFrame Work as mentioned here (for dynamic framework)
XCFramework with Pods Dependencies
To hide code umbrella framework and universal library can be used,but with firebase, this approach is to typical and also not suggested on the internet at many places
Is there any other/alternative way where we can fulfill our requirements?

We have the exact same setup right now, and it works quite well. Hope that'll also be of help for you.
Things that are taken care of:
It's an XCFramework distribution.
It's been distributed by CocoaPods (albeit in a private Podspec repository)
It's a dynamic framework.
Prerequisites:
CocoaPods version >= 1.10.1
Xcode version >= 11.6 (could be lower though, not sure)
After creating your .xcframework, you need to have a .podspec for your framework, which should look like:
Pod::Spec.new do |s|
# ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
s.name = "MyAwesomeSDK"
s.version = "1.0.0"
s.summary = "Best framework ever: MyAwesomeSDK"
s.description = <<-DESC
"Best framework ever: MyAwesomeSDK"
DESC
s.homepage = "http://github.com"
s.license = "MIT"
s.author = { "ItIsI" => "me#myself.com" }
# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
s.platform = :ios
s.ios.deployment_target = '11.3'
# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
s.source = { :http => '<MyAwesomeSDK.zip> (We're storing our zipped .xcframework in a hosted page)' }
s.vendored_frameworks = 'MyAwesomeSDK.xcframework'
s.swift_version = "5.0"
# ――― Dependencies ―――――――――――――――――――――――――――---――――――――――――――――――――――――――――――― #
s.dependency 'SwiftProtobuf', '1.12.0'
s.dependency 'lottie-ios', '3.1.8'
# Any other dependency you might need.
end
Then, we are consuming it in another project via Podfile, that will look like:
platform :ios, '13.0'
# If you're going to have a private Podspec repo, add the source URL here.
# Don't forget to add the original source if you're going to specify another source.
# source 'https://cdn.cocoapods.org/'
target 'Test' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# If you are publishing the SDK publicly or in a private Podspec repository, this is it:
pod 'MyAwesomeSDK'
# If not, you should provide the .podspec to your customers, and:
pod 'MyAwesomeSDK', :podspec => '<path/to/MyAwesomeSDK.podspec>'
target 'TestTests' do
inherit! :search_paths
# Pods for testing
end
target 'TestUITests' do
# Pods for testing
end
end
Then, that's it! When running pod install, you should see:
Analyzing dependencies
Downloading dependencies
Installing MyAwesomeSDK (1.0.0)
# These are our own:
# ---
Installing SwiftProtobuf (1.12.0)
Installing lottie-ios (3.1.8)
# ---
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 3 total pods installed.
P.S: We also had to add a post_install setup in our Podfile, otherwise it'll not properly link the dependency frameworks:
if ["SwiftProtobuf", "lottie-ios"].include? target.name
target.build_configurations.each do |config|
config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
end
end

Related

How to add a thirdparty SDK (multiple .framework files) to react native library module?

I have built a react native library module (with RN 0.63). This module depends on some thirdparty SDKs. When integrated with Android (using .aar files) it works just fine. In case of iOS, I have been able to get the library module working without the SDK (using swift hence with the bridging header). On adding the SDK, I am getting errors such as .h is not avaialble.
This is my directory My directory structure:
react-native-lib
--android
--ios
----MyCls.swift
----MyCls.m
----react-native-lib-Bridging-Header.h
----SDKS
------DEBUG
--------A.framework
--------B.framework
--------A-Debug.podspec
--------B-Debug.podspec
------THIRDPARTY
--------JSONModel.framework
--------CocoaLumberjack.framework
--------... other frameworks
--react-native-lib.podspec
--Example
--index.js
--Logger.swift
--package.json
I have a sample application in Swift which uses the SDKS folder, but I cannot seem to get RN to recognize the framework files/headers.
The last few lines of the Podspec file of react-native-lib is as follows:
...
s.dependency "React"
s.dependency 'JSONModel', '~> 1.8.0'
s.dependency 'CocoaLumberjack', '~> 3.6.1'
My example application Podfile:
require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/#react-native-community/cli-platform-ios/native_modules'
platform :ios, '11.0'
use_frameworks!
project 'example', {
'Debug' => :debug,
'Release' => :release,
}
#
def applibs
pod 'A-Debug', :configuration => ['Debug'], :path => '../node_modules/react-native-lib/ios/SDKS/DEBUG/A-Debug.podspec'
# ... A-Release, B-Debug, B-Release
# The release folders not shown in structure above.
end
target 'example' do
config = use_native_modules!
use_react_native!(:path => config["reactNativePath"])
applibs
# Disabled Flipper because of use_frameworks!
end
I am not sure what I am doing wrong and how I can overcome this issue. There seems to be not quite a lot of articles on how such 3rd party sdk can be integrated in a library module. I have explored similar questions like this one which is still unsolved and has insufficient information.
after days of research and experimenting, I have been able to resolve the problem. It's simple enough, made difficult with lack of resources on the topic.
Primarily, I used the podspec file in my react native lib (ios folder) to add dependency on the 3rd party frameworks as follows.
react-native-lib.podspec
s.dependency 'A-Debug', '~> 1.2.3', :configurations => :debug
s.dependency 'B-Debug', '~> 2.3.4', :configurations => :debug
s.dependency 'A-Release', '~> 1.2.3', :configurations => :release
s.dependency 'B-Release', '~> 2.3.4', :configurations => :release
In my example application, the podfile works as shown above (by adding the pods in applibs). However, I encountered the 'enable bitcode' error where the compiler asked me to recompile the 3rd party libraries with bitcode enabled. I worked around it with the following post install script in the application (not library) podfile (obtained from here).
Example/ios/Podfile
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
end
end
end
Do a cache clean, and as an extra measure, clear node modules.
Then simply run the following in your application directory:
yarn install && cd ios && pod install && cd .. && yarn react-native start
Open your project in Xcode and import your SDK as per its documentation. Hope this saves you hours of research, experiment and debugging.

Using the framework from one target as a pod in another target locally

I'm having trouble using a framework I wrote in other targets (the same project) using modular imports. I'm using Cocoapods. I'm getting Could not build module: errors while trying to import the module using modular imports (#import CMPComapiFoundation;). I attach a link to the repo for more information.
I did try both local (:path =>) and remote (:git =>) ways of pulling the SDK in Podfile, none of which seems to work. It's worth noting that if added via Cocoapods in a separate project, the code compiles and the SDK can be imported.
SDK's .podspec file:
Pod::Spec.new do |s|
s.name = 'CMPComapiFoundation'
s.version = '1.2.2'
s.license = 'MIT'
s.summary = 'Foundation library for connecting to and consuming COMAPI services'
s.description = <<-DESC
# iOS SDK for Comapi
Client to connect your iOS application with [Comapi](http://comapi.com/) services and add it as a channel to our cloud messaging platform. Written in Objective-C.
For more information about the integration please visit [the website](http://docs.comapi.com/reference#one-sdk-ios).
DESC
s.homepage = 'https://github.com/comapi/comapi-sdk-ios-objc'
s.author = { 'Comapi' => 'support#comapi.com' }
s.source = { :git => 'https://github.com/comapi/comapi-sdk-ios-objc.git', :tag => s.version.to_s }
s.social_media_url = 'https://twitter.com/comapimessaging'
s.ios.deployment_target = '10.0'
s.requires_arc = true
s.source_files = 'Sources/**/*.{h,m}'
s.resources = []
s.dependency 'SocketRocket'
end
And here's the Podfile I'm using for the entire project:
platform :ios, '10.0'
use_frameworks!
def shared
pod 'CMPComapiFoundation', :path => '/Users/dominik.kowalski/Documents/comapi-sdk-ios-objc'
pod 'JWT'
end
target 'CMPComapiFoundation' do
pod 'SocketRocket'
end
target 'CMPComapiFoundationTests' do
shared
end
target 'ComapiFoundationSample' do
shared
end
target 'ComapiFoundationSample-Swift' do
shared
pod 'SnapKit'
end
I expect the test and sample targets to import the modules and compile the code.
Okay I managed to fix it myself. Some header files were missing Target membership, but Xcode gave me misleading hints.

Custom cocoapod not finding swift file

I'm trying to create a custom Cocoapod to handle all of my networking calls for my iOS application. The issue that I am having is that some of my files that should be part of my new Cocoapod/framework are not found when I try to reference them in code.
I followed this tutorial https://www.raywenderlich.com/5823-how-to-create-a-cocoapod-in-swift and I successfully created the Cocoapod .xcworkspace project (named: ios-oauth2-rest-template) and added it to bitbucket and created a private PodSpec repo on bitbucket as well (named: KPodSpec) (two separate repos).
I was able to make it through the tutorial but I got stuck on the part 'Using Your New CocoaPod.' I was able to run the following commands to add the Cocoapod to my PodSpec repo:
pod repo add KPodSpecs [Your RWPodSpecs Git URL]
pod repo push KPodSpecs ios-oauth2-rest-template.podspec
When I try to use my new pod in an existing project (named: KApp) some of the files that are in my Cocoapod are able to be referenced while others will not autocomplete even after I import ios_oauth2_rest_template.
I'm not sure if I'm just missing the framework for ios-oauth2-rest-template inside my KApp project. Any help would be much appreciated.
KApp Podfile:
# Uncomment the next line to define a global platform for your project
platform :ios, '12.0'
source 'https://github.com/CocoaPods/Specs.git'
source 'https://bitbucket.org/kpodspecs.git'
target 'KApp' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for KApp
target 'KAppTests' do
inherit! :search_paths
# Pods for testing
end
target 'KAppUITests' do
inherit! :search_paths
# Pods for testing
end
pod 'LBTAComponents'
pod 'SwiftyJSON', '~> 4.0'
pod 'TRON', '~>4.0'
pod 'ios-oauth2-rest-template', '~> 0.0.1'
end
ios-oauth2-rest-template podspec:
Pod::Spec.new do |s|
# 1
s.platform = :ios
s.ios.deployment_target = '12.0'
s.name = "ios-oauth2-rest-template"
s.summary = "My summary here"
s.requires_arc = true
# 2
s.version = "0.0.1"
# 3
s.license = { :type => "Proprietary", :file => "LICENSE" }
# 4 - Replace with your name and e-mail address
s.author = { "My Name" => "myemail#email.com" }
# 5 - Replace this URL with your own GitHub page's URL (from the address bar)
s.homepage = "https://bitbucket.org/myStuff"
# 6 - Replace this URL with your own Git URL from "Quick Setup"
s.source = { :git => "https://bitbucket.org/myStuff.git",
:tag => "#{s.version}" }
# 7
s.framework = "UIKit"
s.dependency 'Heimdallr', '~> 3.6.1'
# 8
s.source_files = "ios-oauth2-rest-template/**/*.{swift}"
# 9
#s.resources = "RWPickFlavor/**/*.{png,jpeg,jpg,storyboard,xib,xcassets}"
# 10
# s.swift_version = "4.0"
end
ios-oauth2-rest-template Podfile:
# Uncomment the next line to define a global platform for your project
platform :ios, '12.0'
target 'ios-oauth2-rest-template' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
use_frameworks!
# Pods for ios-oauth2-rest-template
pod 'Heimdallr', '~> 3.6.1'
target 'ios-oauth2-rest-templateTests' do
inherit! :search_paths
# Pods for testing
end
end
Thanks in advance. I have scoured the internet to no avail as to why this could be happening and I am stumped since I don't have that much experience with CocoaPods in the first place.
UPDATE
Here are some screenshots
The Pods section of the KApp, to show some of my classes. When I try to use ApplicationUtils it works just fine and autocompletes, but when I try to use BaseClass it cannot seem to find it.
Build phases under settings to show that all 30 files are under compile sources. The 30 files match the 30 files from the ios-oauth2-rest-template Xcode project. The 31st file is ios-oauth2-rest-template-dummy.m.
Image showing the folder structure of ios-oauth2-rest-template Cocoapod project.
Thanks to #balazs630, I was able to solve the problem. I had to make sure my access control was set to public or open in order to access them. Looking back at my code, ApplicationUtils was a public class and BaseClass had no designation, so it was by default internal, which according to the docs:
Internal access enables entities to be used within any source file
from their defining module, but not in any source file outside of that
module.
For anyone with a similar problem, check to make sure your access control is set appropriately. Here are some sources to familiarize yourself with access control in Swift:
https://medium.com/#abhimuralidharan/swift-3-0-1-access-control-9e71d641a56c
https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html

How to import Alamofire/AFNetworking in my custom cocoapod

How do I include Alamofire (a web request pod like AFNetworking) in my cocoapod source files? I have a service in my cocoapod that needs to make web requests using Alamofire, my cocoapod doesn't seem to have a podfile that I can see, so I don't know how to add the dependency to my cocoapod.
I am creating a cocoapod using pod lib create The build fails whenever I go to import Alamofire in any of my files. In a normal project, I'd just add Alamofire to my podfile, but this is a cocoapod, so I can't figure out where to add the dependency, or how to get it to build successfully.
I followed the guide here but it doesn't say anything about importing another pod into my cocoapod's files.
My directory structure looks like this:
MyLib.podspec
Example/
MyLib example project
Pod/
Source files for my cocoapod
If your pod depends on other pods you can define that in your pod's .podspec file. You can add dependencies there.
Have a look at RealmSwift's podspec file as an example. The RealmSwift pod has a dependency to the Realm pod. This is defined in RealmSwift.podspec:
Pod::Spec.new do |s|
s.name = 'RealmSwift'
s.version = `sh build.sh get-version`
s.summary = 'Realm is a modern data framework & database for iOS & OS X.'
s.description = <<-DESC
The Realm database, for Swift. (If you want to use Realm from Objective-C, see the “Realm” pod.)
Realm is a mobile database: a replacement for Core Data & SQLite. You can use it on iOS & OS X. Realm is not an ORM on top SQLite: instead it uses its own persistence engine, built for simplicity (& speed). Learn more and get help at https://realm.io
DESC
s.homepage = "https://realm.io"
s.source = { :git => 'https://github.com/realm/realm-cocoa.git', :tag => "v#{s.version}" }
s.author = { 'Realm' => 'help#realm.io' }
s.requires_arc = true
s.social_media_url = 'https://twitter.com/realm'
s.documentation_url = "https://realm.io/docs/swift/#{s.version}"
s.license = { :type => 'Apache 2.0', :file => 'LICENSE' }
# ↓↓↓ THIS IS WHERE YOU DEFINE THE DEPENDENCY TO ANOTHER POD ↓↓↓
s.dependency 'Realm', "= #{s.version}"
# ↑↑↑ THIS IS WHERE YOU DEFINE THE DEPENDENCY TO ANOTHER POD ↑↑↑
s.source_files = 'RealmSwift/*.swift'
s.prepare_command = 'sh build.sh cocoapods-setup without-core'
s.preserve_paths = %w(build.sh)
s.pod_target_xcconfig = { 'SWIFT_WHOLE_MODULE_OPTIMIZATION' => 'YES',
'APPLICATION_EXTENSION_API_ONLY' => 'YES' }
s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.9'
s.watchos.deployment_target = '2.0' if s.respond_to?(:watchos)
end
You should check out the AlamofireImage project. It uses Carthage to add the Alamofire submodule to the project. The Alamofire project is then added as a dependency to the AlamofireImage project.
The AlamofireImage.podspec also demonstrates how to add Alamofire as a dependency for CocoaPods. If you follow the AlamofireImage project structure exactly, you'll be up and running in no time. Here are some useful commands to get you going:
Cartfile
github "Alamofire/Alamofire" ~> 3.0
Checkout through Carthage
brew update
brew doctor
brew install carthage
// or
brew upgrade carthage
carthage update --no-build --use-submodules
Hopefully that helps!
If you have created a pod and in your .podspec file you are trying to add a dependency (like Alamofire, RealmSwift..) after that you should go to the Example/.. folder and do a pod install to make the dependencies required from the .podspec of your custom pod visible to the .swift files in your custom pod/framework.
A typical example of a pod project folder hierarchy would be:
- MyLib/
- _Pods.xcodeproj
- Example/ // <-- do a pod install under this folder in order to get the dependencies declared in your .podspec
- Podfile
- MyLib.xcodeproj
- MyLib.xcworkspace
- MyLib/
- Classes/ // <-- folder with pod specific logic that also uses Alamofire
- Assets/
- MyLib.podspec // <-- your podspec with dependencies (Alamofire..)

Using dependency in swift module (framework)

I am trying to create a swift module (Cocoa Touch Framework) with reusable code inside the environment set up by cocoa pods which includes third party libraries written in Objective-C (namely here Restkit).
Unfortunately I am not able to use Restkit in the module I create.
Here's what I did to create the module:
File -> New target: Cocoa Touch Framework, Language: Swift, Project: MyProject, Embed in Application: MyProject
In the "Info" tab of the project settings in the "Configurations" section I define the Pods.debug and Pods.release xcconfig file for my newly created target.
In the header file, which Xcode automatically created for me, networkModule.h, I add the following line:
#import <RestKit/RestKit.h>
Result: When trying to compile I get the error "include of non-modular header inside framework module 'networkModule'"
I have set the flag for "Allow Non-modular Includes in Framework Modules" to YES in the build settings for the Project Target and the Module/Framework target.
I went to the Cocoa pod project and have tried setting the visibility of the RestKit.h Header file to "Public" in the target membership (which of course is not a good solution to mess with the cocoa pods environment)
I am not able to compile. I still get the same error.
Is it possible in the first place to create a Cocoa Touch Framework with dependencies to a cocoa pod managed framework?
Btw. My first idea of creating a private cocoa pod didn't work out as well, as it doesn't seem to be supported, although I am using the prerelease of cocoa pods 0.36 with support for swift.
You should be able to make your won private Pod. You just need to make a podspec for it. Here is an example of one.
Pod::Spec.new do |s|
s.name = "Commons"
s.version = "1.0.0"
s.summary = "Common code for my iOS projects."
s.license = {:type => 'Commercial', :text => "Copyright (c) Dan Leonard(Or Your Name?). All rights reserved." }
s.source = { :git => "https://github.com/PATHTOPOD", :tag =>
s.version.to_s }
s.homepage = "https://github.com/PATHTOPOD"
s.requires_arc = true
s.ios.deployment_target = '9.0'
s.subspec 'Core' do |ss|
ss.source_files = '*.swift'
end
s.subspec 'Menu' do |ss|
ss.source_files = 'Menu/*.swift'
ss.resources = ['Menu/*.storyboard', 'Menu/*.xcassets']
ss.dependency 'Alamofire'
end
end
Then Inside your project you just have to do pod init open your podfile that was just created and add this
source 'https://github.com/CocoaPods/Specs.git'
xcodeproj 'YOURPROJECT.xcodeproj'
platform :ios, '9.0'
use_frameworks!
pod 'Commons', git: 'https://github.com/PATHTOPODPROJECT'
#pod 'Commons', :path => '/Users/YourUser/Path/To/Project/commons'
pod 'KeychainSwift'
pod 'SQLite.swift', git: 'https://github.com/stephencelis/SQLite.swift.git'
Now in this example Podfile Commons is stated twice the second is commented out. If you uncomment it and comment out the first then do pod install in your projects folder from the terminal. This will make a DevelopmentPod which is a pod that is local. This way you can make changes to the pod locally within Xcode. No switching and pod installing every time you make a change.
You will import the pod just like any other by putting
import Commons not #import <Commons/Commons.h> That is how you do it in Objective C not Swift
Once you have a working version commit it to git hub and point your project to the the github version.
Hope this helps.

Resources