使用Pod管理本地库

随着项目变大,公司的库和第三方的库越来越多,如果通过手动去导入几十framework文件,感觉头大.通过CocoaPods管理本地库和第三方库就方便了很多,下面开始正题.

 

1.创建Podfile,并编辑

打开终端,cd [xcode项目目录], 输入pod init自动创建Podfile

MacBook-Air:app jipengjo$ pod init

MacBook-Air:app jipengjo$ ls

AppDelegate.h AppDelegate.m Podfile app app.xcodeproj build

MacBook-Air:Banner jipengjo$ vim Podfile

 

ls查看多了一个Podfile.记事本打开Podfile文件进行编辑.路径起始从Podfile文件所在位置,“Pods for app”下面导入的库路径为podspec文件所在路径

# Uncomment the next line to define a global platform for your project

  platform :ios, '9.0'

 target 'app' do

 # Comment the next line if you don't want to use dynamic frameworks

 use_frameworks!

 # Pods for app

pod 'Banner', :path => '../Banner'       

 

2.创建podspec文件,并编辑

MacBook-Air:app jipengjo$ cd ../Banner/

MacBook-Air:Banner jipengjo$ pod spec create Banner

MacBook-Air:Banner jipengjo$ vim Banner.podspec

 

修改后的

spec.description  = <<-DESC

一个广告显示控件,可自动从网络下载图片缓存到本地.

                   DESC

spec.homepage     = "http://www.github.com/hello/Banner"

spec.license      = { :type => "MIT", :file => "LICENSE" }

spec.ios.deployment_target = "8.0"

spec.author             = { "hello" => "hello@126.com" }

spec.source       = { :git => "http://www.github.com/hello/Banner.git", :tag => "#{spec.versi    on}" }

spec.source_files  = "Classes", "Classes/**/*.{h,m}"

spec.resource_bundles = {

  'Banner' => ['Assets/*']

}

 

在当前文件夹下创建Classes和Assets文件夹,LICENSE文件

MacBook-Air:Banner jipengjo$ mkdir Classes

MacBook-Air:Banner jipengjo$ mkdir Assets

LICENSE文件内容是从其它文件拷贝来的:

    Copyright (c) 2019 hello <hello@126.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

把相关的代码文件拷贝到Classes文件夹下,图片文件拷贝到Assets文件夹下,然后回到原来的app目录下,执行pod install.

MacBook-Air:Banner jipengjo$ cd ../app/

 

如果有图片文件在Assets文件夹下需要调用,在库中添加下面的函数.

+ (UIImage *)loadBundleImage:(NSString *)imageName

{

    NSString *bundlePath = [[NSBundle bundleForClass:[self class]].resourcePath stringByAppendingPathComponent:@"Banner.bundle"];

    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];

    UIImage *image = [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];

    return image;

}

不知到为什么上面的代码后面添加的执行pod install,程序报错,而执行pod clean后,再执行pod install就没问题了.

最终可以在Banner.framework内能中到Banner.bundle文件夹,再进里面就能看到里面的图片了.

 

参考:

https://www.jianshu.com/p/9183cded6b2e

https://www.cnblogs.com/1-434/articles/10588234.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值