您的当前位置:首页正文

使用CocoaPods下载第三方库

来源:华拓网

我已经安装好了CocoaPods了,现在需要在xcode里面使用它。

(1)新建了一个swift的工程,命名为Test,工程命名首字母大写。打开工程文件Test.xcodeproj

(2)open Terminal

命令行:cd 你的工程文件路径

(3)创建Podfile文件,它长这个样子

命令行pod search Masonry可以查看Masonry一些版本信息,地址等

可以通过命令行touch Podfile创建,也可以通过命令行vimPodfile创建,创建好了之后,在Podfile文件中写上第三方库名字和版本号,这样子cocoapods才知道你要下载什么。

命令行open -a Xcode Podfile用xcode打开Podfile文件,复制以下内容保存:

platform :ios, '7.0'

inhibit_all_warnings!

pod 'Masonry', '~> 0.6.3’

(4)执行下载命令。

命令行 pod install(需要翻墙)

或者命令行pod install --verbose --no-repo-update(—no-repo-update 是说不去同步仓库,如果确定本地下载过仓库,开启这个选项快很多,无论翻没翻墙)

lileideMacBook-Pro:Testlilei$ pod install

Updating local specs repositories

//注意:这里我开启了vpn,还是需要等待5分钟以上的时间,最讨厌就是命令行等待的时候不给予任何提示信息,应该有什么命令行或是工具可以去看终端的工作状态的吧????

。。。。。

[!] Please close any current Xcode sessions and use `Test.xcworkspace` for this project from now on.

Sending stats

lileideMacBook-Pro:Testlilei$

//得到以上信息就表示下载第三方的库好了,现在可以从.xcworkspace打开你的工程了

(5)打开Test.xcworkspace,在swift工程里面引入oc的头文件,需要一个链接这两者的头文件。

Adding this file to Test will create a mixed Swift and Objective-C target. Would you like Xcode to automatically configure a bridging header to enable classes to be accessed by both languages?将此文件添加到测试将创建一个混合迅速和objective - c的目标。你想Xcode自动配置一个连接头,使类访问两种语言吗? 在这个文件里面导入第三方库头文件 #import<Masonry.h>

现在需要在这里看看两个地方的配置:

我们也可以自己建一个头文件,然后去改配置,像这样子:

[!] The `Test [Debug]` target overrides the `HEADER_SEARCH_PATHS` 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.

(6)使用一下Masonry

func createTextView(){

lettextView =UIView()

textView.backgroundColor=UIColor.grayColor()

self.view.addSubview(textView)

textView.mas_makeConstraints{ (make) ->Voidin

make.top.equalTo()(self.view).offset()(88)

make.left.equalTo()(self.view).offset()(20)

make.right.equalTo()(self.view).offset()(-20)

make.height.equalTo()(40)

}

}


再下载一个AFNetworking

(1)命令行open -a Xcode Podfile打开Podfile文件,在里面再添加一个AFNetworking的版本号和名字:

platform :ios,'7.0'

inhibit_all_warnings!

pod'Masonry','~> 0.6.3’

pod'AFNetworking','~> 2.5.0'

(2)命令行 pod update

(3)test-Bridging-Header.h文件里面导入

#import

(4)使用一下

private let manager =AFHTTPRequestOperationManager()



这些问号真的好难看!!好吧,得去掉它们

touch .gitignore  新建文件.gitignore,

然后在”.gitignore” 文件里输入你要忽略的文件夹及其文件就可以了