关于Pods written in Swift can only be integrated as frameworks问题的解决

来源:互联网 发布:西科软件科技有限公司 编辑:程序博客网 时间:2024/05/22 07:46

使用cocopods导入第三方库报错 :

[!] Pods written in Swift can only be integrated as frameworks; add `use_frameworks!` to your Podfile or target to opt into using it. The Swift Pods being used are: Alamofire and SnapKit


配置如下:

pod 'Alamofire', '~> 3.4.0'
pod 'SnapKit', '~> 0.30.0.beta1'

经查阅资料:

Because Apple doesn't let you build static libraries that contain Swift. Unlike Objective-C, Apple doesn't ship the Swift standard runtime libraries with iOS. This decouples the language version from the platform version. When you build an app with Swift, you're responsible yourself to ship them. By default, Xcode uses swift-stdlib-tool to handle copying the Swift runtime dylibs, but the tooling falls short when attempting to ship frameworks that use Swift with an app that is Objective-C only. Your app executable and the frameworks you ship will all use the same set of dylibs, which are embedded into the Frameworks subdirectory of the application bundle.First, that's because you can't link against different versions of the standard libraries. Furthermore it is desirable to embed them only once and not multiple times, because of constraints to memory size and network speed, which are relevant for distribution. You can make CocoaPods integrate to your project via frameworks instead of static libraries by specifying use_frameworks!. If that's not present, you won't be able to integrate dependencies, if you depend on a pod which includes Swift source code.

加上 use_frameworks! 问题解决

pod 'Alamofire', '~> 3.4.0'
pod 'SnapKit', '~> 0.30.0.beta1'
use_frameworks!


一、什么是CocoaPods

CocoaPods是iOS项目的依赖管理工具,该项目源码在Github上管理。开发iOS项目不可避免地要使用第三方开源库,CocoaPods的出现使得我们可以节省设置和第三方开源库的时间。
在使用CocoaPods之前,开发项目需要用到第三方开源库的时候,我们需要
1.把开源库的源代码复制到项目中
2.添加一些依赖框架和动态库
3.设置-ObjC,-fno-objc-arc等参数
4.管理他们的更新
在使用CocoaPods后,我们只需要把用到的开源库放到一个名为Podfile的文件中,然后执行pod install.Cocoapods就会自动将这些第三方开源库的源码下载下来,并且为我们的工程设置好响应的系统依赖和编译参数。

二、CocoaPods的原理

CocoaPods的原理是将所有的依赖库都放到另一个名为Pods的项目中,然后让主项目依赖Pods项目,这样,源码管理工作都从主项目移到了Pods项目中。Pods项目最终会编译成一个名为libPods.a的文件,主项目只需要依赖这个.a文件即可。

三、CocoaPods的安装

CocoaPods可以方便地通过Mac自带的RubyGems安装。
打开Terminal,然后键入以下命令:
$ sudo gem install cocoapods

执行完这句如果报告以下错误:
ERROR: Could not find a valid gem 'cocoapods' (>= 0), here is why:
Unable to download data from https://rubygems.org/ - Errno::ETIMEDOUT: Operation timed out - connect(2) (https://rubygems.org/latest_specs.4.8.gz)
ERROR: Possible alternatives: cocoa pods

这是因为ruby的软件源rubygems.org因为使用亚马逊的云服务,被我天朝屏蔽了,需要更新一下ruby的源,过程如下:
$ gem sources -l (查看当前ruby的源)
$ gem sources --remove https://rubygems.org/
$ gem sources -a https://ruby.taobao.org/
$ gem sources -l

如果gem太老,可以尝试用如下命令升级gem
$ sudo gem update --system
升级成功后会提示: RubyGems system software updated

然后重新执行安装下载命令
$ sudo gem install cocoapods
这时候应该没什么问题了(如果报告Operation not permitted...之类的错误,请参考第五节:常见问题5,如果报告activesupport requires Ruby version >= 2.2.2这样的错误,请参考第五节:常见问题6)

接下来进行安装,执行:
$ pod setup

Terminal会停留在 Setting up CocoaPods master repo 这个状态一段时间,是因为要进行下载安装,而且目录比较大,需要耐心等待一下.如果想加快速度,可使用cocoapods的镜像索引.(文章末尾附使用镜像索引的方法)
安装成功后,你会看到:


01.png
四、Cocoapods的使用

进入工程所在的目录(工程根目录)
执行命令 touch Podfile
这句是说新建一个名为Podfile的文件(不能写成别的名字,也可以自己在工程根目录里面直接新建)

然后对改文件进行编辑,执行命令 open -e Podfile
第一次执行这个命令,会有一个空白文件打开,可以先放在一边,
Podfile文件的格式应该如下:
platform :ios, '7.0'
pod 'AMap2DMap', '~> 2.5.0'
pod 'AFNetworking', '~> 2.5.3'
pod 'SDWebImage', '~> 3.7.2'

需要注意的几点:platform那一行,ios三个字母都要小写,而且与前面的冒号之间不能有间隔,后面的版本号也可以不写,但是有些开源库对版本是有要求的,比如要在6.0以上才能运行,遇到这样的开源库就需要写上版本号。

platform下面就是Cocoapods需要集成的开源库,根据你的需要确定集成那些库。

举个例子:
我要集成AFNetworking这个库类,需要在Cocoapods里面先搜索是否有需要的库,可以在Terminal中输入:
pod search AFNetworking
回车之后就可以看到和你搜索的关键字相关的一些库类,如图:


02.png

其中第一个就是我们需要的,把pod ‘AFNetworking’, ‘~>2.5.3’
那一行复制到我们的Podfile文件中,保存修改。
然后在Terminal中执行 :
pod install

这样,AFNetworking就已经下载完成并且设置好了编译参数和依赖,以后使用的时候切记如下两点:
1.从此以后需要使用Cocoapods生成的 .xcworkspace文件来打开工程,而不是使用以前的.xcodeproj文件
2.每次更改了Podfile文件,都需要重新执行一次pod update命令

ps:当执行pod install之后,除了Podfile,还会生成一个名为Podfile.lock的文件,它会锁定当前各依赖库的版本,之后即使多次执行pod install也不会更改版本,只有执行pod update才会改变Podfile.lock.在多人协作的时候,这样可以防止第三方库升级时候造成大家各自的第三方库版本不一致。所以在提交版本的时候不能把它落下,也不要添加到.gitignore中.

五、常见问题

1.
[!] Invalid Podfile file: undefined local variable or method `en_US' for #<Pod::Podfile:0x00000102a5d8b0>. Updating CocoaPods might fix the issue.

原因:单引号格式,可能是手动输入导致
解决办法:系统偏好设置-键盘-文本-将“使用智能引号和破折号”一项取消勾选-再将podfile里面的单(双)引号修改一下

2.ArgumentError - invalid byte sequence in US-ASCII
原因:字符集错误
解决办法:
使用locale命令查看当前的字符集,如果都是zh,需要执行以下命令:
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
然后再使用locale命令查看,已经改过来了

  1. [!] The YMTea [Debug] target overrides the OTHER_LDFLAGS build setting defined in `Pods/Target Support Files/Pods/Pods.debug.xcconfig'. This can lead to problems with the CocoaPods installation
    • Use the $(inherited) flag, or
    • Remove the build settings from the target.

[!] The YMTea [Release] target overrides the OTHER_LDFLAGS build setting defined in `Pods/Target Support Files/Pods/Pods.release.xcconfig'. This can lead to problems with the CocoaPods installation

- Use the `$(inherited)` flag, or- Remove the build settings from the target.

原因:我是在已有项目中集成Cocoapods的时候遇到这个问题,原因是项目 Target 中做了一些设置,CocoaPods 也做了默认的设置,如果两个设置结果不一致,就会造成问题。
解决方法:我想要使用 CocoaPods 中的设置,分别在我的项目中定义PODS_ROOTOther Linker Flags的地方(build settings),把他们的值用$(inherited)替换掉,进入终端,执行 pod update
警告没了,回到 Xcode,build通过。
网上还流行另外一种简单粗暴的方法:点击项目文件 project.xcodeproj,右键显示包内容,用文本编辑器打开project.pbxproj,删除OTHER_LDFLAGS的地方,保存(这种我没试过)

4.
[!] Oh no, an error occurred.

It appears to have originated from your Podfile at line 2.

Search for existing GitHub issues similar to yours:
https://github.com/CocoaPods/CocoaPods/search?q=%2FUsers%2Fxiao6%2FMusic%2FGI06%E5%AE%9E%E8%AE%AD%E8%8A%B8%E8%8C%97%E8%8C%B6%E5%8F%B6%2FYMTea%2FPodfile%3A2%3A+syntax+error%2C+unexpected+%27%3A%27%2C+expecting+end-of-input%0Aplatform+%3A+ios%2C+%277.0%27%0A++++++++++%5E&type=Issues

If none exists, create a ticket, with the template displayed above, on:
https://github.com/CocoaPods/CocoaPods/issues/new

Be sure to first read the contributing guide for details on how to properly submit a ticket:
https://github.com/CocoaPods/CocoaPods/blob/master/CONTRIBUTING.md

Don't forget to anonymize any private data!

原因:这个问题比较蛋疼,弄了好久,仔细看发现就是因为Podfile文件里面 platform 那一行 冒号和ios之间多了一个空格。。。。其实这个错误在报错的时候ruby已经给出了,只是一开始没有好好看:


03.png

5.(2016.4.14更新)

如果在执行sudo gem install cocoapods的时候报以下错误:

ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/pod

并且你的操作系统是EI Caption,可是尝试使用以下方法来代替上面的安装命令:

$ sudo gem install -n /usr/local/bin cocoapods

6.(2016.8.4更新)

在执行sudo gem install cocoa pods指令安装的时候提示错误:

ERROR: Error installing cocoapods:
activesupport requires Ruby version >= 2.2.2.

解决方法: 由于你的ruby版本偏低,需要升级你电脑上的ruby.
在命令行输入:ruby -v来查看当前ruby的版本.确定版本是低于2.2.2后就可以开始进行ruby的升级.

一般通过rvm来升级ruby.
具体升级ruby的方法可以参考:
http://blog.csdn.net/lissdy/article/details/9191351 这个链接中的内容(大体操作过程相同,只是要安装的版本号的差异,大家也可以自行查找其它资料来升级自己的ruby)

在执行rvm install 2.2.4的过程中,我还出现了一些报错,标题是"requirements_osx_brew_update_system ruby-2.2.4"后面有一些详细的错误日志,像下面这样:


ruby更新错误.png

从日志内容可以看出大概意思就是说我在更新brew的过程中出现了一些错误.(然后你的也可能有其他报错,只要根据日志提示的错误内容采取相应的措施就好).所以接下来我还更新了brew:

brew update--更新homeBrew自己

我又报错了:The /usr/local directory is not writable.如下:


homeBrew更新报错.png


这是说,我那个路径不可写,按照上面说的,指令也告诉我们了:
sudo chown -R $(whoami):admin /usr/local
输入密码后重新执行brew update,就出现了一系列感觉要成功的进度提示等,最后提示Already up-to-date的时候,brew本身就更新好了(这个过程比较久).
接着用brew outdated命令查看brew下哪些软件可以进行升级.
然后brew upgrade命令用来升级所以可以升级的软件.
最后记得用brew cleanup来清理不需要的版本及其安装包缓存.
截止目前,brew也被我们更新好了.

接下来回到ruby更新,重新执行rvm install 2.2.4,又是漫长的等待后,一行行绿色的字表示ruby也安装完成了,这时候再看看ruby的版本ruby -v:


ruby更新后版本.png

ok,升级好了.

7.(2016.8.4更新)

执行pod setup的时候,出现报错:

Cloning into 'master'...
error: RPC failed; result=56, HTTP code = 200
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

如图:


pod setup报错.png

这个是用于在执行pod setup命令的时候,里面会执行从github上面clone资源到本地的语句,但是要clone的东西太大了,超过的git限制的大小.尝试执行以下语句把默认的限制变大:(52428000=500×1024×1024,即500M)
git config http.postBuffer 524288000
之前git中的配置是没有这一项的,执行完以上语句后输入git config -l可以看到配置项的最下面多出了一行我们刚刚配置的内容.
接下来再执行pod setup试试看吧.

附:如何使用CocoaPods的镜像索引:
所有项目的Podspec文件都托管在https://github.com/CocoaPods/Specs,第一次执行pod setup时,CocoaPods会将这些podspec索引文件更新到本地的~/.cocoapods目录下,这个索引文件比较大,所以第一次更新时非常慢.友好人士在国内的服务器建立了Cocoapods索引库的镜像,所以执行索引跟新操作时候会快很多.具体操作方法如下:$ pod repo remove master$ pod repo add master https://gitcafe.com/akuandev/Specs.git$ pod repo update这是使用gitcafe上的镜像,将以上代码中的 https://gitcafe.com/akuandev/Specs.git 替换成 https://git.oschina.net/akuandev/Specs.git 即可使用oschina上的镜像。


文/CoderAO(简书作者)
原文链接:http://www.jianshu.com/p/6e5c0f78200a
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

0 0