xcode 在多个项目中使用 CocoaPods

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16795280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:26:16  来源:igfitidea点击:

Using CocoaPods with multiple projects

iosxcodecocoapodsxcconfig

提问by Mihai Damian

I have a workspace that contains:

我有一个包含以下内容的工作区:

  • myiPhone.xcodeproj
  • sharedStuff/sharedStuff.xcodeproj
  • 我的iPhone.xcodeproj
  • sharedStuff/sharedStuff.xcodeproj

sharedStuff.xcodeproj builds a static library that is a dependency to myiPhone.xcodeproj (for simplicity assume that each project has a single target).

sharedStuff.xcodeproj 构建了一个依赖于 myiPhone.xcodeproj 的静态库(为简单起见,假设每个项目都有一个目标)。

Now I want to add a library through CocoaPods that should be available to both projects.

现在我想通过 CocoaPods 添加一个应该可用于两个项目的库。

My Podsfile looks like this:

我的 Podsfile 看起来像这样:

workspace 'myWorkspace.xcworkspace'
platform :ios

target :myiPhone do
    xcodeproj 'myiPhone.xcodeproj'
    pod 'MBProgressHUD', '~> 0.6'
end


target :sharedStuff do
    xcodeproj 'sharedStuff/sharedStuff.xcodeproj'
    pod 'MBProgressHUD', '~> 0.6'
end

When I build I get these errors:

当我构建时,我收到这些错误:

diff: /../Podfile.lock: No such file or directory diff: /Manifest.lock: No such file or directory error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.

diff:/../Podfile.lock:没有那个文件或目录 diff:/Manifest.lock:没有那个文件或目录错误:沙箱与 Podfile.lock 不同步。运行“pod install”或更新你的 CocoaPods 安装。

Anyone have a clue what's going on here?

任何人都知道这里发生了什么?

UPDATE: From the looks of it the PODS_ROOT variable is not set when the "Check Pods Manifest.lock" build phase is executed.

更新:从外观上看,在执行“Check Pods Manifest.lock”构建阶段时未设置 PODS_ROOT 变量。

采纳答案by Fergal Rooney

The first targets in your xcode projects have a build phase to perform a diff on two lock files. But it seems like your xcode projects configurations are not referencing the user defined settings configured in Pods/Pods-libPods.xcconfig.

xcode 项目中的第一个目标有一个构建阶段来对两个锁定文件执行差异。但似乎您的 xcode 项目配置并未引用 Pods/Pods-libPods.xcconfig 中配置的用户定义设置。

It looks like you are trying to link a Pod with specific targets in multiple xcodeprojs. If my assumption is correct, you are using the targetattribute incorrectly. The targetattribute creates a new static library within the Pods project that includes the Pods you configured within that target.

看起来您正在尝试将 Pod 与多个 xcodeprojs 中的特定目标链接起来。如果我的假设是正确的,那么您使用的target属性不正确。该target属性在 Pods 项目中创建一个新的静态库,其中包含您在其中配置的 Pod target

The default target for the Pods xcodeproj is libPods which generates the libPods.a static library. This is generated if you do not specify a target. So if you don't care about generating multiple static libaries in the Pods xcodeproj, don't bother defining a target and use the link_withattribute to link the default libPods target(static library) to the targets in your xcodeprojs.

Pods xcodeproj 的默认目标是 libPods,它生成 libPods.a 静态库。如果您未指定target. 因此,如果您不关心在 Pods xcodeproj 中生成多个静态库,请不要费心定义目标并使用该link_with属性将默认 libPods target(静态库)链接到 xcodeprojs 中的目标。

For example, the following Podfile will create a libPods target in Pods.xcodeproj which will add MBProgressHUD sources to the compile phase then add the xcconfig file defining PODS_ROOT and the PODS_HEADER_SEARCH_PATH for example to each of your xcodeprojs. It will then link this static library to the targets you specified with link_withand xcodeproj

例如,以下 Podfile 将在 Pods.xcodeproj 中创建一个 libPods 目标,它会将 MBProgressHUD 源添加到编译阶段,然后将定义 PODS_ROOT 和 PODS_HEADER_SEARCH_PATH 的 xcconfig 文件添加到每个 xcodeprojs。然后它会将这个静态库链接到您指定的目标link_withxcodeproj

workspace 'myWorkspace.xcworkspace'
platform :ios

xcodeproj 'myiPhone.xcodeproj'
link_with 'myiPhone'
xcodeproj 'sharedStuff/sharedStuff.xcodeproj'
link_with 'sharedStuff'

pod 'MBProgressHUD', '~> 0.6'

回答by Andrey Gordeev

I have 2 projects in my Workspace and the accepted answer didn't work for me. But finally I've managed how to get Cocoapods working properly with 2 projects. Here is how my pod file looks like:

我的工作区中有 2 个项目,接受的答案对我不起作用。但最后我已经设法让 Cocoapods 在 2 个项目中正常工作。这是我的 pod 文件的样子:

workspace 'Projects.xcworkspace'
platform :ios, '8.0'

use_frameworks!

# ignore all warnings from all pods
inhibit_all_warnings!

def shared_pods
    # all the pods go here
    # pod 'Parse' etc.
end

xcodeproj 'Project1.xcodeproj'
xcodeproj 'Project2/Project2.xcodeproj'

target :Project1 do
  xcodeproj 'Project1'
  shared_pods
end

target :Project2 do
  xcodeproj 'Project2/Project2.xcodeproj'
  shared_pods
end

回答by Yesbol Kulanbekov

With current syntax it looks like this

使用当前的语法,它看起来像这样

use_frameworks!
workspace 'myWorkspace'

project 'myiPhone'
project 'sharedStuff/sharedStuff'


target 'myiPhone' do
    project 'myiPhone'
    pod 'MBProgressHUD', '~> 0.6'
end


target 'sharedStuff' do
    project 'sharedStuff/sharedStuff'
    pod 'MBProgressHUD', '~> 0.6'
end

回答by Rahul Vyas

This is my folder structure

这是我的文件夹结构

OB
|podfile
|Project1->Project1.xcodeproj
|Project2->Project2.xcodeproj

and this is my podfile inside the OB Folder

这是我在 OB 文件夹中的 podfile

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

workspace 'OB.xcworkspace'
use_frameworks!

# ignore all warnings from all pods
inhibit_all_warnings!

project 'Project1/Project1.xcodeproj'
project 'Project2/Project2.xcodeproj'

abstract_target 'OB' do
    pod 'Alamofire', '~> 4.0'

    target 'Project1' do
        project 'Project1/Project1.xcodeproj'
    end

    target 'SchoolKids' do
        project 'Project2/Project2.xcodeproj'
    end
end

This will add Afnetworking/Alamofire to both projects.If we need a exclusive pod to a particular project then we can do this

这会将 Afnetworking/Alamofire 添加到两个项目中。如果我们需要一个特定项目的专属 pod,那么我们可以这样做

 target 'Project1' do
        project 'Project1/Project1.xcodeproj'
        pod 'Alamofire', '~> 4.0'
    end

回答by loc

Thanks to Rahul Vyas 's answer, I finally sucess in my project. My project is in react-native, and I use a git submodules as subproject. So this is my pod file:

感谢 Rahul Vyas 的回答,我终于在我的项目中取得了成功。我的项目是 react-native,我使用 git 子模块作为子项目。所以这是我的 pod 文件:

workspace 'LuminPDFApp.xcworkspace'
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
use_frameworks!

inhibit_all_warnings!

project 'LuminPDFApp.xcodeproj'
project '../submodules/react-native-filepicker/ios/RNFilePicker.xcodeproj'

abstract_target 'LuminPDFApp' do
    pod 'SwiftyDropbox'

    target 'LuminPDFApp' do
        project 'LuminPDFApp.xcodeproj'
    end

    target 'RNFilePicker' do
        project '../submodules/react-native-filepicker/ios/RNFilePicker.xcodeproj'
    end
end