fbpx

Share Your Framework via CocoaPods

We often use the same code for several applications and websites that we create. To shorten the production time for creating applications and websites, we can reuse the code stored in the dependency manager.

We can create a function inside a custom class and store it inside an application project. After that, we hold the application project to the dependency manager.

Dependency Manager

A dependency manager is an external program or software module (library). Its size can be a single file or a collection of files and folders organized into packages that perform a specific task when integrated into our project.

Dependency manager used to determine:

  1. What dependency to get.
  2. What version of the dependency in particular.
  3. From which repository to get them.

Repository

A repository is a git source from declared dependencies fetched from Pod using the name and version of that dependency.

Most dependency managers have dedicated repositories to fetch the declared dependencies, but CocoaPods does not have dedicated repositories, yet npm, maven, and Gradle have their own reliable repositories.

Example of Dependency Managers:

  1. Composer (primarily used with php projects).
  2. Gradle (used with Java and Kotlin projects for Android apps).
  3. npm: Node Package Manager (with core NodeJS).
  4. ypm: Yarn Package Manager.
  5. Maven (used with Java and Kotlin projects for Android apps).
  6. Cocoapods (Objective-C, Swift, Kotlin).

Package Manager vs Dependency Manager

Package Manager has core systems, for example, to set up your development environment, and with these settings, you can build many projects.

Package Manager such npm (has node.js as its core), a swift package manager (has SwiftUI as its core).

Dependency Manager is a distribution of third-party projects. You manage all dependencies for a single project, which will be saved on your project. When you start another project, you should manage your dependencies again.

Dependency does not have a core, just a third-party project.

CocoaPods

CocoaPods is a dependency manager for some languages such as Objective-C, Swift, and other languages like kotlin, and CocoaPods is used to manage external libraries. CocoaPods was developed by Eloy Durán and Fabio Pelosin, developed in August 2011, and first released on September 1, 2011. It is a third-party project distribution and can automatically integrate into our Xcode and Android projects, which this article will explain. CocoaPods uses a command line to run it and integrated it in Gradle. CocoaPods is just redirection to git repository because CocoaPods does not have a dedicated repository to store project code that is submitted to CocoaPods.


So, the first step to use CocoaPods is to create a git repository. You can use GitHub, BitBucket, etc. In this example, we use GitHub as a git repository. The steps to develop a git repository are :

  1. Create GitHub account
  2. Create New Public Repository
  3. Open the terminal and clone your new repository (git clone URLRepo)
  4. Change directory (cd) to your framework and ls –al (list all)
  5. Create a new xcode project and select cocoa touch framework (iOS) or cocoa app (MacOS) and save it into your framework
  6. Create a new file or copy-paste the file that you need into the project
  7. git status (check for newly modified files)
  8. git add.
  9. git commit –m "your message"
  10. git push origin master (push your local repository)
    After creating git repository, you need to initialize Pod into your project with these steps :
  11. Go to a local repository, cd to the local project name
  12. pod init (create a podfile)
  13. pod spec create your pod name
  14. open yourpodname.podspec –a Xcode (application name)
  15. git tag 1.0.0
  16. git push –tags
  17. pod spec lint (check error in your podspec)

So podspec will be created, you need to rewrite podspec like this :

Pod::Spec.new do |spec|
spec.name = 'ExampleProject’
spec.version = '1.0.0’
spec.homepage = ‘URLRepo without .git’
spec.source = { :git => “URLRepo", :tag => "{spec.version}" }
spec.authors = ‘’
spec.license = ‘’
spec.summary = 'An example project’
spec.source_files = “Classes”, “Classes//.{h,m,swift,kt}” spec.exclude_files = “Classes/Exclude” spec.public_header_files = “Classes//.h” (for objective-c only)
spec.static_framework = true
spec.vendored_frameworks = "ExampleFramework.framework
spec.libraries = "c++"
spec.module_name = "#{spec.name}_umbrellas"
spec.platform = :ios, “11.0”
spec.ios.deployment_target = '13.0’ (ios,osx,watchos,tvos,kotlin for multi deployment target)
end

You can change the spec with another name such as a, b, c, etc.

CocoaPods can share your framework via remote public repository and share via remote private repository. A secret framework can be put in a private git repository. To share via remote private repository, you need to git clone to a local folder and share it using pod command. You can use these steps to do that :

  1. pod init (on another local project to make sure it has podfile)
  2. open Podfile (other local project podfile)
  3. Type pod "yoursharedframework", :path => "your local directory path"
  4. pod install
  5. open yourotherlocalproject.xcworkspace
  6. Type import yoursharedframework, then call classes and their function
    You can publish the framework in the public git repository if the framework is not confidential. To share via remote public repository use pod command, you can use these steps :
  7. git add .
  8. git commit –m "your message"
  9. git push origin master
  10. cd yoursharedframework (which has xcodeproj, podfile, and podspec)
  11. pod trunk me
  12. pod trunk register emailAddress 'yourName' --description='macbook pro’
  13. Confirm your cocoapods registration by clicking or copy-paste URL from cocoapods that sent to your email to your browser
  14. pod trunk push

pod trunk push is to share your framework to CocoaPods after you type code to register to CocoaPods before. After the pod trunk push, the terminal will show you CocoaPods URL for your submitted project. That URL is just redirected to your git remote public repository.

To get your shared framework, you need to do these steps :

  1. Create or open another project
  2. pod init
  3. open Podfile
  4. pod "yoursharedframework"
  5. pod install
  6. open anotherproject.xcworkspace
  7. Type import yoursharedframework
  8. Called shared framework classes and their function inside another project file

The pod command in the podfile allows you to combine a public framework and a secret framework in the same project. It simplifies the time to build an application, so there is no need to retype all the code that we need. Simply by importing in the podfile, you can use the framework you need by importing the framework at the beginning of the Objective-C, Swift, and Kotlin files you create in your project.

If you have changed your public git repository and your private git repository, then you need to update your project by using the terminal in your project folder to type :
$ pod update podName

CocoaPods Integration to Android Project

Native Kotlin can integrate with the CocoaPods. You can add dependencies on Kotlin multiplatform mobile projects with a CocoaPods dependency (Kotlin's Pod).
You can edit Pod dependencies with IntelliJ IDEA code editor, Visual Studio Code, and another code editor. You can integrate CocoaPods in Kotlin Gradle's plugins.
Install the CocoaPods dependency manager and plugin

  1. Install CocoaPods
    $ sudo gem install cocoapods
  2. Install cocoapods-generate command
    $ sudo gem install cocoapods-generate
  3. In build.gradle, apply the CocoaPods plugin as Kotlin Multiplatform plugin.
plugins {
kotlin("multiplatform") version "1.6.0"
kotlin("native.cocoapods") version "1.6.0"
}
  1. Configure summary, homepage, and baseName of the Podspec file in the cocoapods {}.
plugins {
    kotlin("multiplatform") version "1.6.0"
    kotlin("native.cocoapods") version "1.6.0"
}

// CocoaPods demands the podspec to have a version of Gradle.
version = "1.1"

kotlin {
    cocoapods {

        framework {
            // CocoaPods Required Configure fields.
            summary = "your summary"
            homepage = "URL git repository without git"
            // Framework name
            baseName = "MyFramework"
            // (Optional) if use Dynamic framework support
            isStatic = false
            // (Optional) if use Dependency export
            export(project(":anotherFramework"))
            transitiveExport = true
            // (Optional) if use BITCODE
            embedBitcode(BITCODE)
        }

        // Maps custom Xcode configuration to NativeBuildType
        xcodeConfigurationToNativeBuildType["CUSTOM_DEBUG"] = NativeBuildType.DEBUG
        xcodeConfigurationToNativeBuildType["CUSTOM_RELEASE"] = NativeBuildType.RELEASE
    }
}
  1. Re-import the project to add a new dependency.
  2. During and Ecode build, generate the Gradle wrapper to ensure compatibility.
    A Kotlin project necessitates the pod (…) function call in build. Gradle, each dependency requires a separate pod(…) function call. Specify the parameters for the dependence in the configuration block of the function.

Add a Dependency from The Remote Cocoapods Repository

Include dependencies on a Pod library from a remote CocoaPods repository with pod(…) to build.gradle:

  1. Specify the name of a remote CocoaPods repository in the pod(…) function. If you do not want to use the latest version, you can specify the version.
  2. Specify the minimum iOS deployment target version for the Pod library.
kotlin {
ios() cocoapods {
ios.deploymentTarget = "14.1" summary = "your summary" homepage = "URL git repository" pod("yourFramework") { version = "~> 3.1.2" } }
}
  1. Re-import the project to add a new dependency.

To use these dependencies in Kotlin code, import the packages cocoapods <libraryName>.import cocoapods.yourFramework.*

Add a Dependency from Local Stored Pod Library

You can add a dependency from the local stored Pod library with pod(…) to build.gradle:

  1. Specify the name of a local Pod library in the pod(…) function. Specify the local Pod library path: use the path(…) function in the source parameter value.
  2. Specify the minimum iOS deployment target version for the Pod library.
kotlin {
    ios()

    cocoapods {
        summary = "your summary"
        homepage = "URL git repository"

        ios.deploymentTarget = "14.1"

        pod("pod_dependencies") {
            version = "1.1"
            source = path(project.file("../pod_dependencies"))
        }
        pod("subspec_dependencies/Core") {
            version = "1.1"
            source = path(project.file("../subspec_dependencies"))
        }
        pod("yourFramework") {
            version = "~> 3.1.2"
        }
    }
}
  1. Re-import the project to add a new dependency.

To use these dependencies in Kotlin code, import the packages cocoapods <libraryName>.
import cocoapods.pod_dependencies.*
import cocoapods.subspec_dependencies.*
import cocoapods.yourFramework.*

Add a Dependency from The Git Repository Based on Commit, Tag, Branch

  1. Specify the name of a custom Pod library in the pod(…) function. In the configuration block, identify the path to the git repository: use the git(…) function in the source parameter value. Additionally, you can specify the following parameters in the block after git(…):
  • commit (specific commit from the repository)
  • tag (specific tag from the repository)
  • branch (specific branch from the repository)

The git(…) passed parameters in the following order: commit, tag, branch. The Kotlin plugin uses HEAD taken from the master branch if you don't specify any parameter.

  1. Specify the minimum iOS deployment target version for the Pod library.
kotlin {
    ios()

    cocoapods {
        summary = "your summary"
        homepage = "URL git repository"

        ios.deploymentTarget = "14.1"

        pod("yourFramework") {
            source = git("URL git repository yourFramework with git") {
                tag = "3.1.2"
            }
        }

        pod("anotherFramework1") {
            source = git("URL git repository anotherFramework1 with git") {
                branch = "branch name"
            }
        }

        pod("anotherFramework2") {
            source = git("URL git repository anotherFramework2 with git") {
                commit = "commit number"
            }
        }
    }
}
  1. Re-import the project to add a new dependency.

To use these dependencies in Kotlin code, import the packages cocoapods <libraryName>.
import cocoapods.yourFramework.*
import cocoapods.anotherFramework1.*
import cocoapods.anotherFramework2.*

Add a Dependency from an Archived Pod Library

To build, you can add dependencies from the archived Pod library with extension zip, tar, or jar with pod(…) command.gradle:

  1. Name of a Pod library in the pod(…) function. Specify the path to archive: use the url(…) function with URL git repository with archive extension in the url parameter value. Optionally, Boolean flatten parameter indicates that all the Pod files are located in the root directory of the archive.
  2. Specify the minimum iOS deployment target version for the Pod library.
kotlin {
    ios()

    cocoapods {
        summary = "your summary"
        homepage = "URL git repository"

        ios.deploymentTarget = "14.1"

        pod("poddependency") {
            source = url("URL git repository with archive extention", flatten = true)
        }
    }
}
  1. Re-import the project to add a new dependency.

To use these dependencies in Kotlin code, import the packages cocoapods <libraryName>.
import cocoapods.poddependency.*

Add a Dependency with Custom Cinterop Options

You can add dependencies with custom cinterop options with pod(…) to build.gradle:

  1. Specify Pod library name in the pod(…) function. In the configuration block stipulate the cinterop options:
    extraOpts (specify the list of options for a Pod library) with: extraOpts = listOf("compilerOption")
    packageName (specify the package name) with import .
  2. Specify the minimum iOS deployment target version for the Pod library.
kotlin {
    ios()

    cocoapods {
        summary = "your summary"
        homepage = "URL git repository"

        ios.deploymentTarget = "14.1"

        useLibraries()

        pod("yourFramework") {
            packageName = "yourPackageName"
        }
    }
}
  1. Re-import the project to add a new dependency.

To use these dependencies in Kotlin code, import the packages cocoapods <libraryName>.
import cocoapods.yourFramework.*
If you use the packageName parameter, the package name import: can import the library
import yourPackageName.yourModule
import yourPackageName.yourModule

Add a Dependency on a Static Pod Library

Add some dependencies on a static Pod library with pod(…) and use Libraries() to create .gradle:

  1. Specify the library name using the pod(…) function.
  2. Call the useLibraries() function, enabling a special flag for static libraries.
  3. Specify the minimum iOS deployment target version for the Pod library.
kotlin {
    ios()

    cocoapods {
        summary = "your summary"
        homepage = "URL git repository"

        ios.deploymentTarget = "14.1"

        pod("yourFramework") {
            version = "~> 4.3"
        }
        useLibraries()
    }
}
  1. Re-import the project to add a new dependency.

To use these dependencies in Kotlin code, import the packages cocoapods..

import cocoapods.yourFramework.*

Import Kotlin Project to Xcode Project

Changes Podfile to import Kotlin project in an Xcode:

  • If your project has any custom Podspec git repository with git dependencies, you should also specify the path to the Podspec git in the Podfile.
    By including a dependency on yourPodspec, declare the path to the yourPodspec in the Podfile:
    target 'ios-app' do
    # … other depedencies …
    pod 'yourPodspec', :path => 'ext /url/yourPodspec'
    end
    The:path is filepath to the Pod.
  • When you add a library from the custom Podspec repository, you should stipulate the source of the specs at the beginning of your Podfile:
    source 'URL git repository with git'
    target 'your target' do
    # … other dependencies …
    pod 'example'
    end

Remember to always re-import the project after making changes in Podfile.

Add a Dependency Kotlin Pod with One Target Xcode Project

The steps are :

  1. Create an Xcode project with a Podfile.
  2. To create .gradle, include the path to your Xcode project Podfile with podfile = project.file(..). This calling pod installs your Podfile and synchronizes your Xcode project with Kotlin Pod dependencies.
  3. Specify the minimum target version of the Pod library.
kotlin {
    ios()

    cocoapods {
        summary = "your summary"
        homepage = "URL git repository"
        ios.deploymentTarget = "14.1"
        pod("yourFramework") {
            version = "~> 3.1.2"
        }
        podfile = project.file("../iosApp/Podfile")
    }
}
  1. Add the name and path of the Kotlin Pod to include in the Xcode project to Podfile.
use_frameworks!
platform :ios, '14.1'
target 'iosApp' do
        pod 'kotlin_libraries', :path => '../kotlin-libraries'
end
  1. Re-import the project to add a new dependency.

Add a Dependency Kotlin Pod with Several Targets Xcode Project

The steps are :

  1. Create a Podfile in your Xcode project.
  2. Add the path to your Podfile with code podfile = project.file(..) to file build.gradle. This will call the pod install in Podfile, and it will synchronize Xcode project with Kotlin Pod dependencies.
  3. Add dependencies to needed libraries with pod("yourFramework").
  4. For each dependency, specify the needed version of the Pod library if you do not want to use the latest version.
kotlin {
    ios()
    tvos()

    cocoapods {
        summary = "your summary"
        homepage = "URL git repository"
        ios.deploymentTarget = "14.1"
        tvos.deploymentTarget = "13.0"

        pod("yourFramework") {
            version = "~> 3.1.2"
        }
        podfile = project.file("../targetedXcodeProject/Podfile") // path to Podfile
    }
}
  1. Include the name and path of the Kotlin Pod in the Xcode project to the Podfile.
target 'iosApp' do
  use_frameworks!
  platform :ios, '14.1'
  # Pods for iosApp
  pod 'kotlin_libraries', :path => '../kotlin-libraries'
end

target 'TVosApp' do
  use_frameworks!
  platform :tvos, '13.0'

  # Pods for TVosApp
  pod 'kotlin_libraries', :path => '../kotlin-libraries'
end
  1. Re-import the project to add new dependency.

Advantages of Using Cocoapods

The reason why we should use CocoaPods as a dependency manager when building an application are:

  1. It makes it easier for us to use and integrate the required external libraries into the application project that is being built. CocoaPods provide documentation detail needed to explain how to integrate it and use some functions from the library. You can test integrating dependency into your project using the command pod; try projectName.
  2. It makes it easier for us to update the external library used. So we don't need to download it by downloading the required library repository many times to get the latest version of the external library used. pod update will automatically update to a new version using one command. It's easy to check if a new version of a dependency is available using an outdated command pod.
  3. Development becomes more agile.
  4. A library may refer to another dependency, which made you manually download each of them one by one. CocoaPods will do it automatically for you.
  5. Library has specific requirements like OS version, framework imports, etc. CocoaPods will check automatically for you.
  6. It will be difficult to find manual added dependency if you work with your team. With CocoaPods, you need to read Podfile to know all manually added dependencies.
  7. When updating libraries, using CocoaPods will update it faster than if you use Carthage within the same git repository.

Disadvantages of Using CocoaPods

There are some disadvantages when using CocoaPods as a dependency manager to build an application:

  1. It does not have core systems as a dependency manager.
  2. It takes longer to download Cocoapods main specs repository during the first installation.
  3. Some third-party frameworks are not updated to the project language’s minimal version.
  4. Updating the Ruby version is required if you are using macOS.
  5. Other dependency managers such as Swift package manager and Carthage cannot be used when you are using CocoaPods.

References

Author: Oscar Perdana K.A. , Software Analyst

Share

Get the latest news from us to your inbox

(Weekly newsletter)

Leave a comment



from Indonesia:
from Australia:
from New Zealand:
from Singapore:
Our social media
        
© Copyright 1991 - 2022 Mitrais