我在公司的废弃项目上做改造。点击查看项目。
创建podfile
终端cd到工程根目录,执行命令:
pod init
然后用open命令打开podfile文件:
open podfile
以下是默认生成的文件内容:
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'Syiar' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for Syiar
target 'SyiarTests' do
inherit! :search_paths
# Pods for testing
end
target 'SyiarUITests' do
# Pods for testing
end
end
导入AFNetworking
先单独尝试集成AFNetworking
修改podfile,添加下面一行:
# Pods for Syiar
pod 'AFNetworking', '~> 3.0'
然后回到终端,执行安装命令:
pod install
成功后会生成xcworkspace文件:
[!] Please close any current Xcode sessions and use `Syiar.xcworkspace` for this project from now on.
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
有时候也会伴随一些警告。我碰到三条警告:
[!] Automatically assigning platform `iOS` with version `9.0` on target `Syiar` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
[!] The `Syiar [Debug]` target overrides the `OTHER_LDFLAGS` build setting defined in `Pods/Target Support Files/Pods-Syiar/Pods-Syiar.debug.xcconfig'. This can lead to problems with the CocoaPods installation
- Use the `$(inherited)` flag, or
- Remove the build settings from the target.
[!] The `Syiar [Release]` target overrides the `OTHER_LDFLAGS` build setting defined in `Pods/Target Support Files/Pods-Syiar/Pods-Syiar.release.xcconfig'. This can lead to problems with the CocoaPods installation
- Use the `$(inherited)` flag, or
- Remove the build settings from the target.
第一条是提醒我们指定platform。打开podfile,发现已经默认添加了,但是需要取消注释:
platform :ios, '9.0'
后面两条需要我们打开新生成的xcworkspace - 如果之前打开了工程要先关闭。
然后在Build Settings找到other linker flags,将里面原有的删除,添加$(inherited)。Xcode会自动展开成需要的。
会到终端,再执行一次pod install。警告消失了。
然后在Xcode中删除之前Carthage导入的framework。再在Build Phases 下查找有无 包含以下内容的 Run Script:
/usr/local/bin/carthage copy-frameworks
如果有,删除Input Files中的AFNetworking:
$(SRCROOT)/Carthage/Build/iOS/AFNetworking.framework
Xcode编译,成功。
导入其他库
在podfile中添加其他需要导入的库。我添加了下面这些:
pod 'YYModel'
pod 'Masonry'
pod 'SDWebImage'
pod 'MBProgressHUD'
pod 'SSZipArchive'
然后回到终端,执行安装命令:
pod install
完成后回到Xcode删除对应的framework。
整个删除前文提到Run Script脚本。去到工程目录,删掉Carfile、Carfile.resolved、Carthage/文件夹。
ZipArchive这个库的引入方式需要修改一下:
#import <SSZipArchive/ZipArchive.h>
最后将Pods/文件夹加入到.gitingore中:
# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
Pods/
完成。