美文网首页
从零开始,自己鼓捣一套ShareSDK组件

从零开始,自己鼓捣一套ShareSDK组件

作者: 红发_KVO | 来源:发表于2022-06-11 15:13 被阅读0次

    公司有个小插曲之后,要我做一个自己的shareSDK辣样子的东东,一开始真的觉得鸭梨山大。我是谁?我在哪?我要干什么?我只是一个卑微的UI仔啊,不应该啊,实在不应该啊。但是作为一个快四十岁的萌新,没办法啊,只能硬着头皮上了。。。


    一、先搭建自己的Pod多组件调试项目

    • 调试工程的pod文件编写
    platform :ios, '12.0'
    target 'HFKitTest' do
      use_frameworks!
      # Pods for HFKitTest
      pod "AFNetworking"
      pod 'SVProgressHUD'
      pod 'Toast-Swift'
      pod 'YYModel'
      pod 'Masonry'
      pod 'IQKeyboardManager'
    
      pod 'HFKit', :path => '../HFKit.podspec'
    
      pod 'FacebookComm', :path => '../FacebookComm.podspec'
      
      pod 'AppleComm', :path => '../AppleComm.podspec'
      
      pod 'GooglePlusComm', :path => '../GooglePlusComm.podspec'
      
    end
    

    主要看下面本地库的依赖,主组件是HFKit,后面三个是国外主平台的中间件连接库。每个库单独依赖,写成这样子是因为,主framework下面的子framework不能设置为s.static.framework = true

    • 本地Pod子库的podspec编写
    Pod::Spec.new do |s|
      s.name             = 'FacebookComm'
      s.version          = '1.0.O'
      s.summary          = 'FaceSDK中间层'
    
      s.homepage         = 'https://www.mini1.cn'
      s.author           = { 'KVO' => 'handsonwu.tan@gmail.com' }
      s.source           = { :git => 'git@xxxx.git', :tag => s.version.to_s }
      s.static_framework = true
      s.ios.deployment_target = '12.0'
      s.frameworks = 'Foundation', 'UIKit'
      s.source_files = 'FacebookComm/**/*.{h,m}'
      s.prefix_header_file = false
      s.library = 'c++', 'stdc++','z'
      s.vendored_frameworks = 'HFKit/**/ShareSDK/PlatformSDK/FacebookSDK/*.framework'
      s.ios.weak_framework = 'Accelerate', 'Accounts', 'CoreLocation', 'Social', 'Security', 'QuartzCore', 'CoreGraphics','UIKit', 'AudioToolbox','AuthenticationServices','SafariServices','CoreServices'
      
      s.resources = 'HFKit/**/ShareSDK/PlatformSDK/FacebookSDK/*.bundle'
      s.pod_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
      s.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
      
      s.public_header_files = 'FacebookComm/**/{FacebookCommunication}.h'
      
      s.dependency 'HFKit'
      
    end
    

    主要主要就是s.vendored_frameworks,s.resources,s.dependency这个三个参数的配置,我这边依赖本地静态库的方式所以设置了s.vendored_frameworks,其实也可以改用s.dependency直接依赖FBSDKLoginKit。这里只是写了一个podspec的配置,其它也大致如此~

    这里调试库的搭建方式大致就完了,后面就是具体的编写业务逻辑代码和调试了。大家可能很奇怪为什么要这么搭建,会不会太过于多此一举,主要是因为我们的库只提供.framework依赖,不可以暴露源码。。。

    二、创建自己的公共Pod库,让内部可以方便的集成

    • 注册cocoapod的用户名和邮箱
      1、 先查看自己是否绑定过cocoapods账号

       $ pod trunk me
      

      如果终端输入以下的样式,就表示已绑定,不然就是未绑定

          xxxxx@admindeMac-mini TestPod % pod trunk me
          - Name:     Taki
          - Email:    xxxxxx@xxxx.cn
          - Since:    June 8th, 21:07
          - Pods:
          - xxxxxx
          - Sessions:
          - June 8th, 21:07 - October 16th, 21:54. IP: 183.240.204.116 Description: xxxxxxx
          xxxxx@admindeMac-mini TestPod % 
      

      如果是未绑定,执行如下指定进行绑定

      $ pod trunk register your-email 'your user name' --description='your description'
      

      成功之后你会看到以下的提示,并且你会收到一封验证邮件,点击之后就可以进行验证。(一定要点击,虽然可能会告诉你404之类)

      [!] Please verify the session by clicking the link in the verifiacation 
      email that has been sent to xxxxxxx@xxxx.com
      

      再次使用 pod trunk me指令来验证下是否绑定成功

    • 在本地创建一个pod库,我们只是需要它自定生成的podspec文件而已

      • 1.自己找个管理cocoapods的文件夹
      xxx@admindeMac-mini ~ % cd Desktop/xxx/Cocoapods 
      xxx@admindeMac-mini Cocoapods % ls
      HFKit
      xxx@admindeMac-mini Cocoapods % pod lib create mini_mnwsdk
      
      • 2.执行完上述命令后,会依次弹出以下的问题
      What platform do you want to use?? [ iOS / macOS ]
       > iOS
      What language do you want to use?? [ Swift / ObjC ]
       > ObjC
      Would you like to include a demo application with your library? [ Yes / No ]
       > Yes
      Which testing frameworks will you use? [ Specta / Kiwi / None ]
       > None
      Would you like to do view based testing? [ Yes / No ]
       > No
      What is your class prefix?
       > HF
      
      • 3.完成以上问题后,终端会输入以下东西,并且会打开项目
      hint: Using 'master' as the name for the initial branch. This default branch     name
      hint: is subject to change. To configure the initial branch name to use in all
      hint: of your new repositories, which will suppress this warning, call:
      hint: 
      hint:  git config --global init.defaultBranch <name>
      hint: 
      hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
      hint: 'development'. The just-created branch can be renamed via this command:
      hint: 
      hint:   git branch -m <name>
      
      Running pod install on your new library.
      
      Analyzing dependencies
      Downloading dependencies
      Installing HFKit (0.1.0)
      Generating Pods project
      Integrating client project
      
      [!] Please close any current Xcode sessions and use `HFKit.xcworkspace` for this project from now on.
      Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
      
      [!] Your project does not explicitly specify the CocoaPods master specs repo.Since CDN is now used as the default, you may safely remove it from your repos   directory via `pod repo remove master`. To suppress this warning please add   `warn_for_unused_master_specs_repo => false` to your Podfile.
      
       Ace! you're ready to go!
       We will start you off by opening your project in Xcode
       open 'HFKit/Example/HFKit.xcworkspace'
      
      To learn more about the template see `https://github.com/CocoaPods/pod-    template.git`.
      To learn more about creating a new pod, see `https://guides.cocoapods.org/making/making-a-cocoapod`.
      
        1. 然后配置我们的HFKit.podspec文件
      #
      # Be sure to run `pod lib lint mini_mnwsdk.podspec' to ensure this is a
      # valid spec before submitting.
      #
      # Any lines starting with a # are optional, but their use is encouraged
      # To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
      #
      
      Pod::Spec.new do |s|
        s.name             = 'mini_hfkit'
        s.version          = '0.1.1'
        s.summary          = 'HFKit is use to easily login with xxx and other plaforms'
      
      # This description is used to generate tags and improve search results.
      #   * Think: What does it do? Why did you write it? What is the focus?
      #   * Try to keep it short, snappy and to the point.
      #   * Write the description between the DESC delimiters below.
      #   * Finally, don't worry about the indent, CocoaPods strips it!
      
        s.description      = <<-DESC
                        HFKit is make to login easily for xxxx and other plaforms
                        like: apple...
                         DESC
      
        s.homepage         = 'https://github.com/xxxx/HFKit'
      # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
        s.license          = { :type => 'MIT', :file => 'LICENSE' }
        s.author           = { 'Taki' => 'xxxx@xxxx.cn' }
        s.source           = { :git => 'https://github.com/xxxxx/HFKit.git', :tag => s.version.to_s } #这个地址要
       #s.plaform = :iOS
        s.ios.deployment_target = '12.0'
        s.default_subspecs = 'HFKit'
        #主模块
        s.subspec 'HFKit' do |sp|
            sp.frameworks = 'Foundation'
            sp.vendored_frameworks = 'HFKit/HFKit.framework'
            sp.pod_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
            sp.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
        end
        s.subspec 'HFKitPlatforms' do |sp|
          #Apple
            sp.subspec 'Apple' do |ssp|
                ssp.vendored_frameworks = 'HFKit/PlatformComm/AppleComm.framework'
                ssp.ios.weak_framework = 'AuthenticationServices'
                ssp.dependency 'mini_hfkit/HFKit'
                ssp.pod_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
                ssp.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
            end
        end
      end
      
      

    三、去GitHub上创建一个仓库

    • 1.如下图创建一个新仓库

      Creat HFKit repository.png
    • 2.接下来把自己本地的工程关联上Github的远程仓库

    //打开当前项目所在文件夹
    $ cd /Users/xxxx/Desktop/Cocoapods/HFKit
    $ git remote add origin 此为GitHub上面的仓库地址(现在push除了ssh方式,还有token方式,具体自己百度,主要是针对多个ssh key配置导致的)
    $ git add .
    $ git commit -m "first commit"
    $ git push -u origin master
    
    终端出现该输出,标识上传成功
    To https://github.com/xxxx/HFKit.git
     * [new branch]      master -> master
    Branch 'master' set up to track remote branch 'master' from 'origin'.
    
    • 3.验证本地的podspec文件,看是否验证通过
    xxxx@admindeMac-mini HFKit % pod lib lint HFKit.podspec 
    
     -> HFKit (0.1.0)
        - WARN  | summary: The summary is not meaningful.
        - WARN  | url: There was a problem validating the URL https://github.com/xxxxx/HFKit.
        - NOTE  | xcodebuild:  note: Using new build system
        - NOTE  | xcodebuild:  note: Using codesigning identity override: -
        - NOTE  | xcodebuild:  note: Build preparation complete
        - NOTE  | [iOS] xcodebuild:  note: Planning
        - NOTE  | [iOS] xcodebuild:  note: Building targets in dependency order
    
    [!] HFKit did not pass validation, due to 2 warnings (but you can use `--allow-warnings` to ignore them).
    You can use the `--no-clean` option to inspect any issue.
    
    看到上面的报错提示,只需要按照它的指引, 再指令后面加`--allow-warnings`
    xxxx@admindeMac-mini HFKit % pod lib lint HFKit.podspec --allow-warnings
     'HFKit passed validation.' 出现这个表示成功了
    
    • 4. 把自己编写的库文件啊,配置文件啊,一股脑的推送到远程仓库,并且打上版本tag
    $ git add .
    $ git commit -m "添加测试代码"
    $ git push
    '发布项目到Cocoapods上去需要添加版本号的,并创建同名分支branch,用如下指令添加版本号'
    //添加版本号
    $ git tag -a 0.1.0 -m "开始我的冈本0.1之旅"
    //创建同名分支
    $ git branch 0.1.0
    //查看版本号
    $ git tag
    //将本地的添加的tag同步到远程代码仓库
    $ git push --tag
    
    • 5.验证本地的podspec文件的合法性和正确性
      • 1.打开podspec所在文件目录
      xxxx@admindeMac-mini Cocoapods % cd HFKit 
      
      • 2.使用验证命令
      xxxx@admindeMac-mini HFKit % pod spec lint HFKit.podspec 
      'HFKit.podspec passed validation.' 看到这个说明成功,如果有提示错误,还是按照 `pod spec lint HFKit.podspec  --allow-warnings`
      
    • 6.发布Pod库,就是提交podspec文件到Spec仓库
    //HFKit.podspec 就是自己的podspec文件名称
    $ pod trunk push HFKit.podspec 
    
    出现如下日志就表示发布成功了
    Updating spec repo `trunk`
    Validating podspec
    Updating spec repo `trunk`
    
    --------------------------------------------------------------------------------
     🎉  Congrats
    
     🚀  HFKit (0.1.0) successfully published
     📅  Jun 10st, 16:51
     🌎  https://cocoapods.org/pods/HFKit
     👍  Tell your friends!
    
    '如果出现任何警告,验证都是不通过的,会导致发布失败!我们也可以继续用如下命令`pod trunk push HFKit.podspec --allow-warnings`'
    '这样子创建和发布就结束了,你注册的邮箱也会收到发布成功的邮件,可以使用了'
    
    • 7.验证pod和使用
      我们可以使用pod search HFKit的指令来验证自己是否发布功能
    pod search HFKit
    '如果搜索不到的话,不要着急,可以先试着移除索引'
    `rm ~/Library/Caches/CocoaPods/search_index.json`
    再次搜索时,会自己重新创建索引
    '还搜索不到的话,可以先执行`pod update` 来更新库'
    '依然搜索不到的话,那就放弃吧,不要纠结了,直接新建一个工程’
    导入`pod HFKit` 然后pod install吧!这时候不行的话你就要打我了,可是打不着。对咯,这里的HFKit是代号(全程要换成你自己的库)
    

    四、自动生成你的framework的注释使用文档

    这边,我是借鉴别人,使用的appledoc这个库,官方的DocC我看了半天都看明白,用它的指令也没生成。不知道是不是只针对swift有效~还请高人指定,让我这个逼能装到。。。

    • 1.安装appledoc
    git clone https://github.com/tomaz/appledoc.git
    cd 到你的appledoc的目录下
    sudo sh install-appledoc.sh
    '执行下面命令看是否安装成功'
    appledoc --version
    
    • 2.使用appledoc
    cd 你的framework项目路径
    

    使用命令行创建docset-install.txt,安装到当前项目中,也可以指定路径

    appledoc --output ./doc --project-name 你的项目名字 --project-company "你的公司名字" --company-id "你的公司id啥的" ./
    '记住,最后的./一定不能少,不然会报缺少一个路径'
    

    成功之后会显示如下:


    生成了docset-install.txt.png

    接着,我们在调用下面的指令:

    appledoc --no-create-docset  --output ./doc --project-name 你的项目名字 --project-company "你的公司名字" --company-id "你的公司id啥的" ./
    '记住,最后的./一定不能少,不然会报缺少一个路径'
    

    成功之后会显示如下:


    生成doc的html.png

    打开我们的index看一看,用浏览器打开:


    生成的文档的所有头文件png 单个头文件的注释.png

    总结:一套下来,就是生成自己pod库的组合拳了,当然,具体的用法,还是要在readme里面写一下的。今天不开心到此为止吧,明天依旧光芒万丈哦!(还有一些奇淫巧技,例如如果把别人的源码库,通过pod的形式,打成静态库,自己内部引用,还可以隐藏内部引用库等等就不细说了,说多了都是泪!)

    相关文章

      网友评论

          本文标题:从零开始,自己鼓捣一套ShareSDK组件

          本文链接:https://www.haomeiwen.com/subject/huiimrtx.html