操作系统:os x lion 10.7.4

xcode: 4.5.2

ios:6.0

参考文章:http://www.cnblogs.com/rywx/archive/2012/10/05/2712647.html

在没有iDP的情况下,要想将程序放到iPhone上调试,并最终发布IPA用于分享,需要以下几个步骤:
1.自己为自己颁发一个证书用于为生成的程序签名
2.修改工程配置以及Xcode的配置文件和二进制文件以阻止其验证和签名
3.通过自定义生成步骤,用伪造的证书为应用程序签名
4.使用一点小trick来生成IPA文件

 

1.创建证书
创建证书的过程比较简单,打开实用工具-钥匙串访问。然后在菜单栏里点击钥匙串访问-证书助理-创建证书来打开向导。第一个步骤比较重要,必须 要把名称命名为iPhone Developer,将类型设定为代码签名,将"让我覆盖这些默认值"选中。之后的步骤无需更改,一路点击“确定”和“继续”来完成这个向导就可以。

 

 

2.修改Xcode的配置文件和二进制文件
下面的步骤稍微有点繁琐,您应该了解UNIX命令行的基本操作,并了解一种命令行文本编辑器,本文使用的是vim。尽管这里会给出完整的代码,但是关于修改和保存代码的基本操作,不再赘述。下面的操作请先将Xcode按Command+Q完全关闭
(1)修改配置文件
进入目录并备份原文件(4.1、4.2、4.3在这里主要的差别是SDK的目录名不同)
(Xcode4.1请执行)cd /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/
(Xcode4.2请执行)cd /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/
(Xcode4.3请执行)cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk
(Xcode4.3.2请执行)cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk
(Xcode4.5请执行)cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk
 
sudo cp SDKSettings.plist SDKSettings.plist.orig
进行编辑(Xcode 4.5之前)
sudo vim SDKSettings.plist
将以下两段中的YES改为NO
<key>CODE_SIGNING_REQUIRED</key>
<string>YES</string>
<key>ENTITLEMENTS_REQUIRED</key>
<string>YES</string>
 
Xcode 4.5的plist格式不再为XML,而是改为了Apple自己的二进制格式,我们使用Xcode本身来编辑这个plist文件,命令是
sudo /Applications/Xcode.app/Contents/MacOS/Xcode ./SDKSettings.plist
会启动Xcode的图形界面,我们展开DefaultProperties分支,将下面的CODE_SIGNING_REQUIREDENTITLEMENTS_REQUIRED两个属性改为NO
 
下面修改另外一个文件
进入目录并备份原文件
(Xcode4.1/4.2请执行)cd /Developer/Platforms/iPhoneOS.platform/
(Xcode4.3/4.4/4.5请执行)cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform
 
备份
sudo cp Info.plist Info.plist.orig
进行编辑
sudo vim Info.plist
将全部的XCiPhoneOSCodeSignContext 修改成 XCCodeSignContext,网上的大部分文章说有2处,但我找到了3处,可能是Xcode 4.1要多一处?(Xcode 4.2/4.3/4.3.2也有三处)总之都改掉了。提示:在在vim中输入/要搜索的内容来搜索,按n键是搜索下一处。
 
(Xcode 4.5)编辑命令如下
sudo /Applications/Xcode.app/Contents/MacOS/Xcode /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Info.plist
Xcode 4.5也有三处,分别在DefaultProperties分支下、RuntimeRequirements分支下和OverrideProperties分支下。

注:我在测试时使 用命行的方式打开xcode会报错,所以先把这两个plist文件复制到/libs目录下,使用命令行修改了权根,使其可以修改,如

sudo cp ./Info.plist /libs/Info.plist

sudo chmod a+w /libs/Info.plist

sudo chmod 777 /libs/Info.plist

sudo chmod a+w /libs

sudo chmod 777 /libs

试试

然后用xcode打开/libs/Info.plist进行编辑,修改好保存再sudo cp /libs/Info.plist ./Info.plist

SDKSettings.plist也一样

 

(2)二进制补丁
#在桌面上建立script这个脚本
cd ~/Desktop
vim script
#(Xcode 4.1执行)在编辑器中输入如下内容
#!/bin/bash
cd /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Plug-ins/iPhoneOS\ Build\ System\ Support.xcplugin/Contents/MacOS/
dd if=iPhoneOS\ Build\ System\ Support of=working bs=500 count=255
printf "xc3x26x00x00" >> working
/bin/mv -n iPhoneOS\ Build\ System\ Support iPhoneOS\ Build\ System\ Support.original
/bin/mv working iPhoneOS\ Build\ System\ Support
chmod a+x iPhoneOS\ Build\ System\ Support
#(Xcode 4.2或4.5.2执行)在编辑器中输入如下内容
#!/bin/bash
cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/PrivatePlugIns/iPhoneOS\ Build\ System\ Support.xcplugin/Contents/MacOS/
dd if=iPhoneOS\ Build\ System\ Support of=working bs=500 count=255
printf "xc3x26x00x00" >> working
/bin/mv -n iPhoneOS\ Build\ System\ Support iPhoneOS\ Build\ System\ Support.original
/bin/mv working iPhoneOS\ Build\ System\ Support
chmod a+x iPhoneOS\ Build\ System\ Support
保存并退出。(4.1和4.2在这里的区别也是目录名不同,就是代码中绿色的部分。4.1是Plug-ins而4.2是PrivatePlugIns
授予这个脚本执行权限并执行它
chmod 777 script
./script
正常的话应该输出(具体的数字可能有差别)
231+1 records in
231+1 records out
115904 bytes transferred in 0.001738 secs (66694555 bytes/sec)
#(Xcode 4.3注意)在Xcode 4.3版本中,我没有找到iPhoneOS Build System Support.xcplugin这个插件,因此我跳过了这个步骤,并且最终也成功的进行了联机调试。因此我个人认为Xcode 4.3没有必要执行“二进制补丁”这一步骤。请各位朋友测试~!
#(Xcode 4.3.2/4.4/4.5注意)在Xcode 4.3.2版本中,我们可以找到iPhoneOS Build System Support.xcplugin,但我没有修改它,仍然可以进行联机调试。这个修改步骤是从Xcode 3.X中继承过来的,我们有理由猜测,在Xcode 4.x版本中,已经不需要修改这个文件了。(2012年5月28日更新)
 
至此,对SDK中配置文件和二进制文件的修改就完成了

 

3.准备自定义的生成后脚本
连接互联网后执行
#(Xcode 4.1/4.2执行)
mkdir /Developer/iphoneentitlements401
cd /Developer/iphoneentitlements401
curl -O http://www.alexwhittemore.com/iphone/gen_entitlements.txt
mv gen_entitlements.txt gen_entitlements.py
chmod 777 gen_entitlements.py
 
#(Xcode 4.3/4.4/4.5/4.5.2执行)
mkdir /Applications/Xcode.app/Contents/Developer/iphoneentitlements
cd /Applications/Xcode.app/Contents/Developer/iphoneentitlements
curl -O http://www.alexwhittemore.com/iphone/gen_entitlements.txt
mv gen_entitlements.txt gen_entitlements.py
chmod 777 gen_entitlements.py

 

如果您已经联网,则请直接转到步骤4,如果您没有联网,那么请在相应目录手动创建gen_entitlements.py并授予其执行权限,这个文件的内容为
#!/usr/bin/env python
 
import sys
import struct
 
if len(sys.argv) != 3:
print "Usage: %s appname dest_file.xcent" % sys.argv[0]
sys.exit(-1)
 
APPNAME = sys.argv[1]
DEST = sys.argv[2]
 
if not DEST.endswith('.xml') and not DEST.endswith('.xcent'):
print "Dest must be .xml (for ldid) or .xcent (for codesign)"
sys.exit(-1)
 
entitlements = """
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>application-identifier</key>
    <string>%s</string>
    <key>get-task-allow</key>
    <true/>
</dict>
</plist>
""" % APPNAME
 
f = open(DEST,'w')
if DEST.endswith('.xcent'):
f.write("\xfa\xde\x71\x71")
f.write(struct.pack('>L', len(entitlements) + 8))
f.write(entitlements)
f.close()
 
4.修改工程设置
特别注意:本阶段之前的修改配置文件、准备脚本等,只需要做一次。但本阶段的操作,对每个需要真机调试的工程都要做一遍。
这个步骤的主要作用是支持真机调试,如果不做这个步骤,仍然可以通过步骤5来生成ipa在真机上运行,但是无法使用Xcode内置的调试器对在真机上运行的程序进行单步跟踪。如果您的程序在点击Run真机调试时秒退,请检查此步骤是否正确完成。
 (1)禁用Xcode自动的签名操作
将工程配置中所有的Code Signing选项全部设为Don't Code Sign,如图。可能需要先点击“All”让这个选项显示出来

 

(2)添加自定义的生成后脚本
在Build Phases中添加一个Phase,右下角的Add Build Phase,然后单击Add Run Script,输入以下脚本
(对于Xcode 4.1/4.2)
export CODESIGN_ALLOCATE=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate
if [ "${PLATFORM_NAME}" == "iphoneos" ] || [ "${PLATFORM_NAME}" == "ipados" ]; then
/Developer/iphoneentitlements401/gen_entitlements.py "my.company.${PROJECT_NAME}" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/${PROJECT_NAME}.xcent";
codesign -f -s "iPhone Developer" --entitlements "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/${PROJECT_NAME}.xcent" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/"
fi

 

 

 

(对于Xcode 4.3/4.4/4.5/4.5.2)
export CODESIGN_ALLOCATE=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate
if [ "${PLATFORM_NAME}" == "iphoneos" ] || [ "${PLATFORM_NAME}" == "ipados" ]; then
/Applications/Xcode.app/Contents/Developer/iphoneentitlements/gen_entitlements.py "my.company.${PROJECT_NAME}" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/${PROJECT_NAME}.xcent";
codesign -f -s "iPhone Developer" --entitlements "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/${PROJECT_NAME}.xcent" "${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/"
fi
 
至此配置全部完成,下面就可以插上iPhone,重新选择生成目标来测试一下在线调试了!如果是第一次使用该设备调试,请先在Organizer中将设备切换为开发模式,具体操作请见常见问题5。
 

 

 

 

 

 

 

 

posted on 2012-11-15 22:32  袁晓平  阅读(14168)  评论(8编辑  收藏  举报