xcode 制作 iPhone 应用程序时从 xcodebuild 中设置配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/866761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:01:54  来源:igfitidea点击:

Setting a provisioning profile from within xcodebuild when making iPhone apps

iphonexcodexcodebuildprovisioning

提问by Steve Klabnik

I'm using xcodebuildto compile my iPhone app from the command line. Is there a way to pass in some sort of option to set the provisioning profile? There seems to be not very much information about xcodebuild in general.

我正在使用xcodebuild从命令行编译我的 iPhone 应用程序。有没有办法传入某种选项来设置配置文件?一般来说,关于 xcodebuild 的信息似乎并不多。

采纳答案by Corey Floyd

Documentation

文档

It seems from the doc, you can't set the provisioning file BUT you can specify the target:

从文档看来,您无法设置配置文件,但可以指定目标:

[-target targetname]

[-目标目标名称]

So, if you create a target for each provisioning file, you could select the proper target from the command line.

因此,如果您为每个配置文件创建一个目标,则可以从命令行中选择正确的目标。

This would basically accomplish what your asking.

这基本上可以完成您的要求。

回答by Rob

Actually, you should be able to just add it to the XCode command line setting.

实际上,您应该能够将它添加到 XCode 命令行设置中。

xcodebuild [whatever other options you have] PROVISIONING_PROFILE="[Your profile Unique ID here]"

xcodebuild [任何其他选项] PROVISIONING_PROFILE="[您的个人资料唯一 ID]"

Build Settings from the command line are supposed to override everything, so this should win out over anything defined in the project or target.

命令行中的构建设置应该覆盖所有内容,因此这应该胜过项目或目标中定义的任何内容。

回答by jkp

My solution isn't elegant but it does do the job and let me automate everything on the build server:

我的解决方案并不优雅,但它确实完成了工作,让我在构建服务器上自动执行所有操作:

#!/bin/bash

TARGET="Your App"
CONFIGURATION="Release"
SDK="iphoneos"
PROFILE_PATH="/Users/jkp/Desktop/foo.mobileprovision"
IDENTITY="iPhone Distribution: Your Company Ltd"
KEYCHAIN="/Users/jkp/Desktop/keychain"
PASSWORD="foobar"

open "${PROFILE_PATH}"
sleep 5
osascript -e "tell application \"Xcode\" to quit"
security unlock-keychain -p ${PASSWORD} ${KEYCHAIN}
xcodebuild \
  -target "${TARGET}" \
  -configuration ${CONFIGURATION} \
  -sdk iphoneos \
  CODE_SIGN_IDENTITY="${IDENTITY}" \    
  OTHER_CODE_SIGN_FLAGS="--keychain ${KEYCHAIN}"

The key thing here is that I didn't need to install the provisioning profile first. I actually have another script that uses mechanize to download the latest copy of the provisioning profile before each build which means we can update the profile (adding new devices for example) remotely and have those changes picked up by our CI server without any extra work.

这里的关键是我不需要先安装配置文件。我实际上有另一个脚本,它使用机械化在每次构建之前下载配置文件的最新副本,这意味着我们可以远程更新配置文件(例如添加新设备),并让我们的 CI 服务器获取这些更改,而无需任何额外工作。

Note:I've since found a way to install or update a provisioning profile without needing to involve Xcode at all - much cleaner! See herefor full details.

注意:我已经找到了一种安装或更新配置文件的方法,根本不需要涉及 Xcode - 更干净!有关完整详细信息,请参见此处

回答by BitByteDog

The provisioning profile has to be provided by UUID, in my case the provisioning profiles are checked in to the source control system, and are therefore checked out with the code by the developer/build server/CI system. In the source tree the profiles have human readable names such as MyApp.mobileprovison and are located in a directory called "ProvisioningProfiles". To create an xcode archive, the profiles have to be renamed and copied to the ~/Library/MobileDevice/Provisioning Profiles directory before xcodebuild will recognize them. This is a code snippet that can be used in a CI build script.

供应配置文件必须由 UUID 提供,在我的情况下,供应配置文件被检入源控制系统,因此由开发人员/构建服务器/CI 系统检出代码。在源代码树中,配置文件具有人类可读的名称,例如 MyApp.mobileprovison,并位于名为“ProvisioningProfiles”的目录中。要创建 xcode 存档,配置文件必须重命名并复制到 ~/Library/MobileDevice/Provisioning Profiles 目录,然后 xcodebuild 才能识别它们。这是一个可以在 CI 构建脚本中使用的代码片段。

# The keychain needs to be unlocked for signing, which requires the keychain
# password. This is stored in a file in the build account only accessible to
# the build account user
if [ ! -f $HOME/.pass ] ; then
    echo "no keychain password file available"
    exit 1
fi

case `stat -L -f "%p" $HOME/.pass`
in
    *400) ;;
    *)
        echo "keychain password file permissions are not restrictive enough"
        echo "chmod 400 $HOME/.pass"
        exit 1
        ;;
esac

#
# unlock the keychain, automatically lock keychain on script exit
#
security unlock-keychain -p `cat $HOME/.pass` $HOME/Library/Keychains/login.keychain
trap "security lock-keychain $HOME/Library/Keychains/login.keychain" EXIT

#
# Extract the profile UUID from the checked in Provisioning Profile.
#
uuid=`/usr/libexec/plistbuddy -c Print:UUID /dev/stdin <<< \
        \`security cms -D -i ProvisioningProfiles/MyApp.mobileprovision\``

#
# Copy the profile to the location XCode expects to find it and start the build,
# specifying which profile and signing identity to use for the archived app
#
cp -f ProvisioningProfiles/MyApp.mobileprovision \
        "$HOME/Library/MobileDevice/Provisioning Profiles/$uuid.mobileprovision"
xcodebuild -workspace MyApp.xcworkspace -scheme MyScheme \
        -archivePath build/MyApp.xcarchive archive \
        PROVISIONING_PROFILE="$uuid" CODE_SIGN_IDENTITY="iOS Distribution"

The keychain must be unlocked and the "/usr/bin/codesign" tool must be allowed access to the private key associated with signing identity for this to work - The following references were used https://stackoverflow.com/a/21327591/2351246and Add codesign to private key ACL without Keychainfor unlocking and adding keychain access for codesign respectively.

必须解锁钥匙串,并且必须允许“/usr/bin/codesign”工具访问与签名身份相关联的私钥才能工作 - 使用了以下参考 https://stackoverflow.com/a/21327591/ 2351246codesign添加到没有钥匙串的私钥 ACL分别用于解锁和添加用于codesign 的钥匙串访问。

If the archive is to be subsequently exported to an IPA using xcodebuild then the following issue must be taken into account (xcodebuild not copying file from .app). The provisioning profile needs to be supplied again. The script snippet to create an IPA is

如果随后要使用 xcodebuild 将存档导出到 IPA,则必须考虑以下问题(xcodebuild 不从 .app 复制文件)。需要再次提供配置文件。创建 IPA 的脚本片段是

profileName=`/usr/libexec/plistbuddy -c Print:Name /dev/stdin <<< \
        \`security cms -D -i ProvisioningProfiles/MyApp.mobileprovision\``

xcodebuild \
        -exportArchive \
        -exportFormat IPA \
        -archivePath build/MyApp.xcarchive \
        -exportPath $IPADIR/MyApp.ipa \
        -exportProvisioningProfile "$profileName"

The keychain will have to be unlocked while this command is running.

运行此命令时必须解锁钥匙串。

UPDATE

更新

On OSX Mavericks (v10.9.5) and OSX Yosemite we started seeing code signing errors:

在 OSX Mavericks (v10.9.5) 和 OSX Yosemite 上,我们开始看到代码签名错误:

Codesign check fails : ...../MyApp.app: resource envelope is obsolete

Check this posting here for the cause xcodebuild - codesign -vvvv says"resource envelope is obsolete"

在此处查看此帖子以了解原因xcodebuild - codesign -vvvv 说“资源信封已过时”

To implement the change suggested by Apple Support in the referenced post, run the following command:

要实施 Apple 支持在引用的帖子中建议的更改,请运行以下命令:

 sudo perl -pi.bak -e 's/--verify"./--verify", "--no-strict",/ if /codesign.*origApp/;' `xcrun -sdk iphoneos -f PackageApplication`

回答by Tim Fulmer

I realize this is probably a little off OP topic, but this question comes up first when searching for how to get Xcode builds working on Jenkins. Posting some additional information here for posterity. And points. If you found this useful, please give points :)

我意识到这可能有点偏离 OP 主题,但是在搜索如何让 Xcode 构建在 Jenkins 上工作时首先出现这个问题。在这里发布一些额外的信息以供后代使用。和积分。如果你觉得这有用,请给分:)

Anyway, went several rounds on this one a couple times lately. Found this:

无论如何,最近在这个上进行了几次。发现这个:

http://code-dojo.blogspot.co.uk/2012/09/fix-ios-code-signing-issue-when-using.html

http://code-dojo.blogspot.co.uk/2012/09/fix-ios-code-signing-issue-when-using.html

Seems to work. Make sure to:

似乎工作。确保:

  1. Setup the Xcode build as a regular OSX user first; get it building your distributable from there, fixing any provisioning profile issues through Xcode;

  2. Get the command line build working using xcodebuild, as a regular OSX user;

  3. Then follow the instructions in the post above to the letter, copying all iOS certs from Login Keychain to System and copying ~/Library/MobileDevice/Provisioning Profilesto the Jenkins user's ~jenkins/Library/MobileDevice/Provisioning Profilesfolder.

  1. 首先以普通 OSX 用户身份设置 Xcode 版本;让它从那里构建您的可分发文件,通过 Xcode 修复任何配置文件问题;

  2. 作为普通 OSX 用户,使用 xcodebuild 获取命令行构建;

  3. 然后按照上面帖子中的说明进行操作,将所有 iOS 证书从 Login Keychain 复制~/Library/MobileDevice/Provisioning Profiles到System 并复制到 Jenkins 用户的~jenkins/Library/MobileDevice/Provisioning Profiles文件夹。

The next Jenkins build, using the commands in step 2 above, should work flawlessly.

下一个 Jenkins 构建,使用上面第 2 步中的命令,应该可以完美地工作。