Xamarin.iOS 的手动预配Manual provisioning for Xamarin.iOS

Xamarin.iOS 成功安装后,iOS 开发的下一步是预配 iOS 设备。本指南介绍如何使用手动预配设置开发证书和配置文件。Once Xamarin.iOS has been successfully installed, the next step in iOS development is to provision your iOS device. This guide explores using manual provisioning to set up development certificates and profiles.

备注

此页上的说明与付费访问 Apple 开发者计划的开发人员相关。The instructions on this page are relevant for developers who have paid access to the Apple Developer Program. 如果你有一个免费帐户,请参阅免费预配指南,详细了解基于设备的测试。If you have a free account, please take a look at the Free provisioning guide for more information about on-device testing.

创建签名标识Creating a signing identity

设置开发设备的第一步是创建签名标识。The first step in setting up a development device is to create a signing identity. 签名标识包含以下两项:A signing identity consists of two things:

  • 开发证书A Development Certificate
  • 私钥A private key

开发证书和关联密钥对 iOS 开发者至关重要:它们生成了你的 Apple 标识,并将你与给定设备和配置文件相关联以用于开发,类似于将你的数字签名放入应用程序。Development certificates and associated keys are critical for an iOS developer: they establish your identity with Apple and associate you with a given device and profile for development, akin to putting your digital signature on your applications. Apple 会检查证书以控制对你可以部署的设备的访问。Apple checks for certificates to control access to the devices you are allowed to deploy.

可通过访问 Apple Members Center 的证书、标识符和配置文件(要求登录)部分管理开发团队、证书和配置文件。Development teams, certificates, and profiles can be managed by accessing the Certificates, Identifiers & Profiles (login required) section of Apple's Member Center. Apple 要求你拥有签名标识才能为设备或模拟器生成代码。Apple requires you to have a signing identity to build your code for device or simulator.

重要

请务必注意,任何时候,一次只能有两个 iOS 开发证书。It is important to note that you can only have two iOS Development certificates at any one time. 如果需要创建更多证书,需要撤销现有证书。If you need to create any more, you will need to revoke an existing one. 使用已撤销证书的计算机不能对应用进行签名。Any machine using a revoked certificate will not be able to sign their app.

若要生成签名标识,请执行以下操作:To Generate a signing identity, do the following:

  1. 登录开发人员门户中的证书、标识符和描述文件部分,从“iOS 应用” 列选择“证书” 部分。Login to the Certificates, Identifiers, and Profiles section of the Developer Portal and select the Certificates section from the iOS Apps column. 然后,按“+” 以创建新证书:Then, hit the + to create a new certificate:

  2. 对于证书类型,选择“iOS 应用开发” 选项,然后单击“继续” 。Select the iOS App Development option for the certificate type and click Continue. 此屏幕可能会根据帐户特权有所不同:This screen may look different depending on your account privileges:

  3. 请求“证书签名请求”,上传该请求以手动生成证书。Request a Certificate Signing Request, which will be uploaded to generate a certificate manually. 若要执行此操作,在 Mac 上启动 Keychain AccessTo do this, launch Keychain Access on a Mac. 导航到主菜单,选择“证书助手” 和“向证书颁发机构申请证书...” ,如下所示:Navigate to the main menu, and select Certificate Assistant and Request a Certificate from a Certificate Authority..., as illustrated below:

  4. 填写你的信息,并选择“保存到磁盘” 选项:Fill in your information, and select the option to Save to disk:

  5. 在易于找到的位置保存 CSR:Save the CSR at a location where it can be easily found:

  6. 返回设置门户,将证书上传到门户并提交:Return to the Provisioning Portal, upload the Certificate to the portal, and submit:

    如果没有管理员权限,证书必须由管理员或团队代理批准。If you do not have admin privileges, the Certificate must be approved by an admin or team agent.

  7. 证书经过批准后,从设置门户下载此证书:Once the Certificate is approved, download it from the Provisioning Portal:

  8. 双击下载的证书,启动 Keychain Access 并打开“我的证书” 面板,显示新证书和关联私钥:Double-click on the downloaded Certificate to launch Keychain Access and open the My Certificates panel, showing the new certificate(s), and associated private key:

了解证书密钥对Understanding certificate key pairs

开发人员配置文件包含证书、关联密钥,以及任何与帐户关联的设置配置文件。The Developer Profile contains certificates, their associated keys, and any provisioning profiles associated with the account. 实际上有两个版本的开发者配置文件 — 一个在开发者门户中,另一个在本地 Mac 上。There are actually two versions of a Developer Profile — one is on the Developer Portal, and the other lives on a local Mac. 两者之间的区别在于它们所包含的密钥类型:门户上的配置文件包含与证书关联的所有公钥,而本地 Mac 上的副本包含所有私钥The difference between the two is the type of keys they contain: the Profile on the Portal houses all the public keys associated with your certificates, while the copy on your local Mac contains all the private keys. 要使证书生效,密钥对必须匹配。For the certificates to be valid, the key pairs must match. 在本地 Mac 上保存开发者配置文件的备份,因为如果密钥丢失,需要重新生成所有证书和设置配置文件。Keep a backup of the Developer Profile on the local Mac, because if the private keys are lost, all the certificates and provisioning profiles will need to be regenerated.

警告

丢失证书和关联密的钥会很麻烦,因为需要撤销现有证书并重新设置任何关联的设备,包括为临时部署注册的设备。Losing the certificate and associated keys can be incredibly disruptive, as it will require revoking existing certificates and re-provisioning any associated devices, including those registered for ad-hoc deployment. 成功设置开发证书后,导出备份副本并将其存储在安全位置。After successfully setting up Development Certificates, export a backup copy and store them in a safe place. 有关如何执行此操作的详细信息,请参阅 Apple 文档中维护证书指南的“导出与导入证书和配置文件”部分。For more information on how to do this, refer to the Exporting and Importing Certificates and Profiles section of the Maintaining Certificates guide in Apple's docs.

预配 iOS 设备进行开发Provisioning an iOS Device for development

现在你已经生成 Apple 标识,并且已具有开发证书,现在必须设置预配配置文件和所需实体,以便将应用部署到 Apple 设备。Now that you’ve established your identity with Apple and have a development certificate, you must set up a provisioning profile and the required entities so it is possible to deploy an app to an Apple device. 设备必须运行 Xcode 支持的 iOS 版本 - 可能需要更新设备和/或 Xcode。The device must be running a version of iOS that is supported by Xcode — it may be necessary to update the device, Xcode or both.

添加设备Add a device

创建用于开发的设置配置文件时,必须声明哪些设备可以运行应用程序。When creating a provisioning profile for development, we must state which devices can run the application. 为了实现此目的,开发者门户每个日历年可以添加最多 100 台设备,还可以从这里选择要添加到特定设置配置文件的设备。To enable this, up to 100 devices per calendar year can be added to our Developer Portal, and from here we can select the devices to be added to a particular provisioning profile. 请按照以下步骤在 Mac 上将设备添加到开发人员门户Follow the steps below on your Mac to add a device to the Developer Portal

  1. 启动 Xcode。Start Xcode.

  2. 使用提供的 USB 电缆连接要设置到 Mac 的设备。Connect the device to be provisioned to the Mac with its supplied USB cable.

  3. 从“Windows” 菜单中选择“设备” :From the Windows menu select Devices:

  4. 从“设备”窗口左侧的“设备” 列表选择所需的 iOS 设备。Select the desired iOS device from the DEVICES list on the left side of the Devices Window.

  5. 突出显示“标识符” 字符串并将其复制到剪贴板:Highlight the Identifier string and copy it to the clipboard:

  6. 在 Safari 中,导航到 Apple 开发者中心并登录。In Safari, navigate to the Apple Developer Center and log in.

  7. 单击“证书、标识符和描述文件” 链接:Click the Certificates, Identifiers & Profiles link:

  8. 单击“设备” 链接:Click on the Devices link:

  9. 单击“+” 按钮:Click the + button:

  10. 为新设备提供名称并将前面复制的设备“标识符” 粘贴到“UUID” 字段:Provide a name for the new device and paste the device Identifier that we copied above into the UUID field:

  11. 单击“继续” 按钮。Click the Continue button.

  12. 最后,检查信息并单击“注册” 按钮:Finally, review the information and click the Register button:

对将用于测试或调试 Xamarin.iOS 应用程序的任何 iOS 设备重复上述步骤。Repeat the above steps for any iOS device that will be used to test or debug a Xamarin.iOS application.

将设备添加到开发人员门户后,需要创建设置配置文件并向其添加设备。After adding the device to the developer portal, it is necessary to create a provisioning profile and add the device to it.

创建开发预配配置文件Creating a development provisioning profile

与开发证书一样,预配配置文件可以通过 Apple Members Center 的证书、标识符和配置文件部分手动创建。As with the Development Certificate, Provisioning Profiles can be manually created through the Certificates, Identifiers & Profiles section of Apple's Members Center.

创建预配配置文件以前,应先创建应用 ID 。Before creating a provisioning profile, an App ID must be made. 应用 ID 是反向 DNS 样式字符串,用于唯一标识应用程序。An App ID is a reverse-DNS style string that uniquely identifies an application. 以下步骤演示如何创建通配符应用 ID ,以用于生成和安装大多数应用程序。The steps below will demonstrate how to create a Wildcard App ID, which can be used to build and install most applications. “显式应用 ID” 只允许安装一个应用程序(具有匹配的捆绑 ID),且通常用于某些 iOS 功能,如 Apple Pay 和 HealthKit。Explicit App IDs only allow the installation of one application (with the matching bundle ID), and are generally used for certain iOS features such as Apple Pay and HealthKit. 有关创建“显式应用 ID”的信息,请参阅使用功能指南。For information on creating Explicit App IDs, refer to the Working with Capabilities guide.

应用 IDApp ID

  1. 开发者门户中找到Apple 开发者中心的证书、标识符和描述文件部分。In the developer portal browse to the Certificate, Identifiers and Profiles section in the Apple Developer Center. 在“标识符” 下选择“应用 ID” 。Select App IDs under Identifiers.

  2. 单击“+” 按钮,并提供“名称” :Click the + button and provide a Name:

  3. 应预设应用前缀。The App prefix should be preset. 为应用后缀选择“通配符应用 ID” 。Select Wildcard App ID for the app suffix. com.[DomainName].* 格式输入捆绑 ID:Enter a Bundle ID in the format com.[DomainName].*:

  4. 单击“继续” 按钮,按照屏幕上的说明创建新的应用程序 ID。Click the Continue button and following the on screen instructions to create the new App ID.

预配配置文件Provisioning profile

创建好应用 ID 后,即可生成配置文件。Once the App ID has been created, the Provisioning Profile can be produced. 此配置文件包含以下信息:与此配置文件相关的应用有哪个(些)(如果是通配符应用 ID,则为多个应用) ,谁 可以使用配置文件(具体取决于添加了哪些开发人员证书),以及哪些 设备可以安装应用。This Provisioning Profile contains information on what app (or apps, if it's a wildcard app ID) this profile relates to, who can use the profile (depending on what developer certificates are added), and what devices can install the app.

若要手动创建用于开发的配置文件,请执行以下操作:To manually create a provisioning profile for development, do this:

  1. 使用 Safari 浏览到 Apple 开发者成员中心,然后在“证书、标识符和描述文件” 部分下选择配置文件。Use Safari to browse to the Apple Developers Member Center, and under the section Certificates, Identifiers & Profiles select Provisioning Profiles.

  2. 在右上角单击“+” 按钮,以创建新的配置文件。Click the + button, in the top right corner to create a new profile.

  3. 在“开发” 部分中,选中“iOS 应用开发” 旁边的单选按钮,再按“继续” :From the Development section, select the radio button next to iOS App Development, and press Continue:

  4. 在下拉菜单中,选择要使用的应用 ID:From the dropdown menu, select the App ID that to use:

  5. 选择要在预配配置文件中添加的一个或多个证书,再按“继续” :Select the Certificate(s) to include in the provisioning profile, and press Continue:

  6. 选择将安装应用的所有设备。Select all the devices that the app will be installed on.

  7. 为预配配置文件提供可识别名称,再按“继续” 创建配置文件:Provide the Provisioning Profile with an identifiable a name, and press Continue to create the profile:

  8. 按“下载” ,将预配配置文件下载到 Mac 中:Press Download to download the provisioning profile onto a Mac:

  9. 双击文件,在 Xcode 中安装配置文件。Double-click on the file to install the provisioning profile in Xcode. 请注意,除非打开,否则 Xcode 可能不会显示任何可见提示来说明它已安装配置文件。Note that Xcode might not show any visual clues that it has installed the profile except for opening. 可访问“Xcode”>“偏好设置”>“帐户” 进行验证。This can be verified by browsing to Xcode > Preferences > Accounts. 选择你的 Apple ID,然后单击“查看详细信息...” 。此时,应列出新的预配配置文件,如下所示:Select your Apple ID and click View Details.... Your new provisioning profile should be listed, as illustrated below:

成功创建配置文件后,可能需要刷新 Xcode,以便所有开发证书都可用于 Visual Studio for Mac 和 Visual Studio。After the provisioning profile has been successfully created it may be necessary to refresh Xcode so that all the development certificates are available to Visual Studio for Mac and Visual Studio.

在 Xcode 中下载配置文件和证书Downloading profiles and certificates in Xcode

Apple Developer 门户中已创建的证书和配置文件可能不会自动显示在 Xcode 中。Certificates and provisioning profiles that have been created in the Apple Developer Portal, may not automatically appear in Xcode. 因此,可能需要下载它们,以便其可由 Visual Studio for Mac 和 Visual Studio 访问。Therefore, it may be necessary to download them so they that they can be accessed by Visual Studio for Mac and Visual Studio. 若要更新和下载 Apple Developer 门户中创建的任何证书,请执行以下操作:To update and download any certificates created in the Apple Developer portal, do the following:

  1. 退出 Visual Studio for Mac 或 Visual Studio。Quit Visual Studio for Mac or Visual Studio.

  2. 启动 Xcode。Start Xcode.

  3. 选择“Xcode 菜单”>“偏好设置...” Choose Xcode Menu > Preferences...

  4. 单击“帐户” 选项卡。Click the Accounts tab.

  5. 选择团队,然后单击“下载手动配置文件”按钮 :Select a team and click the Download Manual Profiles button:

  6. 退出 Xcode。Quit Xcode.

  7. 启动 Visual Studio for Mac 或 Visual Studio。Start Visual Studio for Mac or Visual Studio.

Visual Studio for Mac 或 Visual Studio 将有新的证书或预配配置文件可供使用。The new certificates or provisioning profiles will be available in Visual Studio for Mac or Visual Studio and ready to use.

重要

可能需要停止和重启 Visual Studio for Mac,才能看到任何新的或修改的证书或由 Xcode 更新的配置文件。It may be necessary to stop and restart Visual Studio for Mac before it will see any new or modified certificates or profiles updated by Xcode.

对应用程序服务进行预配Provisioning for application services

Apple 提供一系列精选的特殊应用服务(亦称为“功能”),可以为 Xamarin.iOS 应用激活这些服务。Apple provides a selection of special Application Services, also called capabilities, that can be activated for a Xamarin.iOS application. 必须同时在 iOS 设置门户(创建“应用 ID” 时)和“Entitlements.plist” 文件(它是 Xamarin.iOS 应用程序项目的一部分)上配置这些应用程序服务。These Application Services must be configured on both the iOS Provisioning Portal when the App ID is created and in the Entitlements.plist file that is part of the Xamarin.iOS application's project. 若要了解如何将应用程序服务添加到应用,请参阅功能简介指南和使用授权指南。For information on adding Application Services to your app, refer to the Introduction to Capabilities guide and the Working with Entitlements guide.

  • 创建具备所需应用服务的应用 ID。Create an App ID with the required app services.
  • 创建含有此应用 ID 的新配置文件Create a new provisioning profile that contains this App ID.
  • 在 Xamarin.iOS 项目中设置权利Set Entitlements in the Xamarin.iOS Project

部署到设备Deploying to a device

这时,设置应已完成,且应用已准备好部署到设备。At this point provisioning should be complete, and the app is ready to be deployed to the device. 为此,请执行以下步骤:To do this, follow the steps below:

重要

开始之前,请务必先选择 Info.plist 中的“手动设置” 。Before you begin, make sure to select Manual Provisioning in the Info.plist.

  1. 在 Mac 上插入设备。Plug the device in to a Mac.

  2. 在项目的“Info.plist” 中,确保捆绑标识符与应用 ID 匹配(除非应用 ID 是通配符):In the project's Info.plist, make sure the Bundle Identifier matches the App ID (unless the App ID is a wildcard):

  3. 右键单击项目,查看“项目选项”对话框,并浏览到“生成”>“iOS 捆绑签名” 。Right-click on the project to view the Project Options dialog and browse to Build > iOS Bundle Signing. 从“签名标识” 和“预配配置文件” 旁边的下拉列表中,验证 Visual Studio for Mac 是否可以显示正确的配置文件,然后选择特定的标识和配置文件:From the drop-down list next to both the Signing Identity and Provisioning Profile, verify that Visual Studio for Mac can see the correct profiles, and select a specific identity & profile:

    如果设置为“自动” ,Visual Studio for Mac 将基于步骤 2 中设置的捆绑 ID 选择标识和配置文件。If this is set to Automatic, Visual Studio for Mac will select the identity and profile based on the Bundle ID that was set in step #2.

  4. 请确保将生成配置设置为 iPhone / iPad,而不是模拟器。Make sure to set the build configuration to iPhone / iPad, rather the simulator.

  5. 在 Visual Studio for Mac 中,单击“运行” 并查看在设备上运行的应用。Click Run in Visual Studio for Mac and view the app running on the device.

总结Summary

本指南介绍了针对 Xamarin.iOS 设置开发环境所需的步骤。This guide covered the steps required to setup the development environment for Xamarin.iOS. 探讨了如何使用关于开发人员、开发团队、可运行应用的设备和单个应用 ID 的信息对应用程序进行代码签名。It explored how an application is code signed with information about the developer, their team, the devices that an app can run on, and individual app id.