
1.9 真机测试
模拟器虽然可以实现真机上的一些功能,但是它是有局限的。例如,在模拟器上没有重力感应、相机等。如果想要进行此方面的游戏的开发,进行程序测试时,模拟器显示就不是首选了,需要使用真机进行测试。本节将讲解真机进行测试前的准备工作和如何进行真机测试。
1.9.1 申请和下载证书
申请和下载证书的具体步骤如下。
1.创建App ID
在申请和下载证书之前,首先要创建一个App ID。App ID是一系列字符,用于唯一标识iOS设备中的应用程序。创建App ID的具体步骤如下:
(1)在Safari的搜索栏中输入网址(https://developer.apple.com/devcenter/ios/index.action),按下回车,进入iOS Dev Center-App Developer网页,如图1.81所示。

图1.81 iOS Dev Center-App Developer网页
(2)单击Log in按钮,进入Sign in with your Apple ID-Apple Developer网页,在此网页中需要开发者输入App ID以及密码,单击Sign In按钮,此时会再次进入iOS Dev Center-App Developer网页,如图1.82所示。

图1.82 iOS Dev Center-App Developer网页
注意:图1.82所看到的网页只有申请付费开发者账号后,才可以看到。
(3)选择Certificates,Identifiers&Profiles选项,进入到Certificates,Identifiers &Profiles-App Developer网页,如图1.83所示。

图1.83 Certificates,Identifiers &Profiles-App Developer网页
(4)选择Identifiers选项,进入iOS App IDs-Apple Developer网页。在此网页中,选择蓝色的Register your App ID字符串,进入Register-iOS App IDs-Apple Developer网页。在此网页中填入一些相关的内容。这些内容分为了4部分内容,分别为App ID Description、App ID Prefix、App ID Suffix、App Services,如图1.84所示。

图1.84 iOS App IDs-Apple Developer网页
(5)单击Continue按钮,进入Add-iOS App IDs-Apple Developer网页。单击Submit按钮,之后再单击Done按钮。这样一个App ID就创建好了,
2.获取设备的UDID
将设备连接到Mac(或者Mac虚拟机)上,启动Xcode。在菜单栏中选择Window|Devices命令,弹出Devices对话框,如图1.85所示。在对话框中显示的就是开发者的设备信息,其中Identifier就是UDID。

图1.85 Devices对话框
3.注册设备
如果开发者的设备是连接的Mac(或者Mac虚拟机)上的,回到Certificates, Identifiers & Profiles-App Developer网页。选择Devices选项,或者是如果开发者还处于创建App ID的网页,可以选择此网页右侧的Devices下的All选项,都会进入iOS Devices-Apple Developer网页,并会看到连接在Mac(或者Mac虚拟机)上的设备已经被注册好了,如图1.86所示。

图1.86 Devices对话框
注意:如果开发者还需要注册其他的设备,可以单击添加设备的按钮,对设备进行添加,如图1.87所示。

图1.87 注册新的设备
在此图中,开发者只需要在Name中输入设备的名称,在UDID中输入设备的标识符就可以了。单击Continue按钮,进入对设备的检测和登记网页。单击Register按钮,进入登记设备成功的网页。单击Done按钮,一个新的设备就注册成功了。
4.生成证书签名申请
为了从Apple公司申请开发证书,需要生成一个证书签名申请。生成一个证书签名申请的具体步骤如下:
(1)选择菜单栏中的“前往|实用工具”命令,到“实用工具”文件夹中,如图1.88所示。

图1.88 “实用工具”文件夹
(2)找到“钥匙串访问”应用程序,双击图标,将其打开。选择菜单栏上的钥匙串访问,如图1.89所示,选择“证书助理|从证书发布机构请求证书…”。

图1.89 “钥匙串访问”菜单
(3)弹出“证书助理”对话框,如图1.90所示。

图1.90 “证书助理”对话框
(4)输入用户电子邮件地址、选中“存储到磁盘”单选按钮。单击“继续”按钮,弹出“存储位置”对话框,如图1.91所示。

图1.91 “存储位置”对话框
注意:在“存储位置”对话框中,“存储为”以及“位置”都有默认的选项。
(5)设置位置为“桌面”,单击“存储”按钮,就在桌面生成了一个证书签名申请,并回到“证书助理”对话框,告诉开发者证书请求已经在磁盘上创建了,单击“完成”按钮即可。
5.生成证书
以上这些准备工作都做好后,便可以生成证书了,它包括了证书的申请和下载。具体的操作步骤如下:
(1)如果开发者还处于注册设备的网页,可以选择此网页右侧的Certificates的Development选项,进入iOS Certificates (Development)-Apple Developer网页,如图1.92所示。

图1.92 iOS Certificates (Development)-Apple Developer网页
(2)选中iOS App Development单选按钮,单击Continue按钮,进入到Request选项卡的网页中。在此网页中,单击Continue按钮,进入到Generate选项卡的网页中,如图1.93所示。

图1.93 Generate选项卡
(3)单击Choose File…按钮后,弹出选择文件对话框,如图1.94所示。

图1.94 选择文件对话框
(4)选择在桌面的CertificateSigningRequest.certSigningRequest文件,此文件就是生成的证书签名申请,单击“选取”按钮。再单击Generate按钮,进入到Download选项卡的网页中,如图1.95所示。

图1.95 Download选项卡
(5)单击Download按钮,对生成的证书进行下载。下载后的证书名为ios_development.cer。
(6)如果开发者还处于下载证书的网页,可以选择此网页右侧的Provisioning Profiles的Development选项,进入iOS Provisioning Profiles (Development)-Apple Developer网页。在此网页中,单击蓝色的manually generate profiles链接,进入Add-iOS Provisioning Profile-Apple Developer网页,如图1.96所示。

图1.96 Add-iOS Provisioning Profile-Apple Developer网页
(7)选中iOS App Development单选按钮,单击Continue按钮,进入到Configure选项卡的选择App ID的网页中,如图1.97所示。

图1.97 选择App ID的网页
(8)选择App ID(这里的App ID是之前创建的App ID),单击Continue按钮,进入到Configure选项卡的选择证书的网页中,如图1.98所示。

图1.98 Configure选项卡
(9)选中Select All复选框或者选择某一个证书,单击Continue按钮,进入到Configure选项卡的选择设备的网页中,如图1.99所示。

图1.99 选择设备
(10)选中Select All复选框或者选择某一个设备,单击Continue按钮,进入到Generate选项卡的网页中,如图1.100所示。

图1.100 Generate选项卡
(11)输入配置的文件名,单击Generate按钮,进入到Download选项卡的网页中,如图1.101所示。

图1.101 Download选项卡
(12)单击Download按钮,对Provisioning Profiles进行下载,下载后的文件为KongKong.mobileprovision。
(13)双击下载的ios_development.cer证书,弹出“添加证书”对话框,如图1.102所示。

图1.102 “添加证书”对话框
(14)单击“添加”按钮,将下载的ios_development.cer证书添加到钥匙串中。
(15)双击下载的KongKong.mobileprovision文件,将此文件添加到Organizer的Provisioning Profiles中。
1.9.2 实现真机测试
在进行真机测试之前,首先需要确保设备已经连在了Mac(或者Mac虚拟机)上,从1.9.1小节开始,设备就一直连接在Mac(或者Mac虚拟机)上,并且此设备就是注册过的。打开创建的项目,在运行按钮一栏中,将程序运行的设备设置为真机的名称。它会自动加载到“选择程序运行的设备”这一项中,如图1.103所示。单击运行按钮,就可以看到应用程序在真机上运行了。

图1.103 设置设备