iOS App重签名步骤整理--记录要点

App重签名需要准备很多原理类的知识,知道iOS 的代码签名机制熟悉证书与配置文件Mach-O 二进制文件的组成程序包与资源文件授权机制等相关知识;除此之外还要各种各样的工具配合。
本文不准备讲原理性的东西,只有通过实际操作,原理才能转化为产品。
下面看一下对App重签名的步骤,自己也记录一下,以后发布App的时候可以使用下面的步骤方法。

首先准备原材料:

需要准备的原材料

注:mac机器上安装和证书和配置文件都要与appbundle id和真机的UID相匹配。
ios_resign_with_app文件请看步骤4

 ### 一、马上开始重签名步骤
 ####1.打开终端,cd到当前目录

$ cd /Users/mango/Desktop/重签名

***以下操作都是在“重签名”路径下。***

####2.拷贝替换配置文件:

$ cp embedded.mobileprovision Chromium.app/embedded.mobileprovision

####3.查找mac中可用证书

$ xcrun security find-identity -v -p codesigning


![可用证书](http://upload-images.jianshu.io/upload_images/3076482-011953b62de744e9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

####4.下载签名脚本
下载地址:
https://github.com/chenhengjie123/iOS_resign_scripts

使用这个脚本可以让```app```包内的其它需要签名的文件,如**插件**、**iwatch**、**framework**等正确签名,因此使用这个脚本可以完全不用再管```app```内部是否有插件有情况。

***将脚本拷贝到“重签名”文件夹中。***

####5.给脚本赋予权限

$ chmod a+x ios_resign_with_app

####6.给app签名

$ ./ios_resign_with_app Chromium.app "iPhone Developer: lin xiao (XEAAXAHAFS)" embedded.mobileprovision Chromium-resigned.app

有如下输出就说明成功了

![创建签名文件](http://upload-images.jianshu.io/upload_images/3076482-8d6b06b0d6a204ff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


####7.产出:
在“重签名”文件夹中,产生了一个叫```Chromium-resigned```的```app```,如下图所示:
![输出产品Chromium-resigned.app](http://upload-images.jianshu.io/upload_images/3076482-369deed18e73ebbe.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

####8.部署到真机

$ ios-deploy -d -b Chromium-resigned.app

注:``` ios-deploy```工具的安装可以参照文章第二部分。```ios-deploy```甚至可以以```debug```模块启动```App```,并在终端上输出与```XCode```控制台一样的信息输出。

**有如下输出即代表成功部署到真机上了。**

[....] Waiting for iOS device to be connected
[....] Using ef215c406137b21eef4c3dc5e8bd76943c5646c0 (N61AP, iPhone 6 (GSM), iphoneos, arm64) a.k.a. 'xlin的iPhone'.
------ Install phase ------
[ 0%] Found ef215c406137b21eef4c3dc5e8bd76943c5646c0 (N61AP, iPhone 6 (GSM), iphoneos, arm64) a.k.a. 'xlin的iPhone' connected through USB, beginning install
[ 5%] Copying /Users/qmango/Desktop/重签名/Chromium-resigned.app/META-INF/ to device
[ 5%] Copying /Users/qmango/Desktop/重签名/Chromium-resigned.app/META-INF/com.apple.ZipMetadata.plist to device
[ 5%] Copying /Users/qmango/Desktop/重签名/Chromium-resigned.app/_CodeSignature/ to device
[ 5%] Copying /Users/qmango/Desktop/重签名/Chromium-resigned.app/_CodeSignature/CodeResources to device

...

[ 52%] CreatingStagingDirectory
[ 57%] ExtractingPackage
[ 60%] InspectingPackage
[ 60%] TakingInstallLock
[ 65%] PreflightingApplication
[ 65%] InstallingEmbeddedProfile
[ 65%] InstallingEmbeddedProfile
[ 65%] InstallingEmbeddedProfile
[ 70%] VerifyingApplication
[ 75%] CreatingContainer
[ 80%] InstallingApplication
[ 85%] PostflightingApplication
[ 90%] SandboxingApplication
[ 95%] GeneratingApplicationMap
[100%] Installed package Chromium-resigned.app
------ Debug phase ------
Starting debug of ef215c406137b21eef4c3dc5e8bd76943c5646c0 (N61AP, iPhone 6 (GSM), iphoneos, arm64) a.k.a. 'xlin的iPhone' connected through USB...
[ 0%] Looking up developer disk image
[ 95%] Developer disk image mounted successfully
[100%] Connecting to remote debug server


###二、碰到的问题汇总:

####1.运行```ios_resign_with_app```脚本时找不到文件或文件夹

xlmini:重签名 xiaoL$ ./ios_resign_with_app Chromium1.app "iPhone Developer: lin xiao (XEAAXAHAFS)" embedded.mobileprovision Chromium-resigned.appcp: Chromium1.app: No such file or directorycp: tmp.app/embedded.mobileprovision: No such file or directoryResigning with certificate: iPhone Developer: lin xiao (XEAAXAHAFS)find: tmp.app: No such file or directorysecurity: unable to open "tmp.app/embedded.mobileprovision" for reading: No such file or directoryCannot parse a NULL or zero-length dataCreating the Signed APPmv: rename tmp.app to Chromium-resigned.app: No such file or directory
Created app: Chromium-resigned.app

***解决办法:***
执行如下命令 

$ chmod a+x ios_resign_with_app

####2.安装```ios-deploy```工具,安装方法:

$ sudo npm install -g ios-deploy --unsafe-perm=true

###三、参考资料:
https://objccn.io/issue-17-2/

推荐阅读更多精彩内容