IOS App打包發佈完整流程

註冊成爲開發者

登陸蘋果開發者中心,點擊Accounts,在這裏須要你填寫你的Appple ID進行登陸,若是沒有,點擊這裏申請一個,填寫信息就成,這裏就再也不贅述。申請完成以後,使用申請的AppID進行登陸。html

login

進入主頁以後,而後點擊Join the Apple Developer Program進行開發者帳號申請。點擊Enrollreact

enroll

這裏有幾個點須要說明下:android

  • 蘋果的開發者帳號主要分爲我的、組織(公司/企業)、教育機構ios

    類型 費用 是否支持App Store上架 最大支持uuid數量 開發人數 備註
    我的(Individual) $99 支持 100 1 能夠轉爲公司帳號
    組織(Organization) $99 支持 100 多人 申請時須要填寫公司的鄧白氏編碼(DUNS Number)
    企業程序(Enterprise Program) $299 不支持 不限 多人 申請時須要填寫公司的鄧白氏編碼(DUNS Number)

點擊Start Your Enrollment,目前申請開發者帳號,須要在AppStore下載 Apple Developer而後進行申請。git

register

點擊如今註冊,按照程序一步步填寫信息,最後點擊訂閱便可。github

subscription

通常狀況下,訂閱成功須要兩天時間,請耐心等待。shell

Certificates、p12以及provisioning Profiles

小白(在沒有查閱資料以前我也是😺)最初看到這三個名詞的第一反應多是:證書我聽過,可是其餘兩個是個什麼東西,我倒不太清楚。api

這三個文件是作什麼的,暫時能夠不用瞭解,如今只須要記住的是:在ios打包的時候須要用到。下面是一個比較完整的ios打包流程圖,能夠提早幫助理解。xcode

overview

注:以上圖片來自於https://reactorapps.io/blog/ios-certificate-guide/ruby

下面,我就用小白的視角來說講我是怎麼認識這三個東西的。

證書(Certificates)

證書是由公證處或認證機關開具的證實資格或權力的證件,它是代表(或幫助判定)事理的一個憑證。證件或憑證的尾部一般會烙印公章

A certificate is a document that Apple issues to you. This certificate states that you are a trusted developer and that you are in fact, who you claim to be, rather than someone else posing as you.

證書的最主要功效就是證實你是誰以及你是否合法,就像通常開一家餐館同樣,你須要拿到一個營業執照,顧客也就認這個,這裏申請的證書是蘋果公司頒發的。

那麼怎麼申請證書?

網上關於這方面的資料不少,這裏不在贅述,能夠查看手把手教你搞定ios的證書申請

  • 生成Certificate Signing Request (CSR),填寫相關的信息後,保存到本地,會生成一個以.certSigningRequest結尾的CSR文件

    A CSR or Certificate Signing request is a block of encoded text that is given to a Certificate Authority when applying for a certificate.

Keychain Access

  • 在蘋果開發者中心中建立一個Certificate
  • 上傳在本地生成的CSR文件,下載證書
  • CSR文件中包含一個public key,這個key也會包含在生成的證書中,同時還會在keychian中生成一個private key

非對稱加密(Asymmetric cryptography)

Public-key cryptography, or asymmetric cryptography, is a cryptographic system that uses pairs of keys: public keys, which may be disseminated widely, and private keys,which are known only to the owner.

一個簡單的例子,圖片來自於https://en.wikipedia.org/wiki/Public-key_cryptography#Examples

Asymmetric cryptography

一樣仍是以開餐館的例子來說,當餐館越作越大,須要採購一大批原材料的時候,這時候就須要請示老闆了,老闆拿了採購單審查了以後,以爲沒啥問題,而後就會在採購單上簽名。採購員拿着有老闆簽名的採購單,就去採購貨物去了。這裏面有幾個關鍵點:

  • 採購員只有在見到有老闆簽名的單子才認爲是老闆下達的命令
  • 任何僞造的、模仿的或者不是老闆簽字的一概不具備效益
  • 採購員在心中已經造成了一個老闆簽名的樣板

其實這就是一個非對稱加密的例子,老闆的簽名樣板其實就是一個公鑰(public key),餐館中的任何人均可以知道,而真實的老闆簽名字樣便是私鑰(private key),這個簽名手法是老闆獨有的。只有當通過簽名以後的採購清單和簽名樣板進行匹配(這裏的匹配其實就是使用公鑰解密簽名以後的內容)以後,纔會認爲這個採購單具備效益。

那麼同理,怎麼認爲App是你獨有的呢?就須要在發佈的時候,對App進行私鑰加密,便是數字簽名

P12

P12文件中包含了你的證書(包含了你的公鑰)和你的私鑰。

當你的App須要簽名的時候,P12文件會一分爲二。私鑰用來作數字簽名,公鑰會打包進入你的app中,這樣用戶就能夠根據app中的公鑰來驗證你的app的真實性。

獲取p12文件

咱們能夠從下載下來的證書中導出p12文件。

12.png

選擇Export,其間會要求你輸入密碼。

描述文件(provisioning Profiles)

簡單來講,描述文件其實就是一份清單,包含了App中的一些必要信息,主要包括

  • AppId,即爲Bundle identifier,惟一,一般以reverse domain name的形式出現,例如org.reactjs.native.example.TryWithReactNative
  • 證書(Development Certificates),打包App時生成的證書
  • Device UUid,設備的編號,規定了打出來的這個包只能由哪些設備使用(Distrubtion Provisioning Profiles中不包含Device id)

Provisioning Profiles

Provisioning Profiles分爲兩種,一種用於Development模式,能夠供多人測試使用;一種用於Distribution模式,用於上傳App Store。兩種文件中的區別是,Distribution Provisioning Profiles中不包含device id。

AdHoc

注:以上圖片來自於https://medium.com/@jadhavrajashri49/provisioning-profile-67fad1907694

怎麼建立Provisioning Profiles?

關於怎麼建立,以及建立不一樣模式下的Provisioning Profiles,能夠參看證書(Certificate)與描述文件(Provisioning Profiles),這裏再也不贅述。

證書建立完成後須要把描述文件手動下載到本機

找到你要使用的描述文件(開發者、發佈)單擊後顯示以下內容,單擊「Download」後保存到 「/Users/rongshenglai/Library/MobileDevice/Provisioning Profiles」 目錄中注意每一個人的我的目錄不一樣根據狀況修改。

下載的文件名相似「XXXX.mobileprovision」 前邊的XXXX記錄下來它就是描述文件名,使用時不要帶.mobileprovision

如何使用Xcode + personal certificates真機調試

若是須要真機調試,可是又沒法獲取蘋果開發者中心頒發的證書,那麼可使用xcode + 免費的開發者帳號進行personal的證書申請。具體操做以下:

打開xcode,點擊Preferences選擇Accounts

Accounts

點擊左下角+號,使用Apple Id建立一個新的帳戶。

New Account

Apple ID中填寫本身在蘋果這開發中心申請的帳號,完成後點擊Manage Certificates

Manage Certificates

點擊坐下角+號,建立一個證書便可,完成以後點擊Done

Add Certficate

回到xcode的工程目錄下,在Signing & Capabilities面板的Team下,選擇剛剛建立的Team

Team

而後數據線連上真機,點擊Run便可。

Run

最後一步,則是在手機設置 -> 通用 -> 設備管理中,將未受信任的App置爲信任便可。

fastlane自動化打包上傳

上面講解了怎麼生成certificates、p12以及provisioning profiles,有了這三個文件,如今就能夠來打包發佈了。這裏採用的是fastlane。

網上關於fastlane上的教程不少,這裏只是簡單介紹。

fastlane是一個針對iOS和Android應用的Continuous Delivery工具集。可以自動化測試、截圖以及管理你的provisioning profiles,爲你打包上傳應用節省了不少時間。

fastlane is a tool for iOS and Android developers to automate tedious tasks like generating screenshots, dealing with provisioning profiles, and releasing your application.

fastlane

注:圖上的相關stage在最新的fastlane版本中可能有變化,以官網爲準。

基本安裝

安裝最新的xcode命令行工具

xcode-select --install
複製代碼

安裝fastlane

# Using RubyGems
sudo gem install fastlane -NV
 # Alternatively using Homebrew
brew install fastlane
複製代碼

初始化項目

fastlane init
複製代碼

若是你選擇了下載已經存在app的元數據,下面是生成的structure

structure

工具集

到目前爲止,Fastlane的工具集大約包含180多個小工具,基本上涵蓋了打包、簽名、測試、部署、發佈、庫管理等等移動開發中涉及到的內容。另外Fastlane自己強大的Action和Plugin機制,可以使你輕鬆地寫出你本身想要的工具。

tools

代碼簽名(Codesigning)

打包ios以前,最主要的就是要進行代碼簽名,這也是這篇文章上面講解的內容。這裏主要有幾種方式:

  • cert & sign

    • cert會保證你能建立一個合法的證書而且將private key存儲在keychain中

    • sign會保證你能根據你的證書建立一個合法的provisioning profiles

    • certget_certificates action的別名,signget_provisioning_profile action的別名

    • lane :beta do
        get_certificates           # invokes cert
        get_provisioning_profile   # invokes sigh
        build_app
      end
      複製代碼
  • match

    • A new approach to iOS code signing: Share one code signing identity across your development team to simplify your codesigning setup and prevent code signing issues.

      match is the implementation of the codesigning.guide concept. match creates all required certificates & provisioning profiles and stores them in a separate git repository, Google Cloud, or Amazon S3. Every team member with access to the selected storage can use those credentials for code signing. match also automatically repairs broken and expired credentials. It's the easiest way to share signing credentials across teams

    • 官方推薦的形式

    • 使用git/cloud的形式管理證書

    • 可以自動修復和過時的證書

    • 方便在組內分享、管理

    • matchsync_code_signingactoin的別名

    • lane :grant do |options|
        register_devices(devices_file: "./devices.txt")
        match(
          git_url: "git@xxx/certificates.git",
          type: "development",
          force_for_new_devices: true,
          app_identifier: ["org.reactjs.native.example.TryWithReactNative"]
        )
      end
      複製代碼

開發打包(Beta Deployment)

若是證書已經搞定,下面就要使用build_app(gym)打開發包進行測試了

lane :beta do
  sync_code_signing(type: "development")    # see code signing guide for more information
  build_app(scheme: "TryWithReactNative")
  upload_to_testflight
  slack(message: "Successfully distributed a new beta build")
end
複製代碼

打包完成以後,能夠上傳到預發佈平臺進行測試。這裏有幾個推薦:

  • testflight(upload_to_testflight)

  • appcenter,在github中打開

    • fastlane插件

    • # install
      fastlane add_plugin appcenter
      
      # basic usage
      appcenter_upload(
        api_token: "<appcenter token>",
        owner_name: "<appcenter account name of the owner of the app (username or organization URL name)>",
        owner_type: "user", # Default is user - set to organization for appcenter organizations
        app_name: "<appcenter app name (as seen in app URL)>",
        file: "<path to android build binary>",
        notify_testers: true # Set to false if you don't want to notify testers of your new release (default: `false`)
      )
      複製代碼
  • payer 蒲公英,在github中打開

    • fastlane插件

    • # install 
      fastlane add_plugin pgyer
      
      # basic usage
      lane :beta do
        gym
        pgyer(
          api_key: "7f15xxxxxxxxxxxxxxxxxx141",
          user_key: "4a5bcxxxxxxxxxxxxxxx3a9e",
        )
      end
      複製代碼

生產打包(App Store Deployment)

相似於開發打包過程,不過這裏是要上傳到蘋果app store中,在此以前記得切換生產發佈包的provisioning profiles

lane :release do
  capture_screenshots                  # generate new screenshots for the App Store
  sync_code_signing(type: "appstore")  # see code signing guide for more information
  build_app(scheme: "TryWithReactNative")
  upload_to_app_store                  # upload your app to App Store Connect
  slack(message: "Successfully uploaded a new App Store build")
end
複製代碼

結束語

至此,整個App從註冊、打包到發佈就已經徹底連成一條線了。對於新手小白來講確時是不太容易,至於最後關於fastlane講解的相關部分,由於本身並無我的帳號,採用的是公司生成的證書,因此也沒辦法親自操做截圖,若是有紕漏,敬請諒解。

參考資料

相關文章
相關標籤/搜索