iOS App Development: Publishing a CocoaPods library with Swift that turns pictures to grayscale.
Tags: ios, swift, tutorial, beginners
Published: 2021-08-11 14:01:19

#Step 1: Install CocoaPods on your Mac
In order to create a CocoaPods, you must first install CocoaPods on your Mac. To install CocoaPods on your Mac, you need to open the Terminal application and run the following command.

{% gist https://gist.github.com/Murmeko/83686399e83e9eda0fb3c4762231b42c %}

When you run the command, Terminal will ask you to type your password, after entering your password CocoaPods will be installed on your Mac.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/teodstg6q59z4au9op4n.png)



#Step 2: Creating the pod
After installing CocoaPods on your Mac, use the "cd" command to change to the directory where you want to create the Pod using Terminal. For me the command is as follows.

{% gist https://gist.github.com/Murmeko/58aea44f8f1e6b9568076d2966678cd9 %}

After moving to the directory where you want to create your Pod, create the Pod by replacing the "POD NAME" part of the command below with the name of your Pod. Also, make sure that there are no spaces in the name of your Pod.

{% gist https://gist.github.com/Murmeko/299330cf0ab2b767fd7d99b87d529b3e %}

After running the command, Terminal will ask you the following questions about your Pod.

{% gist https://gist.github.com/Murmeko/a0e7c34462db350db8bd45ce105d88c2 %}

In the first of the questions, you are asked on which platform you will use your Pod, write “iOS” and continue. In the second, you are asked in which language you will write your Pod, write "Swift" and continue. In the third question, you are asked whether to include a demo application with your Pod, write “Yes” and continue. In the fourth question, which frameworks you will use to test your Pod, write “None” and continue. In the fifth question, you are asked whether you want to do View-based testing, write “No” and continue. If you followed the steps correctly, an Xcode window will open when the command finishes running.

#Step 3: Signing in to Xcode with GitHub
If you are already logged into your Xcode with GitHub, you can skip this step and go to Step 4.

To sign in to Xcode with GitHub, you must first create a GitHub account. After opening a GitHub account, you should click the profile icon at the top right, click the "Settings" button from the drop-down list, and click the "Developer settings" button in the list on the left of the page you are directed to.

![Alt Text](https://media.giphy.com/media/gO366xH1ob5E6aoVwd/giphy.gif)

After pressing the button, you should press the "Personal access tokens" button on the left of the page you are directed to, and when the page is updated, you should press the "Generate new token" button, which is the 2nd button from the top right.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a4i9wlf60b3s992wyybg.gif)

In the Note section of the new page that opens, you must enter the name of the Token you want to create, I briefly write "Xcode". In the Expiration section, we are asked to specify the expiration date of your Token, we can choose the "No expiration" option because we will use it in Xcode. In the Select scopes section, you specify the access permissions of your Token. Here you need to tick “repo”, “admin:repo_hook”, “delete_repo” respectively. After checking the boxes, we press the "Generate token" button at the bottom and our token is created.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahsz4jt7ilyorcqlo3xr.gif)

Copy your password in the green box on the page we are directed to and return to Xcode. After returning to Xcode, click the Xcode tab in the Menu Bar and click the "Preferences" button. Click the “Accounts” button at the top of the window that opens, and then click the “+” button at the bottom left. Select GitHub from the list in the window that opens and click the “Continue” button. In the renewed window, paste your e-mail address that you used to open your GitHub account in the “Account” section, and your Token password that you copied from the green box in the “Token” section and press the “Sign In” button. If you followed the guide correctly, congratulations, you have integrated your GitHub account into your Xcode.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddch33325l1ts8c5fwkp.gif)





#Step 4: Updating our Pod's settings and importing our Pod to GitHub
After creating your pod, you'll see a yellow warning sign appear to the left of the top middle bar of your Xcode. When you click on the sign, the Navigator part of your Xcode will switch to the Issue Navigator tab.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/is6q6abutc11zp3q9mkf.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29ojcyuyf8jnkxdmuier.png)

In the first warning in this section, Xcode tells us that it can convert your project's code to Swift 5 code. Click on the warning and convert your code of “Next, Next, Next” method to Swift 5 code. In the second and third warnings, Xcode is telling you that it can update the settings of your Pod and our example project to the recommended settings. Again, update the settings of your project with the “Next, Next, Next” method. The thing to keep in mind here is that Xcode is upgrading the minimum iOS version your sample project and Pod can run on from 9.0 to 12.0.

After all the warnings disappear, click on the "Source Control Navigator" button, which is the second of the horizontal buttons at the top of the Navigator section on the left. There are 4 folders here, right click on the "Remotes" folder, which is the 3rd folder from the top, and click on the "New 'POD NAME' remote" button. When you click the "Create" button at the bottom right of the window that opens, your Pod will be transferred to your GitHub.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gyzd8kypj2l97rduaoaj.gif)

After transferring your code to GitHub, click the "Project Navigator" button, which is the 1st from the left among the buttons at the top of the Navigator section of Xcode. Project Navigator lists the files that make up our project. Here, click on the arrows to the left of the files named “POD NAME” and Pods if they are minimized and expand your files. Then expand the "Podspec Metadata" folder, which is the 1st one from the top. Click on the file named POD NAME.podspec in the expanding folder. This file acts as the identity of your Pod. Here, you are expected to enter a brief summary of your Pod in the "s.summary" section, and a description of your Pod in the "s.description" section. I am writing the following for my own Pod, respectively.

{% gist https://gist.github.com/Murmeko/44a876fad75efac11746ae4b0fb023b6 %}

After writing your summary and explanation, you need to update the minimum iOS version that your Pod can be used in from the "s.ios.deployment_target" section and specify the Swift version we used while writing your library. To specify the iOS version you used while writing our library, you should add "s.swift_version = '5.4'" under the "s.ios.deployment_target" section. At the end of all changes, your 'POD NAME'.podspec file should look like the one below.

{% gist https://gist.github.com/Murmeko/43fb5801efbe1c1131ef96eb43ef34e7 %}

After updating your 'POD NAME'.podspec file, open the "README.md" file in the folder where your file is located. Add " :git => 'https://github.com/Murmeko/POD NAME.git'" line under the "pod 'POD NAME'" line under the "## Installation" section here.

After you have done this, you finally need to update the “.podfile” file of your sample project. For this, open the "Podfile" file in Project Navigator and replace the "platform :ios, '9.0'" part with 12.0.

Now that we have everything set up, it's time to make our first Commit. For this, we click on the Source Control tab in the Menu Bar, click on the “Commit…” button from the drop-down list. We tick all the files that appear on the left of the window that opens, and write the name of our commit in the "Enter commit message here" box at the bottom. I will write “Updated project settings” as the name. After clicking the "Push to remote:" box at the bottom left, we click the "Commit Files" button at the bottom right and our files on GitHub are updated.





#Step 5: Creating your library
After updating your code on GitHub, expand the "Development Pods" folder from the Project Navigator section, and then expand the "POD NAME" folder in the same folder. Update the file named "ReplaceMe.swift" that appears here as "POD NAME.swift" and open the file. You will write your code here.

First, import the Frameworks you will use while writing your code.

{% gist https://gist.github.com/Murmeko/fb024e34c07d242318379b0f82b66ef0 %}

Next, create a class. The point you should pay attention to here is that you must define the class as " public ", otherwise you cannot use your library.

{% gist https://gist.github.com/Murmeko/b964b10e5a86789190eb45daa2887d5b %}

Then, define the function that will turn your images into black and white in this class that you have defined, so that it takes a “UIImage” and returns a “UIImage”. Also, remember that when defining the function, it must be "public".

{% gist https://gist.github.com/Murmeko/ad8f2c7062b417dea7ff35ca0b286e08 %}

Define the properties of the UIImage you will draw in your function. Here, make sure that the new UIImage you are going to draw has its dimensions from the original, otherwise the aspect ratio of the new image will change. Also, make sure that the color space of the new UIImage you will draw is "CGColorSpaceCreateDeviceGray".

{% gist https://gist.github.com/Murmeko/a4b043aff78ba20588add76069df0cd5 %}

Then draw your UIImage using all these definitions.

{% gist https://gist.github.com/Murmeko/a4b043aff78ba20588add76069df0cd5 %}

Finally, you must define a “public init” for the class you originally created. Otherwise, you won't be able to use your class in the project where you added your Pod.

{% gist https://gist.github.com/Murmeko/ac3828e9825a4e00346e2947dbe5ae9e %}

Your library is ready! If you want, you can also add a sample application to the "Example" folder in the main folder of the project. You can use your library in your projects as follows.

{% gist https://gist.github.com/Murmeko/956d30344c13a20c6f76b09e86963109 %}





#Step 6: Publish the Pod
After creating your library, commit your project one last time. Then open the Terminal application and go to the directory where your project is located with the “cd” command. My directory is “Documents/Developer/GIFFromImages“. Then run the “pod lib lint” command in this directory. This script validates your files, if it successfully validates it will give the following output.

{% gist https://gist.github.com/Murmeko/89e6961a14f05aca3849794598546e2a %}

Then you need to register CocoaPods by running the “pod trunk register” command. Here, you should replace the "address@e-mail.com" part with your e-mail address, the "Your Name" part with your name, and the " — description='Name of your Mac'" part with the name of your Mac.

{% gist https://gist.github.com/Murmeko/f83a2e5adc79e3fafd145ab62be51ebc %}

Once you've done this, open your project's GitHub page and click the "Releases" button on the right. Then click on the “Draft a new release” button on the top right of the page we are directed to.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3p1wlkbqo33fe8glg4e.gif)

Type 0.1.0 in the “Tag version” section on the page you are directed to. This is the part where you specify the version of your Pod. As you update your pod, you have to update the .podspec file and the releases section on GitHub. Then write “Release 0.1.0” in the “Release title” section on this page. Actually, you can write whatever you want here, but I will write it like this to avoid confusion. Then click on the "Publish release" button at the bottom left of the page, so you publish the 0.1.0 version of your Pod on GitHub.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ndep90ln0ezmcvgadm7.gif)

After that, go back to Terminal and run the command " pod trunk push NAME.podspec ". And congratulations, your Pod is officially live!

For projects GitHub page: {% github Murmeko/GrayscaleImages %}