In iOS 8 and Xcode 6, Apple provided a new template, Cocoa Touch Framework for creating framework. As you’ll see, it makes creating custom frameworks much easier than before.

By using Cocoa pods we can pod file for framework in iOS Swift.

Creating a Framework

The first thing to do is to create the project for the framework

  1. Create a new project. In Xcode, go to File/New/Project.
  2. Choose iOS/Framework & Library/Cocoa Touch Framework to create a new framework.iOS App developoment,Mobile app development
  3. Click Next
  4. Set the Product Name to MyFramework. Use your own Organization Name and Organization Identifier. Check Include Unit Tests. That’s right! You’re going to have automated tests ensure your framework is bug free.

Mobile app development

5.Click Next and select the path where you want to save your framework and Check Create git repository on which will create local repository for project and useful to push your code to github.

Mobile app development

6. Click Create

Now you have a project that creates a framework.Now click on cmd+B you will get build success

Add Classes to Framework

  1. Create a new project. In Xcode, go to File/New/File
  2. Choose Cocoa Touch Class to create a new framework.

Mobile app development

3.Click Next

4.Add what type file you want to add

5. Click Next and then Click Create

6. Open .swift files and write your own code.

7.By default, Swift makes everything internal or visible only within its own module.By putting Public in front of classes and methods, we can make them public visibility otherwise these won’t come to other projects when you add this framework to your projects

8 Build the project you will get build success message, that means your framework is ready.

 

Adding framework to other projects

We can add frameworks to other projects by simply drag and drop of .xcodeproj file of framework to your project.Whenever you updated the framework and to get changes into your project you will have to do same process again and again, this will become complex or time consuming, for this purpose we can use CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects

Mobile app development

 

Here ZZZ is your project,  YYYYYYYYY and xxx are the frameworks (dragged into your project), to access framework classes you have to import .h files of framework into your classes.

Pod File for Framework

If you haven’t installed CocoaPods on your system yet just Click Here to install it.

Now open terminal and change the directory path to your frame work path and type the following commands

pod spec create MyFramework

This  will create MyFramework.podspec file in your framework folder.

Open the .podspec file in text editor, you can see lot of comments, just delete all and add the following tex

Pod::Spec.new do |s|
 s.name = "MyFramework"
 s.version = "1.0"
 s.summary = "the summery of your framework"

s.homepage = "http://www.yourdomain.com"
 s.license = { :type => "MIT", :file => "LICENSE" }
 s.author = { "your name" => "your email" }
 s.social_media_url = "http://twitter.com/id"
 s.platform = :ios
 s.ios.deployment_target = "8.0"
 s.source = { :git => "github link of your framework", :tag => s.version }
 s.source_files = "MyFramework/*.swift"
 
 s.requires_arc = true

end

Use the Pod of Framework

Create new Xcode project

Open terminal and change directory to your new Xcode project and type following command

pod init

this will create pod file on your project and open pod file in text editor and type the following text

platform :ios, '8.0'
 use_frameworks!
 target 'your project' do
 pod 'MyFramework', :git => 'your framework github url'
 end

Then back to terminal and type the following command

pod install

The above command will install/add your framework to your Xcode project.

Author: venkat

Lead software engineer for mobile(iOS, Android) app development teams & web development teams