关于使用react-native-http-cache之 cocoapods 管理的ios项目 遇到的坑

3,596 阅读2分钟

关于react-native-http-cache,根据github上,第一步我们需要

yarn add react-native-http-cache

// or

npm install react-native-http-cache --save
```

然后执行

```
react-native link react-native-http-cache
```

安卓上我先不说了。但是遇到`cocoapods`管理的项目,`build`时会一直报找不到 `***.h`,网上也有些方法,改了之后还是会有一堆其他的问题,然后观察别的第三方库发现`cocoapods`可以管理 `react-native-http-cache`,于是依样画葫芦,在 `Podfile` 中加入了 `pod 'RCTHttpCache', :path => '../node_modules/react-native-http-cache/ios'`,但是不如人意,执行 `pod install` 报找不到 `react-native-http-cache` 的错误。
仔细观察别的没问题的第三方库时,发现每个库里会有这样一个文件 `***.podspec`,于是一样的也在 `react-native-http-cache` 加入了这样一个文件,命名为 RCTHttpCache.podspec`,我观察看应该文件名是这个依赖的项目名,然后里面还需要填入一些内容,于是继续仿写

```
require "json"

package = JSON.parse(File.read(File.join(__dir__, "../package.json")))
version = package["version"]
giturl = package["repository"]["url"]
bugsurl = package["bugs"]["url"]

Pod::Spec.new do |s|
  s.name         = "RCTHttpCache"
  s.version      = version
  s.summary      = "react-native-http-cache"
  s.description  = <<-DESC
                      Control http cache used by fetch/XMLHttpRequest
                   DESC
  s.homepage     = giturl
  s.license      = "MIT"
  # s.license    = { :type => "MIT" }
  s.author       = { "tdzl2003" => "git+https://github.com/reactnativecn/react-native-http-cache.git" }
  s.platform     = :ios, "8.0"
  s.source       = { :git => giturl, :tag => version }
  s.source_files  = "RCTHttpCache/*.{h,m}"
  s.requires_arc = true


  s.dependency "React"
  #s.dependency "others"

end
```

观察看
`s.name` 及为 依赖中 ios 的项目文件名
`s.version` 为 版本号
`v.summary` 
`s.description` 为描述
`s.homepage` 
`s.license` 
`s.author` 为 作者
`s.platform` 为 运行系统
`s.source`
`s.source_files` 为 加载的代码文件
`s.requires_arc` 
`s.dependency` 为 依赖的第三方库

这些东西有些不填似乎也没事,我把这个文件拷贝到依赖的ios文件下面,然后执行`pod install` 成功的导入了 `RCTHttpCache`

***注意这里是手动加入的这个文件,当执行yarn操作别的依赖的时候回删掉该文件,可以做好文档记录,备份一份该文档,或者`fork`一份代码,加入文件,重新的地址安装该依赖***