Post

pod与spm的比较与区别

参考:

前言

最近在搞亚马逊人脸识别SDK接入,想把spm的方式改成pod的方式,改造起来十分痛苦都快抑郁了,特研究学习下AFNetworking的写法

一、Pod的配置

pod_spm_afn1.jpeg

AFNetworking里一个主pod与多个子pod的写法

1、主项目的源代码:s.source_files = 'AFNetworking/AFNetworking.h'
2、子项目的源代码管理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
s.subspec 'Serialization' do |ss|
  ss.source_files = 'AFNetworking/AFURL{Request,Response}Serialization.{h,m}'
end

s.subspec 'Security' do |ss|
  ss.source_files = 'AFNetworking/AFSecurityPolicy.{h,m}'
end

s.subspec 'Reachability' do |ss|
  ss.ios.deployment_target = '9.0'
  ss.osx.deployment_target = '10.10'
  ss.tvos.deployment_target = '9.0'

  ss.source_files = 'AFNetworking/AFNetworkReachabilityManager.{h,m}'
end

3、如果子项目依赖其他子项目,可以正常使用依赖 3.1 NSURLSession子项目依赖AFNetworking/SerializationAFNetworking/ReachabilityAFNetworking/Security

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
s.subspec 'NSURLSession' do |ss|
  ss.dependency 'AFNetworking/Serialization'
  ss.ios.dependency 'AFNetworking/Reachability'
  ss.osx.dependency 'AFNetworking/Reachability'
  ss.tvos.dependency 'AFNetworking/Reachability'
  ss.dependency 'AFNetworking/Security'

  ss.source_files = 'AFNetworking/AF{URL,HTTP}SessionManager.{h,m}', 'AFNetworking/AFCompatibilityMacros.h'
end
3.2 `UIKit`子项目依赖`AFNetworking/NSURLSession`
# UIKit子项目依赖NSURLSession子项目
# 代码路径是UIKit+AFNetworking
s.subspec 'UIKit' do |ss|
  ss.ios.deployment_target = '9.0'
  ss.tvos.deployment_target = '9.0'
  ss.dependency 'AFNetworking/NSURLSession'

  ss.source_files = 'UIKit+AFNetworking'
end

完整的配置pod配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Pod::Spec.new do |s|
  s.name     = 'AFNetworking'
  s.version  = '4.0.1'
  s.license  = 'MIT'
  s.summary  = 'A delightful networking framework for Apple platforms.'
  s.homepage = 'https://github.com/AFNetworking/AFNetworking'
  s.social_media_url = 'https://twitter.com/AFNetworking'
  s.authors  = { 'Mattt Thompson' => 'm@mattt.me' }
  s.source   = { :git => 'https://github.com/AFNetworking/AFNetworking.git', :tag => s.version }

  s.ios.deployment_target = '9.0'
  s.osx.deployment_target = '10.10'
  s.watchos.deployment_target = '2.0'
  s.tvos.deployment_target = '9.0'

  s.ios.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking' }
  s.osx.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking' }
  s.watchos.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking-watchOS' }
  s.tvos.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking' }

  s.source_files = 'AFNetworking/AFNetworking.h'

  s.subspec 'Serialization' do |ss|
    ss.source_files = 'AFNetworking/AFURL{Request,Response}Serialization.{h,m}'
  end

  s.subspec 'Security' do |ss|
    ss.source_files = 'AFNetworking/AFSecurityPolicy.{h,m}'
  end

  s.subspec 'Reachability' do |ss|
    ss.ios.deployment_target = '9.0'
    ss.osx.deployment_target = '10.10'
    ss.tvos.deployment_target = '9.0'

    ss.source_files = 'AFNetworking/AFNetworkReachabilityManager.{h,m}'
  end

  s.subspec 'NSURLSession' do |ss|
    ss.dependency 'AFNetworking/Serialization'
    ss.ios.dependency 'AFNetworking/Reachability'
    ss.osx.dependency 'AFNetworking/Reachability'
    ss.tvos.dependency 'AFNetworking/Reachability'
    ss.dependency 'AFNetworking/Security'

    ss.source_files = 'AFNetworking/AF{URL,HTTP}SessionManager.{h,m}', 'AFNetworking/AFCompatibilityMacros.h'
  end

  s.subspec 'UIKit' do |ss|
    ss.ios.deployment_target = '9.0'
    ss.tvos.deployment_target = '9.0'
    ss.dependency 'AFNetworking/NSURLSession'

    ss.source_files = 'UIKit+AFNetworking'
  end
end


一、SPM的配置

AFNetworking的Package\library\target的名字都是AFNetworking,路径为AFNetworking文件夹,从这个配置可知SPM与Pod相比不包含UIKit+AFNetworking
Package->products->.library->targets->.target->path
如果没有在.target里配置path,则默认代码文件夹为:Sources

完整配置如下Package.swift,:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// swift-tools-version:5.0

import PackageDescription

let package = Package(name: "AFNetworking",
                      platforms: [.macOS(.v10_10),
                                  .iOS(.v9),
                                  .tvOS(.v9),
                                  .watchOS(.v2)],
                      products: [.library(name: "AFNetworking",
                                          targets: ["AFNetworking"])],
                      targets: [.target(name: "AFNetworking",
                                        path: "AFNetworking",
                                        publicHeadersPath: "")])

pod_spm_afn2.jpeg

This post is licensed under CC BY 4.0 by the author.