iOS App Development: Publishing a CocoaPods library that converts images to GIFs with Swift.
Published: 2021-08-11 14:12:08

#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, and after entering your password, CocoaPods will be installed on your Mac.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmlcckf4njhvsnta6lla.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 question, you are asked on which platform you will use your Pod, we write “iOS” and continue. In the second, you are asked in which language you will write your Pod, we write "Swift" and continue. In the third question, you are asked whether to include a demo application with your Pod, we write “Yes” and continue. In the fourth question, which frameworks you will use to test your Pod, we write “None” and continue. In the fifth question, you are asked whether you want to do View-based testing, we 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/2x2lkbxnxdkwax6aor66.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/v0hn19mvzzx79z3rztkw.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/djmbt6rmsp5pumlzm2yg.gif)

#Step 4: Update your pod's settings and export to GitHub
After creating your pod, you can 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/v3n7nbxtfwssxfuf0f7k.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5lfpkbju3bl6lsg0yep.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/qug4phiegj80jei5y7au.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/51b8c59e83efec44afb21c503e8ab693 %}

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/76d5a1049cb37f6b080c33e17498aa91 %}

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 you have everything set up, it's time to make your first Commit. For this, click on the Source Control tab in the Menu Bar and click on the “Commit…” button in the drop-down list. Click on all the files that appear on the left of the window that opens and type the name of your commit in the "Enter commit message here" box at the bottom. I will write “Updated project settings” as the name. Finally, click the "Push to remote:" box at the bottom left and click the "Commit Files" button at the bottom right, your files on GitHub have been 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/a002b8a87695d98226935c2cc6861879 %}

Next, define 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/35ff326a004ffae61d1e7a7f6d71458e %}

After defining your class, define an enum named “ colorSpace ” inside this class.

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

Then, inside the class you define, the function that will convert your images to GIF is a "UIImage" string, a "URL" that indicates where the file will be located, a "colorSpace" that will specify the color space of your GIF, an "Int" value that indicates the time between the photos that make up your GIF, and Define it to take an “Int” value that specifies how many times your GIF will repeat, and give it a “UIImage” string at the end. Also remember that when defining the function and enum they must be " public ".

{% gist https://gist.github.com/Murmeko/7ff0e58128dd0aea35cda3829e73a2f9 %}

Next, define another function that will take a “String” into your function and return the URL of your GIF.

{% gist https://gist.github.com/Murmeko/419fffd7e41bdb97677f74e383182556 %}

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/6618bdba0f77f570fce8c816e496a4dc %}

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/65e67192e68bab260a7cfaaca8fb8713 %}

#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/c3b59e910665135a91680910257b147e %}

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/cx4p16gv5fiazdze0pc9.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/ajlkozwbe1pzcqcu9c3e.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/GIFFromImages %}