iOS开发证书要点详解

引言

        关于开发证书配置(Certificates&Identifiers&Provisioning Profiles),相信做iOS开发的同学没少被折腾。对于一个iOS开发小白、半吊子(比如像我自己)抑或老兵,或多或少会有以下不详、疑问、疑惑甚至困惑:

  1. 什么是App ID?Explicit/Wildcard App ID有何区别?什么是App Group ID?
  2. 什么是证书(Certificate)?如何申请?有啥用?
  3. 什么是Key Pair(公钥/私钥)?有啥用?与证书有何关联?
  4. 什么是签名(Signature)?如何签名(CodeSign)?怎样校验(Verify)?
  5. 什么是(Team)Provisioning Profiles?有啥用?
  6. Xcode如何配置才能使用iOS真机进行开发调试?
  7. 多台机器如何共享开发者账号或证书?
  8. 遇到证书配置问题怎么办?

本文将对相关概念做个系统的梳理串烧。

写在前面

1.假设你使用过Apple设备(iMac/iPad/iPhone)且注册过Apple ID(Apple Account)。

2.假设你或你所在的开发组已加入苹果开发者计划(Enroll in iOS Developer Program to become a member),即已注册开发者账号(Apple Developer Account)。

  • 只有拥有开发者账号,才可以申请开发/发布证书及相关配置授权文件,进而在iOS真机上开发调试Apps或发布到App Store。
  • 开发者账号分为Individual和Company/Organization两种类型。如无特别交代,下文基于$99/Year的普通个人开发者(Individual)账号展开

3.如果要真机调试实践,你必须至少拥有一台装有Mac OS X的Mac开发机(iMac or MacBook),其上自带原生的Keychain Access,并且你需要下载Xcode。

.App ID(bundle identifier)

App ID即Product ID用于标识一个或者一组App,App ID应该和Xcode中的Bundle Identifier是一致的或匹配的。

App ID字符串通常以反域名(reverse-domain-name)格式的Company Identifier(Company ID)作为前缀(Prefix/Seed),一般不超过255个ASCII字符。

App ID全名会被追加ApplicationIdentifierPrefix(一般为TeamID.),分为两类:

  • Explicit App ID:唯一的App ID,这种App ID用于唯一标识一个应用程序。例如“com.apple.garageband”这个App ID,用于标识Bundle Identifier为“com.apple.garageband”的程序。
  • Wildcard App ID:通配符App ID,用于标识一组应用程序。例如“*”(实际上是ApplicationIdentifierPrefix)表示所有应用程序;而“com.apple.*”可以表示以“com.apple.”开头的所有应用程序。
用户可在网站上删除(Delete)已注册的App IDs。
App ID被配置到XcodeTarget|Info|Bundle Identifier】下;对于Wildcard App ID,只要bundle identifier包含其作为Prefix/Seed即可。

.设备(Device)

Device就是运行iOS系统用于开发调试App的设备,每台设备使用UDID来唯一标识。iOS设备连接Mac后,可通过iTunes->Summary或者Xcode->Window->Devices获取iPhone的UDID(identifier)。

Apple Member Center网站个人账号下的Devices中包含了注册过的所有可用于开发和测试的设备。普通个人开发账号每年累计最多只能注册100个设备,用户可在网站上启用/禁用(Enable/Disable)已注册的Device。

  • Apps signed by you or your team run only on designated development devices.
  • Apps run only on the test devices you specify.
本文的Devices即连接到Xcode被授权用于开发测试的iOS设备(iPhone/iPad)。

.证书(Certificates)

顾名思义,证书是用来证明内容(App的executalbe code)的合法性和完整性的。对于想安装到真机或发布到AppStore的应用程序(App),只有经过签名验证(Signature Validated)才能确保来源可信,并且保证App内容是完整、未经篡改的。

证书分为两类:DevelopmentProduction(Distribution)

  • Development证书用来开发和调试应用程序:development certificate identifies you, as a team member, in a development provisioning profile that allows apps signed by you to launch on devices. 
  • Production主要用来分发应用程序(根据证书种类有不同作用):distribution certificate identifies your team or organization in a distribution provisioning profile and allows you to submit  your app to the store. Only a team agent or an admin can create a distribution certificate.

普通个人开发账号最多可注册iOS Development/Distribution证书各2个,用户可在网站上删除(Revoke)已注册的Certificate。下文主要针对开发调试阶段的Development证书。

首先,iOS以及Mac OS X系统(在安装Xcode时)将自动安装AppleWWDRCA.cer(Apple Worldwide Developer Relations Certification Authority)这个中间证书(Intermediate Certificates)。它实际上就是iOS证书的CA,其公钥用于解密认证证书的可靠性。如果Mac Keychain Access证书助理在申请证书时尚未安装过该证书,请先下载安装(Signing requires that you have both the signing identity and the intermediate certificate installed in your keychain)。


 

通过Keychain证书助理手动申请开发证书时(也可通过Xcode自动请求生成),keychain将生成一个包含开发者身份信息的CSR(Certificate Signing Request)文件;同时,Keychain Access|Keys中将新增一对Public/Private Key PairThis signing identity consists of a public-private key pair that Apple issues)。


private key用于签名(CodeSign),始终保存在Mac OS的Keychain Access中;public key一般随证书散布出去,对签名进行校验认证。用户必须保护好本地Keychain中的private key,以防伪冒。

  • Keep a secure backup of your public-private key pair. If the private key is lost, you’ll have to create an entirely new identity to sign code. 
  • Worse, if someone else has your private key, that person may be able to impersonate you.

在Apple开发网站上传该CSR文件,Apple证书颁发机构WWDRCA将使用private key对CSR中的public key和一些身份信息进行加密签名生成数字证书(ios_development.cer)并记录在案(Apple Member Center)。

从Apple Member Center网站下载证书到Mac上双击即可安装。证书安装成功后,在 KeychainAccess|Keys中展开创建CSR时生成的Key Pair中的私钥前面的箭头,可以查看到包含其对应公钥的证书(Your requested certificate will be the public half of the key pair.);在Keychain Access|Certificates中展开安装的证书(ios_development.cer)前面的箭头,可以看到其对应的私钥。



Certificate被配置到Xcode Target|Build Settings|Code Signing|Code Signing Identity】下,下拉选择Identities from Profile "..."(一般先配置Provisioning Profile)。

.供应配置文件(Provisioning Profiles

Provisioning Profile文件包含了上述的所有内容:证书、App ID和设备

一个Provisioning Profile对应一个Explicit App ID或Wildcard App ID(一组相同Prefix/Seed的App IDs)。 在网站上手动创建一个Provisioning Profile时,需要依次指定App ID(单选)、证书(Certificates,可多选)和设备(Devices,可多选)。用户可在网站上删除(Delete)已注册的 ProvisioningProfiles。

Prov

posted @ 2016-04-18 15:44  Betterer  阅读(305)  评论(0编辑  收藏  举报