CocoaPods为多个target添加依赖库/Podfile的配置

时间:2023-03-09 15:54:46
CocoaPods为多个target添加依赖库/Podfile的配置

Podfile的相关配置,请看官方文档http://guides.cocoapods.org/syntax/podfile.html

1)多个target公用相同库,还可以添加额外的不同第三方库

编辑工程中的Podfile,根据需求修改库和target名称,Podfile其实是一个ruby文件

写法一:通过abstract方式引入

#targetA: [AFNetworking,Masonry]
#targetB:[AFNetworking,SDWebImage]
abstract_target 'abstract_pod' do #这里的abstract_pod在实际targets中不存在,是虚拟
pod 'AFNetworking' target 'targetA' do
pod 'Masonry'
end target 'targetB' do
pod 'SDWebImage'
end
end

写法二:循环target 添加pod

# -*- coding: UTF-8 -*-
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0' # ruby语法
# target数组 如果有新的target直接加入该数组
targetsArray = ['targetName1', 'targetName2', 'targetName3', 'targetName4', 'targetName5']
# 循环
targetsArray.each do |t|
target t do
pod 'MJRefresh', '~> 1.4.6'
pod 'Masonry', '~> 0.6.1'
end
end

写法三:提取公共pods 各个target引入

workspace 'MST.xcworkspace'
project 'MST.xcodeproj' source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
inhibit_all_warnings! #定义相同的依赖库
def commonPods
pod 'Masonry', '~> 1.1.0'
end #工程一:
target 'AnyDoorDemo' do
commonPods
pod 'AFNetworking', '~> 3.2.1' #测试相关
target 'AnyDoorDemoTests' do
inherit! :search_paths
pod 'Specta', '~> 1.0.7'
pod 'OCMock', '~> 3.4.2'
pod 'Expecta', '~> 1.0.6'
end target 'AnyDoorDemoUITests' do
inherit! :search_paths
# Pods for testing
end
end #工程二:
target 'HostApp' do
commonPods
end

2)单个target依赖库

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
target 'targetName1' do
pod 'MJRefresh', '~> 1.4.6'
pod 'Masonry', '~> 0.6.1'
end

3)不同target依赖库

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
target 'targetName1' do
pod 'MJRefresh', '~> 1.4.6'
pod 'Masonry', '~> 0.6.1'
end target 'targetName2' do
pod 'MJRefresh', '~> 1.4.6'
pod 'Masonry', '~> 0.6.1'
pod 'AFNetworking', '~> 3.0'
end