Create Cocoa Touch Framework and publish it.

Rageeni Jadam
6 min readJul 2, 2018

--

In this article I will give a demo how to create and distribute Cocoa Touch Framework without sharing the source code.

Overview

  1. Cocoa Touch Framework vs. Cocoa Touch Static Library.
  2. Create Cocoa Touch Framework.
  3. Write source code for creating Cocoa Touch Framework.
  4. Test Framework without publishing it
  5. Create Pod Specification file.
  6. Validate .podspec file.
  7. Publish Cocoa Touch Framework pods.
  8. Test Framework after publishing it.

1. Cocoa Touch Framework vs. Cocoa Touch Static Library

A. A static library is a collection of compiled source files which is then linked directly into an app’s binary. That is, it becomes a part of your app’s binary. A framework is a standalone entity that contains a binary containing compiled code (along with any related resources) that is a standalone unit itself.

B. Cocoa Touch Frameworks are supported from iOS 8 onwards. If you need to support iOS versions before iOS 8, you should consider using a Cocoa Touch Static Library and writing your code in Objective-C.

C. Cocoa Touch Static Libraries do not support Swift. So if you decide to use a Cocoa Touch Static Library, you need to use Objective-C.

D. Frameworks also require less manual work to setup in a project than static libraries.

2. Create Cocoa Touch Framework

A. To create Cocoa Touch Framework:

File > New > Project (command + shift + N) then select Cocoa Touch Framework under iOS tab.

Create Cocoa Touch Framework

B. Enter your framework name & other details. Select language Swift/Objective C as you prefer.

Enter Detail of Framework

C. Created framework will look like.

3. Write source code for creating Cocoa Touch Framework

A. Create New File:

  • Right click on your project name folder then select New File.
  • Select Swift File under iOS tab.
  • Enter file name and click on Create button.

B. Create public class:

C. Generate framework for iPhone Simulator by build the project with any iOS Simulator, similarly for Generic iOS Device.

D. Show framework in Finder.

4. Test Framework without publishing it

A. Create demo project for framework:

  • To create demo project, File > New > Project (command + shift + N) then select Single View App under iOS tab.
  • Enter your project name & other details.

B. Attach framework with your project:

  • Drag and drop framework to Embedded Binaries in General Section.
  • Copy framework to project and click Finish button.

C. Import framework and call its method:

D. Run the project (command + R).

5. Create Pod Specification File

A pods specification file (.podspec file) describes a version of Pod library. It includes details about where the source should be fetched from, what files to use, the build settings to apply, and other general metadata such as its name, version, description, homepage, license, author, and more.

For more detail about podspec read https://guides.cocoapods.org/syntax/podspec.html

A. For publish the Cocoa Touch Framework you need to create podspec file

  • For create podspec file open terminal and change directory to your project folder.

B. Enter following command for generate .podspec.

pod spec create demoTouchFramework

Create podspec file

C. For source you need to upload zip including framework to your server or AWS.

  • For create a zip archive by running command in Framework folder.

zip -r demoTouchFramework.zip demoTouchFramework.framework

Create Archive

D. demoTouchFramework.podspec file:

6. Validate .podspec file

  • For validate podspec file run following command at terminal.

pod spec lint

Validate podspec

7. Publish the Pod

A. To publish your pods firstly you need to register your account by running command.

pod trunk register YOUR_EMAIL_ID ‘YOUR_NAME’

Now, check your e-mail and confirm your CocoaPods session by clicking the conformation link.

Your account is activated now you can publish the cocoa pod by running command.

pod trunk push demoTouchFramework.podspec

8. Test Framework after publishing it

A. Create new demo project or use previous one (delete demoTouchFramework.framework).

B. For create podfile open terminal and change directory to your project folder and run following commands.

  • For initialise the pods

pod init

  • For open pod file

open podfile -a xcode

  • Write pod name inside podfile

pod ‘demoTouchFramework’

  • For install pod and update local repo

pod install — repo-update

C. Open demoExample.xcworkspace and run the project (command + R).

Yes !! I done it.🤩 😃 🙌

Note:

A. Use swift framework in Objective C project

  • When you run framework in Objective C project it cause crush:
  • To resolve it, goto Build Settings > Search ‘swift’ > Select ‘Yes’ option for ‘Always Embed Swift Standard Libraries’
  • To access a swift class in objc, that is not inherited from NSObject you need to @objc public class And you need to add @objc with public class method.

--

--

Rageeni Jadam

iOS Lead @ Deloitte A seasoned Software Engineer with 8+ years of experience in iOS app development, skilled in both native and cross platform frameworks.