본문 바로가기

iOS/Swift

create module with private cocoapods

priavte cocoapods로 module 만들어 배포하기.

업무상 만들어둔 프로젝트의 일부 기능을 모듈로 만들어 재사용 및 외부에 배포해야 하는 일이 생겨 갖은 검색 끝에 private cocoapods와 private github을 통해 배포하는 방법을 찾았다.

 

public cocoapods는 cocoapods를 통해 관리 및 배포되지만

private cocoapods는 git을 통해 버전 관리 및 배포한다.

Spec repository는 버전 관리를 위한 용도이고 Pod repository는 코드를 작성해서 git에 업로드 해둔다.

pod를 사용할때는 spec repo에만 접근 가능하고 실제 pod repo를 노출하지 않는다

 

1. github에 Spec Repo와 Pod Repo 두개의 repository 생성.

두 repo 모두 private으로 생성했으며 

Spec repo는 README 추가,

Pod repo는 README를 추가하지 않았다.

 

 

2. local에 Spec pod repo 추가하기

$ pod repo add REPO_NAME SOURCE_URL

pod repo add REPO_NAME SOURCE_URL

REPO_NAME : pod repository 이름

SOURCE_URL : github에 생성한 spec repo 링크

ex)

$ pod repo add specs https://github.com/username/Specs.git
Cloning spec repo 'specs' from 'https://github.com/username/Specs.git'

* spec repo 확인하기

~/.cocoapods/repos 경로 아래에 추가한 REPO_NAME 이름으로 폴더가 추가 되어있는지 확인

추가한 REPO_NAME의 폴더로 이동하여 pod repo lint . 실행시 아래와 같이 나오면 정상적으로 spec repo가 추가된것임.

$ cd ~/.cocoapods/repos
$ ls
specs

$ cd ~/.cocoapods/repos/specs
$ pod repo lint .


Linting spec repo 'specs'

Anlayzed 0 podspecs files.

All the specs passed validation.

 

3. pod library 만들기

프로젝트 파일을 관리할 경로로 이동

주로 문서폴더 아래에 xcode 프로젝트를 생성해서 해당 위치로 이동했음.

$ pod lib create LIB_NAME

pod lib create LIB_NAME

LIB_NAME : 생성할 library 이름, 배포시 pod 프로젝트 명이 된다. pod file에 작성하는 pod 프로젝트명

github pod repo와 같은 이름으로 생성하였다.

ex ) 

$ cd
$ cd documents/ios
$ pod lib create TestLib

pod lib 생성시 5가지 항목에 대해 입력해야 한다.

1. What platform do you want to use? [ iOS / macOS ] : 만들려는 플랫폼 선택

2. What language do you want to use? [Swift / ObjC ] : 개발 언어 선택

3. Would you like to include a demo application with your library? [ Yes / No ] : 해당 모듈을 테스트할 데모앱 함께만들지 여부

* yes 선택시 Example 폴더 아래에 해당 프로젝트의 이름의 폴더가 추가 생성되고 해당 폴더 안에는 viewController등이 있음.

(left) include a demo application vs (right) not include a demo application

 

4. Which testing frameworks will you use? [ Quick / None ] : 테스트를 위한 프레임워크를 사용할지 선택

5. Would you like to do view based testing? [ Yes / No ] : yes 입력시 FBSnapshotTestCase를 포함해서 Project 생성

 

4. Library로 배포할 파일 또는 코드 작성.

TestLib > TestLib > Classes 에 위치한 ReplaceMe.swift에 코드를 작성하거나 해당 경로에 새로운 파일을 추가해야 cocoapods로 배포된다.

단순히 동작을 테스트 해보기 위해 함수를 작성한 Utils 클래스와 Test ViewController를 작성하였다.

public 또는 open으로 작성해야 해당 libaray pod를 참고한 프로젝트에서 사용할 수 있다.

 

5. podspec 수정

private pod의 버전 과 github에 대한 정보들을 작성해준다.

s.source에 처음에 만들어준 github의 pod repo url을 작성해준다.

s.author에는 github 계정 이름과 이메일이 들어간다.

 

s.ios.deployment_target과 s.swift_version을 배포할 버전에 맞게 작성해준다.

s.ios.deployment_target은 생성시 9.0이였는데 배포하려는 프로젝트 타겟이 15이상이여서 15로 설정했는데 11.0이하의 경우 버전이 너무 낮다고 오류가 떠서 올려줘야 했던듯 하다.

s.swift_version은 직접 추가하였다.

s.swift_version을 작성해주지 않으면 terminal에서 swift 버전을 입력해야 됬어서 아예 podspec에 작성하였다.

 

추후 코드 수정하는 경우 github의 업데이트하고 .podspec에서 s.version을 올린뒤 배포해주어야된다.

 

# TestLib.podspec
# Be sure to run `pod lib lint TestLib.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'TestLib'
  s.version          = '0.1.0'
  s.summary          = 'A short description of TestLib.'

# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://github.com/username/TestLib' #POD REPO git 경로
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'username' => 'githubAccount@email.co.kr' }
  s.source           = { :git => 'https://github.com/username/TestLib.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '15.0'
  s.swift_version = '4.0'

  s.source_files = 'TestLib/Classes/**/*'
  
  # s.resource_bundles = {
  #   'TestLib' => ['TestLib/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end

 

6. 작성한 코드 배포 가능 여부 확인

library pod 프로젝트의 루트 폴더에서 pod spec lint를 실행하여 pod 배포가 가능한지 확인한다.

$ pod spec lint

아래와 같이 오류가 났다면 아직 github의 pod repo에 업로드하지 않았기 때문이므로 아래 글을 따라 git에 올리면 된다.

WARN이 뜨는 경우는 --allow-warnings 옵션으로 배포가능하지만 ERROR가 뜨는 경우 pod 배포가 안되지 오류 수정을 해야한다.

github에 올린뒤 아래처럼 passed validation이 뜨면 배포 가능하다.

7. github 업로드

$ git add .
$ git commit -m "Initial Commit"
$ git remote add origin https://github.com/username/LIB_REPO.git
$ git push origin main

$ git tag 0.1.0
$ git push origin 0.1.0

 

코드를 수정후 재 배포할 경우에는 아래와 같이 버전을 업데이트 하면 된다.

$ git add .
$ git commit -m "Initial Commit"
$ git tag 0.1.1
$ git push origin 0.1.1

 

8. Spec Pod에 Lib podspec 등록

처음에 만들어두었던 spec pod 경로로 이동하여 수정해둔 Lib Pod의 podspeec 경로를 등록한다.

$ cd ~/.cocoapods/repos/REPO_NAME
$ pod repo push REPO_NAME LIB_NAME.podspec경로
$ cd ~/.cocoapods/repos/specs
$ pod repo push specs /Users/Documents/ios/frameworks/TestLib/TestLib.podspec

Lib_Name.podspec 경로 찾는 방법

 

 

이때 WARN이 뜨면 --allow-warnings 옵션을 추가하면 된다.

$ pod repo push --allow-warnings REPO_NAME LIB_NAME.podspec경로

$ pod repo push --allow-warnings specs /Users/Documents/ios/frameworks/TestLib/TestLib.podspec

 

정상적으로 진행되면 아래와 같이 github spec repo에 LIB_NAME.podspec이 등록된다.

버전 업데이트가 되면 podspec의 version정보를 변경하고 다시 push 해주면 등록된 버전을 볼 수 있다.

 

Private Repo 사용하기

private repo를 사용할 ios project를 하나 생성해주고 terminal에서 해당 프로젝트 경로로 이동후 pod init 을 통해 podfile을 추가해준다.

LibTestProject라는 이름으로 ios project를 생성하였다.

$ cd documents/ios/LibTestProject
$ pod init

 

pod file에 spec pro git 경로와 lib name을 작성하고 저장한다.

source 'https://github.com/username/SPEC_REPO.git' #spec repo git 경로

pod 'LIB_NAME'  #lib name

 

Podfile 전체 코드

# Uncomment the next line to define a global platform for your project
# platform :ios, '15.0'
source 'https://github.com/username/Specs.git' #spec repo git 경로

pod 'TestLib'  #lib name
target 'LibTestProject' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for LibTestProject

  target 'LibTestProjectTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'LibTestProjectUITests' do
    # Pods for testing
  end

end

 

처음에는 pod install을 통해 설치해주고 이후 버전이 변경되면 pod update를 통해 설치해준다.

$ pod install

$ pod update

 

install이 끝나면 import 를 통해 작성한 코드를 호출하여 사용한다.


결과

pod에 만들어둔 viewcontroller를 ios project에서 호출하여 잘 보여주고 있다.

 

안타깝게도 수정은 막혀있지만 작성한 파일과 코드가 모두 공개되어 보인다. 

외부 배포가 필요하기 때문에 코드를 숨기는 방법이 있는 지 다시 찾아봐야겠다.


참고한 사이트 :  https://medium.com/@hacky12/private-cocoa-pods-%EB%A7%8C%EB%93%A4%EA%B8%B0-cd1a89695ab