How do I codesign a Swift app via the commandline?

2019-02-01 22:47发布

问题:

I am attempting to run an enterprise signed build of my Swift app on a real phone running iOS 7.1.

If I create the enterprise signed app via the Xcode interface, the app works perfectly. If I create the enterprise signed app via xcodebuild and codesign on the command line, then the app crashes on open with the following log:

Dyld Error Message:
Library not loaded: @rpath/libswiftCore.dylib
Referenced from: /var/mobile/Applications/96578E7F-7FE6-4603-82F2-8941561225D8/Foo.app/Foo
Reason: no suitable image found.  Did find:
  /private/var/mobile/Applications/96578E7F-7FE6-4603-82F2-8941561225D8/Foo.app/Frameworks/libswiftCore.dylib: code signature invalid for '/private/var/mobile/Applications/96578E7F-7FE6-4603-82F2-8941561225D8/Foo.app/Frameworks/libswiftCore.dylib'
Dyld Version: 324

When I create the IPA via the command line, the IPA always contains the same libswiftCore.dylib

$ md5 cli/Payload/Foo.app/Frameworks/libswiftCore.dylib
MD5 (cli/SwiftSupport/libswiftCore.dylib) = 0fe0370b5585a88a89d230b7501aee31 <- same every time; matches what is provided by Xcode

$ md5 /Applications/Xcode6-Beta5.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphoneos/libswiftCore.dylib
MD5 (/Applications/Xcode6-Beta5.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphoneos/libswiftCore.dylib) = 0fe0370b5585a88a89d230b7501aee31 <- Same!

When the Xcode UI produces the IPA, it is clear that it is somehow signing or modifying libswiftCore.dylib

$ md5 ui/Payload/Foo.app/Frameworks/libswiftCore.dylib
MD5 (ui/Payload/Foo.app/Frameworks/libswiftCore.dylib) = df73f604b1370b19721dfa7de298340f <- different every time

I have tried using the --deep option on codesign with no luck.

How can I get libswiftCore.dylib to get signed correctly when building/codesigning via the command line?

回答1:

The solution is not obvious but easy. Apple changed something in the code signing process for Swift:

  • Delete your distribution certificate and provisioning profile from your developer machine
  • Log into the developer portal
  • generate a new production certificate and distribution provisioning profil
  • Install both on your mac

The error should be gone.



回答2:

It looks like as of Xcode 6 Beta 6 you need to codesign the Swift libraries manually.

codesign --force --verbose --sign 'iPhone Distribution: My Company' Foo.app/
codesign --force --verbose --sign 'iPhone Distribution: My Company' Foo.app/Frameworks/*

I have filed a bug report with Apple and hopefully they will make this happen automatically if you use codesign's --deep option.



回答3:

I create a gist to solve this problem, and it works (->link).

The main idea is to codesign the dylibs frist, and then codesign the app.



回答4:

codesign -f -s 'iPhone Distribution: My Company' Foo.app/Frameworks/*
codesign -f -s 'iPhone Distribution: My Company' Foo.app --entitlements=$ENTITLEMENTS_PATH

not the other way around, otherwise it will fail on installing



回答5:

When building my Mac app from command line, I can see Xcode code-signing Swift libraries by running swift-stdlib-tool:

/Applications/Xcode6-Beta5.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-stdlib-tool --verbose --copy
Copying libswiftCore.dylib from /.../XcodeDefault.xctoolchain/usr/lib/swift/macosx to /.../LiveReload.app/Contents/Frameworks
... (repeat for 8 libswift*.dylib libraries)
/usr/bin/codesign '--force' '--sign' 'C2F5439B97B16E0BD7FF671913391346B17440DC' '--verbose' '/.../LiveReload.app/Contents/Frameworks/libswiftCore.dylib'
... (repeat for 8 libswift*.dylib libraries)

Looks like you can run similar codesign commands yourself.

Also, this is for Mac, but perhaps if you run xcodebuild for your iOS project, you will see the specific commands that are invoked for your project. (Just configure the binary to be signed in Release configuration, the way you'd like it signed in your IPA, including the provisioning profile.)



标签: ios swift xcode6