美文网首页SetiOS tipsgit和pods知识
CocoaPods为多个target添加依赖库

CocoaPods为多个target添加依赖库

作者: _时光念你 | 来源:发表于2016-11-25 15:46 被阅读4944次

CocoaPods1.0.1依赖库添加方法

自从CocoaPods升级到1.0.1之后,各种坑,之前的link_with语法不能用了,在网上找了好久也没找到解决办法.错误如下:

[!] Invalid `Podfile` file: [!] The specification of `link_with` in the Podfile is now unsupported, please use target blocks instead..

而且我们公司都是快20个target,依赖库都是一样的,Ctrl+c 再 Ctrl+v target 'targetName1' do ... end 这样的语法也是可以解决问题,但是咱们程序员最重要的就是有一颗拒绝复制粘贴的心.
由于Podfile文件是ruby写的,所以我就去网上看了下ruby的语法,写了多个target依赖的库大部分相同的Podfile文件,运行pod install 没问题,全部安装完毕!

编辑工程中的Podfile,根据需求修改库和target名称

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

# -*- 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

附:
单个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依赖库

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

相关文章

网友评论

  • LD_左岸:您好当我在podfile文件中这样写的时候
    source 'https://github.com/CocoaPods/Specs.git'
    platform :ios, '8.0'
    target 'tectCplusPlus' do
    pod 'AFNetworking', '~> 3.1.0'
    pod 'Masonry', '~> 1.0.2'
    pod 'MJExtension', '~> 3.0.13'
    end

    target 'WordMemoryLib' do
    pod 'AFNetworking', '~> 3.1.0'
    pod 'Masonry', '~> 1.0.2'
    pod 'MJExtension', '~> 3.0.13'
    end
    其中WordMemoryLib是我添加的静态库
    发现模拟机无限重启....
    您知道咋回事吗
    LD_左岸:sorry 解决了 是podfile没写好
  • hehtao:对于多target cocoapod 官网(https://guides.cocoapods.org/using/the-podfile.html)有详细的说明,如下:
    If you want multiple targets to share the same pods, use an abstract_target.
    # There are no targets called "Shows" in any Xcode projects
    abstract_target 'Shows' do
    pod 'ShowsKit'
    pod 'Fabric'

    # Has its own copy of ShowsKit + ShowWebAuth
    target 'ShowsiOS' do
    pod 'ShowWebAuth'
    end

    # Has its own copy of ShowsKit + ShowTVAuth
    target 'ShowsTV' do
    pod 'ShowTVAuth'
    end
    end
  • 张芳涛:你好,这边按照你的教程集成出现了问题,就是之前的每一个第三方引用,现在都变成了两个,一个是对应的iOS9.0的一个是对应iOS9.2的,而且还一直报错,找不到第三方库,请问怎么办?
  • eb3ff47429db:你好,问个问题 — — 如何更新某个target下的某个特定库呢?或者如何更新所有target下的某个同名的库?
    _时光念你:没有单独更新某个库的命令。指定了版本号的库会更新到那个版本号,没写的会更新到最新。如果你没写的话又不想更新那个库,可以查看该库版本号,以MJRefresh为例,办法:在pod/MJRefresh/Support Files/Info.plist的Bundle versions string, short对应的值就是当前库的版本号,然后指定版本号该库就不会更新。
  • ChardXu:区分依赖库的话貌似不行啊,报错:
    [!] Could not automatically select an Xcode project. Specify one in your Podfile like so:

    project 'path/to/Project.xcodeproj'
    _时光念你:你cd的目录不对吧,没有工程文件在该目录下!

本文标题:CocoaPods为多个target添加依赖库

本文链接:https://www.haomeiwen.com/subject/yuxwpttx.html