Step 2: Change the build configuration to release mode of your framework
- Select your framework target and Navigate to Product->Scheme->Edit Scheme
- You can see the Build configuration is set to Debug. It means that whenever you run this project it will create our framework in Debug mode so if we want to use this framework in our application we switch to Release mode.
- Now if we run this project it will be ready for release
Step 4: Create our first class in swift file
- Create class named YLog. This can actually be used inside our framework project however we want but we want to use this class outside the framework so we have to mark this class as public.
- Write following code to your YLog.swift file:
//1. private var isDebug: Bool! //2. public init() { self.isDebug = false } //3. public func setup(isDebug: Bool) { self.isDebug = isDebug print("Project is in Debug mode: \(isDebug)") } //4. public func YPrint<T>(value: T) { if self.isDebug == true { print(value) } else { //Do any stuff for production mode } }
- The purpose of this YLog class is going to be if our application is in Debug mode we want to printing out debugger but our application is in production for presenting to appStore we don’t want to print this debugger because we can’t see them and also slow down the application performance.
- Create a variable named isDebug so we manage application is in debug or in production mode.
- We should add initialiser to our class. So in init() method we initialise debug variable and set it to false because just incase if you forgot to set your app to debug mode, the Logging framework automatically stop printing debugger. We actually need this init() method to set as public so we can initialise our variable from outside the framework.
- The setup() method is used to set the application mode, set to true means application in Debug mode and false means in Production mode.
- Now we create actual logging method called YPrint and this is going to be a generic. In this method if isDebug is set to true, we print out the value in debugger otherwise not because out app in production mode and you can do any stuff in else part.
Step 7: Add framework in our application
- Create a folder called as Frameworks in your project directory and drag your framework from desktop into this folder
- Now select your targets and click on + button under the Embedded Binaries tab.
- Click On Add other and navigate to your project directory and select your framework from Frameworks folder
Step 8: Import your framework and Setup debugging mode
- Open AppDelegate.swift file and at top write import YLogging.
- Create global variable so we can use this logging framework anywhere in our application.
let logger = YLog() - Call the setup() method of framework from didFinishlaunchingWithOptions method and set isDebug to true.
logger.setup(isDebug: true) - So Now your AppDelegate.swift file looks like below:
Step 9: Use of YPrint method
Distributing your framework using CocoaPods
CocoaPods manages library dependencies for your Xcode projects. The dependencies for your projects are specified in a single text file called a Podfile. CocoaPods will resolve dependencies between libraries, fetch the resulting source code, then link it together in an Xcode workspace to build your project. Ultimately the goal is to improve discoverability of, and engagement in, third party open-source libraries by creating a more centralised ecosystem.
Here we are going to make our framework into a pod, so we are distributing the code, resolving the dependencies, including and building the framework source, and easily sharing it with our organization.
Step 1: Remove current framework from your project
- Select YLogging.framework from the project navigator and delete it.
- Choose Move To Trash to permanently remove framework from your project.
Step 2: Install CocoaPods
- If you are not familiar with CocoaPods before, you’ll need to follow a brief installation process using CocoaPods Installation Guide before going any further.
Step 3: Create the Pod of your framework
- Open terminal in YLogging project directory and run following command:
pod spec create YLogging - This command creates the file YLogging.podspec in current directory. Right click on this file and open in text editor.
- Remove all the text from that file and paste following lines for commonly use settings for pod configuration:
Pod::Spec.new do |s| #1. s.name = "YLogging" #2. s.version = "1.0.0" #3. s.summary = "Sort description of 'YLogging' framework" #4. s.homepage = "http://yudiz.com" #5. s.license = "MIT" #6. s.author = "AKanjariya" #7. s.platform = :ios, "10.0" #8. s.source = { :git => "URL", :tag => "1.0.0" } #9. s.source_files = "YLogging", "YLogging/**/*.{h,m,swift}" end
- Save the file and now you have a workable development Podspec.
- Specify the name of pod
- Version of pod
- Short description of your pod
- Point to a project page for the framework.
- Specify this iOS frameworks tutorial code using an MIT License
- Name of that person credited and contacted for this framework.
- Specify the platform which can use this framework.
- Setup the URL for link to the GitHub repository. When you’re ready to share the pod, replace URL with your GitHub repository url, so this will be a link to the GitHub repository and the commit tag for this version.
- Specify the source files folder and types of files which you want to add into pod.
Step 4: Use pod into your application
- Now our pod is ready to use. Test it out by implementing in the FrameworkTest app.
- Go to terminal and navigate to the FrameworkTest directory and run following command
pod init - This will create a pod file into current directory. Open That file into text editor and add the following line under the comment, # Pods for FrameworkTest:
pod ‘YLogging’, :path => ‘../YLogging’ - Save this file and run following command in terminal:
pod install - With this command, you’re searching the CocoaPods repository and downloading any new or updated pods that match the Podfile criteria. and it will create a FrameworkTest.xcworkspace file. Use this file from now on instead of the .xcodeproj file because it has the reference to the Pods project and the app project.
- Open FrameworkTest.xcworkspace and run the project.
Step 5: Create GitHub Repository
- Here you can publish your pod on GitHub and use like a third party framework.
- If you don’t already have a GitHub account, create one.
- Create a new repository to host the pod. Specify the name of repository here YLogging, you can name it whatever you want. Select Swift as .gitignore language and MIT as licence.
- Click on Create Repository. From the dashboard page of your repository copy the HTTPS link.
Step 6: Add your framework to GitHub
- Open your YLogging.podspec file from YLogging folder and update the s.source line. Replace URL with your repository link copied from GitHub dashboard
- Go to terminal and navigate to YLogging folder and run following commands:
git init git remote add origin GitHub_URL git fetch --all git pull origin master git add . git commit -m "Initial commit" git push -u origin master
- You need to tag the repository so it matches the Podspec. Run this command to set the tags:
git tag 1.0.0 git push --tags
- Now your framework is on your GitHub repository. Go to the repository on browser and refresh to see changes:
Step 7: Update your podfile.
- Update your podfile in the FrameworkTest directory. Replace the existing pod line with:
pod ‘YLogging’, :git => ‘URL’, :tag => ‘1.0.0’ - Replace URL with your GitHub Link.
- Go to terminal and navigate to FrameworkTest directory and run following command to update your pod file:
pod update - Now the code will be downloaded the framework from the GitHub repository.