Section 3/151 menit

3. CocoaPods: Cara Kerja Mendalam

3. CocoaPods: Cara Kerja Mendalam

Podfile Anatomy

swift
# Podfile — letakkan di root project

# Platform minimum
platform :ios, '16.0'

# Gunakan static frameworks — lebih cepat launch time
use_frameworks! :linkage => :static

# Opsi global
install! 'cocoapods',
  :deterministic_uuids => false,
  :warn_for_unused_master_specs_repo => false

# Target utama
target 'MyApp' do
  # Dependency dengan versi constraint
  pod 'Alamofire', '~> 5.9'          # ~> = compatible dengan (>=5.9, <6.0)
  pod 'Kingfisher', '>= 7.0', '< 8'  # explicit range
  pod 'Lottie', :git => 'https://github.com/airbnb/lottie-ios.git',
                :branch => 'master'  # dari branch
  pod 'SnapKit', '5.7.1'             # exact version
  
  # Pod dari path lokal (untuk library internal)
  pod 'MyInternalSDK', :path => '../MyInternalSDK'
  
  # Pod dengan subspecs
  pod 'Firebase/Analytics'
  pod 'Firebase/Crashlytics'
  pod 'Firebase/Messaging'
  
  # Target test — pod hanya untuk testing
  target 'MyAppTests' do
    inherit! :search_paths
    pod 'Quick', '~> 7.0'
    pod 'Nimble', '~> 13.0'
    pod 'OHHTTPStubs/Swift', '~> 9.0'
  end
  
  target 'MyAppUITests' do
    inherit! :search_paths
  end
end

# Post-install hook — modifikasi build settings setelah pod install
post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      # Set minimum deployment target sama dengan app
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '16.0'
      
      # Suppress warnings dari pods
      config.build_settings['GCC_WARN_INHIBIT_ALL_WARNINGS'] = 'YES'
      config.build_settings['SWIFT_SUPPRESS_WARNINGS'] = 'YES'
      
      # Exclude arm64 untuk simulator di M1 (jika ada pod yang belum support)
      # config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
    end
  end
end

Podspec: Definisi Library

swift
# MyLibrary.podspec

Pod::Spec.new do |spec|
  spec.name         = 'MyLibrary'
  spec.version      = '1.2.3'
  spec.summary      = 'Custom networking library'
  spec.description  = <<-DESC
    A lightweight networking library built on top of URLSession.
    Supports async/await and Combine.
  DESC
  
  spec.homepage     = 'https://github.com/myorg/MyLibrary'
  spec.license      = { :type => 'MIT', :file => 'LICENSE' }
  spec.author       = { 'My Team' => 'team@myorg.com' }
  
  spec.ios.deployment_target  = '16.0'
  spec.macos.deployment_target = '13.0'
  spec.swift_versions = ['5.9', '6.0']
  
  spec.source       = { :git => 'https://github.com/myorg/MyLibrary.git', :tag => spec.version }
  spec.source_files = 'Sources/MyLibrary/**/*.swift'
  
  # Resources
  spec.resources = 'Sources/MyLibrary/Resources/**/*'
  spec.resource_bundles = {
    'MyLibrary' => ['Sources/MyLibrary/Resources/**/*']
  }
  
  # Dependencies
  spec.dependency 'Alamofire', '~> 5.9'
  
  # Subspecs — bagian-bagian opsional library
  spec.subspec 'Core' do |core|
    core.source_files = 'Sources/MyLibrary/Core/**/*.swift'
  end
  
  spec.subspec 'UI' do |ui|
    ui.source_files = 'Sources/MyLibrary/UI/**/*.swift'
    ui.dependency 'MyLibrary/Core'
    ui.frameworks = 'UIKit'
  end
  
  # Default subspec jika tidak dispesifikkan
  spec.default_subspecs = 'Core'
end

Pod Install vs Pod Update

swift
# pod install: HANYA install pod baru atau yang berubah di Podfile
# Menggunakan Podfile.lock untuk versi yang sudah ada
pod install

# pod update: UPDATE semua pod ke versi terbaru yang diizinkan constraint
pod update

# pod update spesifik satu pod
pod update Alamofire

# pod install dengan verbose (debug masalah)
pod install --verbose

# pod install tanpa update repo specs (lebih cepat di CI)
pod install --no-repo-update

# Cek outdated pods
pod outdated

# Validate podspec
pod spec lint MyLibrary.podspec --verbose
pod lib lint --allow-warnings

File yang Harus di-Commit vs .gitignore

swift
# ✅ COMMIT ke Git:
Podfile
Podfile.lock       # PENTING: lock file memastikan semua developer pakai versi sama

# ❌ JANGAN commit (tambahkan ke .gitignore):
Pods/              # Folder ini besar dan bisa di-regenerate
*.xcworkspace      # Bisa di-regenerate dengan pod install
swift
# .gitignore untuk CocoaPods
Pods/
*.xcworkspace