قالب وردپرس درنا توس
Home / IOS Development / How to create pods using CocoaPods and publish them on GitHub

How to create pods using CocoaPods and publish them on GitHub



Welcome back! In the previous tutorial, you learned how to use the network framework to detect and monitor network status. The frames seem to work fine through the NetStatus class, so let's go one step further and let's make our own small open-source frames that will be based on the NetStatus class . In this tutorial, we will make this framework easily distributable and integrable by making a pod using CocoaPods, and we will even push it to GitHub.

Reuse NetStatus that way in several projects is going to be just like a walk in the park. Also, the "recipe" that comes in the following sections can be followed many times for any custom framework you want to create in the future.

Note: If you have not checked the previous tutorial, I advise you to take a look first. If you just want to learn how to make a pod, you can download this project first.

Createing Framework

OK, let's get started with creating a distributable library.

To begin, open File> New> Project … in Xcode to start creating a new project. In the first step, select Cocoa Touch Framework as the template option:

  Cocoapods Tutorial - New Project

Then enter the correct Team, Organization Name and Organizational Identifier values, and use a product name of our class: NetStatus .

  t66_4_product_name

Note: You are free to enter a product name for the frame we are going to create. I use NetStatus, and from now on NetStatus means the custom framework we create, not the class unless explicitly mentioned.

Finally, find a folder to save the new project. I would recommend creating a subfolder named NetStatus in the same root folder where NetStatusDemo project exists and stores it there.

Once the project is created, you will notice that not many files are contained in it. Most times you do not need to make any changes, except perhaps to change the version number or distribution target:

  t66_6_target_settings

When you talk about it, change Deployment Target to 12.0 so we can use this frame on devices running iOS 12.0 and later.

Generally, it is considered a good idea to create a group to add the source files and separate them from the rest project content. Therefore, right-click the NetStatus root group and create a new named Source .

Now switch back to the NetStatusDemo project, and right-click the file NetStatus.swift . From the context menu, select the View option in the Finder .

When NetStatus.swift file appears on the Finder, include NetStatus the Xcode project in front again and drag and drop NetStatus.swift into the source group of Xcode . After dragging, make sure to check Copy Items if needed and that NetStatus is selected as well.

  t66_8_add_source

Is Framework Working?

The steps above are what we needed to create our own small frames based on the class NetStatus . But does it really work? We will confirm that then close the NetStatus project we just created. Then, in the NetStatusDo project, select and delete the NetStatus.swift file from the Project Navigator. When asked, move it to garbage.

  t66_9_delete_original_source

Now, go to the Finder and find NetStatus Xcode Project ( NetStatus.xcodeproj container). Drag and drop it to NetStatusDemo .

Note: Once again, make sure you have closed the NetStatus project before dragging it into NetStatusDemo.

  t66_10_add_framework

Remember that if you want to delete the project you just pulled from the NetStatusDemo project, you should remove the references only. Do not move to the Trash or delete NetStatus the frame files.

Open the NetStatusDemo target and go to General tab. Click plus (+) button in Embedded Binaries and select NetStatus.framework from the dialog that appears. The selected framework will also be displayed in Linked Frameworks and Binaries .

  t66_11_link_framework

Build the project once by pressing Cmd + B on your keyboard, or through Product> Build menu in Xcode. When the building is finished, open the ViewController.swift file and import NetStatus .

  t66_12_import_framework

Build again. You will see an error appear several times! Why is it happening?

  t66_13_error_message_1

It has everything to do with the access control and standard access levels that the modules have in Swift! The NetStatus class has internal access level set by default, making it visible only in its own module, the NetStatus frame. NetStatusDemo is not able to "see" it right now, so let's fix it.

Expand NetStatus.xcodeproj and then expand NetStatus and Source groups thereafter. You will find the NetStatus.swift file there. As a side note, the recommended way to implement a custom framework is by incorporating it into a demo project and discovering such issues as you go. As an added tip, changes made to embedded projects are not immediately visible to other modules, so build each time you want to make some changes visible to the demo project.

Now, open NetStatus.swift archives and update the class definition as follows:

Rebuild and open ViewController.swift file. We still have errors, but this time the message is different:

  t66_14_error_message_2

The error is clearer this time, and again we face access level limitations. The presence of shared cannot be accessed as it is specified as internal by default. To make it a little more clear, in the NetStatus.swift file, we define the shared instance as follows:

This is similar to:

Internal level is derived, and it is not necessary to explicitly write it. If we now want to highlight the shared occurrence of NetStatusDemo we just need to change the above to this:

Build again and you will see that now is not the same error repeated many times, but more errors of the same kind emerging. The default access level prevents NetStatusDemo from accessing the properties and methods we implemented in the NetStatus class.

  t66_15_multiple_errors

Let's fix everything at once now, then add public keywords to all properties except monitor :

Do the same for start Monitoring () and stop Monitoring () methods too:

You should see a message that says "Specification created on NetStatus. podspec and a new file called NetStatus.podspec appears in the Finder.

Open the file in the favorite editor or just in Xcode. Take a look at what the .podspec file contains if it is your first time with such a file. At the end, you can either edit it or replace the content with the following: