Try creating an iOS library with CocoaPods

Introduction

In my main business, I mainly do web application development, mainly on the back end side and infrastructure (a little). The front end side has touched js (Vue.js) and CSS (a little) for a while, but I had no experience in developing native apps.

Recently, when the system in charge of the shared authentication system of the company's service tried to introduce social login such as Facebook, according to Facebook's rules, it is necessary to use Facebook's dedicated SDK natively, so the native side Implementation is required. Also, I'm currently providing multiple services with a shared system, and there are native apps for each service, and if implemented separately, it would be inefficient and difficult to maintain, so a shared library ( I decided to make an SDK).

I had no experience developing iOS native apps, but I tried to challenge while learning. (Of course, we had the support and flow from those who have experience with native apps.)

This time I would like to write about that experience.

Preparation

The environment you were using

knowledge

--Overview of Swift (basic knowledge)

Language is the most important factor in creating a library. So, let's study the basic knowledge of Swift language first!

To study Swift, I used the official Swift website. Specifically, study from Swift Tour and Language Guide.

--Developing iOS native apps

In order to create a library for iOS native apps, you also need to learn about iOS native app development.

You don't have to learn from scratch to develop an iOS app that can be published to the Apple Store, but you should learn the basics of developing an iOS app. Below are the official Apple tutorials and free udacity courses (both in English).

pod development

The outline is as follows.

  1. Create and configure a project in Xcode
  2. Implement the pod
  3. Works with git
  4. Incorporate into the app

Create and configure a project in Xcode

--Create a new project

Open Xcode and create a new project. I want to create a CocoaPods library, so select Framework.

スクリーンショット 2020-12-22 0.55.26.png

Enter the MySwiftLib product name and check the" Include Tests "checkbox for unit tests

スクリーンショット 2020-12-22 10.57.41.png

--Add a demo app to show how to use the library

Select the template File> New> Target," App "from the Xcode menu

スクリーンショット 2020-12-22 10.38.54.png

Enter the product name MySwiftLibExamples and uncheck the" Include Tests "checkbox as the demo app does not require unit tests.

スクリーンショット 2020-12-22 10.58.29.png

The structure of the project is as follows.

スクリーンショット 2020-12-22 11.06.23.png

Implement pod

Create a podspec file

The podspec file is where the version, source location, meter information, etc. are defined.

A simple podspec looks like this:

First, create a pod spec in the project directory with the following command.

$ pod spec create MySwiftLib

MySwiftLib.podspec


Pod::Spec.new do |spec|

  # ―――  Spec Metadata  ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  spec.name         = "MySwiftLib"
  spec.version      = "0.0.1"
  spec.summary      = "My First CocoaPods Library written in Swift"
  spec.homepage     = "https://github/username/MySwiftLib"

  # ―――  Spec License  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  spec.license      = { :type => "MIT" }

  # ――― Author Metadata  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  spec.author             = { "trandoantan" => "[email protected]" }

  # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  spec.platform     = :ios

  # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  spec.source       = { :git => "https://github/username/MySwiftLib.git", :tag => "#{spec.version}" }

  # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  spec.source_files  = ["MySwiftLib/**/*.swift", "MySwiftLib/MySwiftLib.h"]

  # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  # spec.dependency "FacebookCore"
  # spec.dependency "FacebookLogin"
end

--Metadata (** required ): Pod name, version, summary, home page --License ( required ): For private pods, you only need to specify the license type. --Author information ( required ) --platform ( optional ): Available platforms --source ( required ): Specify the git repository and specify the tag for versioning (specify using the git tag when a new version is released) --source_files ( required **): Source code files included in the library. In the above example header files and all Swift files --dependency: If you want to use an external package in the library, specify this

Library implementation

Add a new Swift file to MySwiftLib.

スクリーンショット 2020-12-22 19.24.16.png

スクリーンショット 2020-12-22 19.21.47.png

MySwiftLib.swift



enum Language: String {
    case japanese = "ja"
    case english = "en"
    
    var greetingTemplate: String {
        switch self {
        case .japanese:
            return "Hello,%@!"
        case .english:
            return "Hello, %@!"
        }
    }
}

public class MySwiftLib {
    var defaultLang: Language
    
    public init(lang: String) {
        if let language = Language.init(rawValue: lang) {
            defaultLang = language
        } else {
            defaultLang = .english
        }
    }
    
    public func greetingMessage(userName: String) -> String {
        return String(format: defaultLang.greetingTemplate, userName)
    }
}

It's a sample, so it's very easy.

Write unit tests

Next, let's write a unit test for the above MySwiftLib.

--Edit the MySwiftLibTest.swift file in MySwiftLibTests as follows.

MySwiftLibTest.swift


import XCTest
@testable import MySwiftLib

class MySwiftLibTests: XCTestCase {
    func testGreetingMessageEnglish() {
        let mySwiftLib = MySwiftLib(lang: "en")
        XCTAssertEqual(mySwiftLib.greetingMessage(userName: "John"), "Hello, John!")
    }
    
    func testGreetingMessageJapanese() {
        let mySwiftLib = MySwiftLib(lang: "ja")
        XCTAssertEqual(mySwiftLib.greetingMessage(userName: "Alice"), "Hello, Alice!")
    }
    
    func testGreetingMessageDefaultEnglish() {
        let mySwiftLib = MySwiftLib(lang: "hogehoge")
        XCTAssertEqual(mySwiftLib.greetingMessage(userName: "John"), "Hello, John!")
    }
}

--Run unit tests

スクリーンショット 2020-12-22 19.34.44.png

Examples implementation

Next, we will implement MySwiftLibExamples for demonstration purposes.

--Add Label to Main.storyboard and edit ViewController as follows.

スクリーンショット 2020-12-22 19.40.19.png

ViewController.swift


import UIKit
import MySwiftLib

class ViewController: UIViewController {
    //MARK: Properties
    @IBOutlet weak var greeting: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let mySwiftLib = MySwiftLib(lang: "ja")
        self.greeting.text = mySwiftLib.greetingMessage(userName: "Tan")
    }
}

--When I build MySwiftLibExamples, it looks like the following. : tada :: tada :: tada:

スクリーンショット 2020-12-22 19.46.28.png

Works with git

--Create a MySwiftLib repository on github --Initialize git in the project directory and push the code to the repository you created

$ cd /your/project/path
$ git remote add origin [email protected]:username/repo-name.git
$ git add .
$ git commit -m "initial commit"
$ git push origin master

-Update podspec with the repository you created (if needed)

MySwiftLib.podspec


  spec.homepage     = "https://github.com/username/repo-name"
  spec.source       = { :git => "https://github.com/username/repo-name.git", :tag => "#{spec.version}" }

--Tag and push the latest code

$ git tag 0.0.1
$ git push origin --tags master

This is a pod for personal use, so I will not publish the pod. If you want to publish the pod, please refer to here.

Introduced to the app

--To install the pod in your app, add the following code to the Podfile

    # for https: pod 'MySwiftLib', :git => 'https://github.com/username/repo-name.git'
    pod 'MySwiftLib', :git => '[email protected]:username/repo-name.git'

Since this pod will not be published, specify the pod with git

** If you use git during development, it is inconvenient to reflect the update with the install/update command every time, so if you put the pod in the same directory of the installation destination application, the pod with the following settings will be used. Can be specified, and the update will be reflected immediately. ** **

   # for https: pod 'MySwiftLib', :git => 'https://github.com/username/repo-name.git'
   # pod 'MySwiftLib', :git => '[email protected]:username/repo-name.git'
   pod 'MySwiftLib', :path => '../MySwiftLib'

--Execute the following command to install the pod in the app

$ pod install
#In case of update
$ pod update MySwiftLib

After installation, you can use it by importing the module, like MySwiftLibExamples.

the end

This time I introduced how to create a simple pod library using Cocoapod, but I am still new to iOS application development, so if you have any mistakes in this article, I would appreciate it if you could point out. ..

Thank you for reading!

reference

Recommended Posts

Try creating an iOS library with CocoaPods
About creating an application with springboot
Try running an app made with Quarkus on Heroku
Try DI with Micronaut
Try create with Trailblazer
Try WebSocket with jooby
Try WildFly with Docker
Become an iOS engineer
CarPlay starting with iOS14
Getting started with Java and creating an AsciiDoc editor with JavaFX
Rough procedure verbalized output when creating an app with Rails
Summary of initial work when creating an app with Rails
How to specify db when creating an app with rails
[swift5] Try to make an API client with various methods
Building an environment for creating apps with Rails and Vue