1. 程式人生 > >Mac提示來自身份不明的開發者

Mac提示來自身份不明的開發者

Mac安裝或者第一次開啟軟體,經常會出現下面對話方塊

      

從使用者角度,有兩種方法解決這個問題。

1.修改安全性與隱私設定,選擇任何來源(系統偏好設定-安全性與隱私)


2.右鍵-開啟


從開發者的角度,則要複雜一些了。

首先看看導致這種現象的原因。簡單來說是Mac系統的GateKeeper在安裝和開啟軟體時,對軟體的來源和開發者進行校驗。校驗的內容是軟體的簽名,即軟體內部的_CodeSignature目錄內容。這是我的理解哈。

然後,就應該從軟體的簽名入手。

簽名需要證書,有多種型別:Developer ID Application,Developer ID Installer,3rd Party Mac Developer Application,3rd Party Mac Developer Installer等。證書的生成,這裡不做介紹,因為本人也不是很瞭解。

經驗證,對於App,需要使用Developer ID Application進行簽名,否則在第一次開啟會提示未知開發者。而對於pkg安裝包,則要使用Developer ID Installer進行簽名,否則在安裝的時候,會提示未知開發者。這裡證書的選擇還是比較重要的,不然,不管你簽名是否成功,都過不了GateKeeper那關。LZ就是因為證書選擇錯了,浪費了好幾天時間。

簽名可以通過下面命令列實現(如果是Xcode專案,可以直接在Xcode中配置code sign):

# App簽名
codesign --timestamp=none -f -v -s "Developer ID Application" test.app

# pkg簽名
productsign --timestamp=none --sign "Developer ID Installer" ./test.pkg ./test_sign.pkg

官方給出了驗證簽名的方法,只有當結果為accepted的時候,軟體才能正常唄開啟或者安裝
# App
spctl -a -v --type exec ./test.app
# pkg
spctl -a -v --type install ./test_sign.pkg

官方指引 

 關於軟體打包,這裡有兩個糾結的地方。

以前我都是用PackageManager來打包bundle,生成mpkg檔案。雖然現在的Xcode沒有附帶PackageManager,不過還是可以通過下載舊版Xcode來獲得。

我將mpkg檔案進行簽名,正常來說,安裝包應該使用Developer ID Installer證書來簽名,但是卻一直報錯。而當我用Developer ID Application證書來簽名時,簽名是成功了,卻沒能解決圖一的問題。所以後來只能用命令列來打包。

一開始使用productbuild,將兩個bundle作為組建,新增到一個pkg中很順利,而且也可以正常安裝這個pkg。但是,安裝對話方塊中,沒有標題顯示。所以需要在外部包多一個pkg,通過distribution檔案來定義標題和其他屬性。


完整命令

export CODE_SIGN="Developer ID Installer"
productbuild --component ./a.bundle /Library/Internet\ Plug-Ins --component ./b.bundle /Library/Internet\ Plug-Ins --sign "${CODE_SIGN}" test.pkg
productbuild --distribution distribution.xml --sign "${CODE_SIGN}" ./installer.pkg

distribution.xml

<?xml version="1.0" encoding="UTF-8"?>
<installer-gui-script minSpecVersion="1">
    <title>軟體標題</title>
    <organization>com.example</organization>
    <options customize="never" allow-external-scripts="no" rootVolumeOnly="false"/>
    <!-- List all component packages -->
    <pkg-ref id="com.example.plugin" version="2005" auth="root" onConclusion="none">test.pkg</pkg-ref>
    <!-- List them again here. They can now be organized as a hierarchy if you want. -->
    <choices-outline>
    <line choice="com.example.plugin"/>
    </choices-outline>
    <!-- Define each choice above -->
    <choice id="com.example.plugin" visible="false" start_visible="false" title="plugin" description="plugin" enabled="false" start_selected="true" selected="true">
    <!--customLocation="false"-->
        <pkg-ref id="com.example.plugin"/>
    </choice>
    <pkg-ref id="com.example.plugin">
        <bundle-version/>
    </pkg-ref>
</installer-gui-script>

這時候,令人蛋疼的事情出現了:test.pkg could not be loaded。Google了很久都沒有找到解決辦法,也嘗試在其他電腦上執行上面的命令,也不行。(如果有解決方案,請告訴我。。。)

所以後來混用pkgbuild和productbuild來解決這個問題

export CODE_SIGN="Developer ID Installer"

pkgbuild --identifier "com.example.plugin" --component ./a.bundle --install-location /Library/Internet\ Plug-Ins --component ./b.bundle --install-location /Library/Internet\ Plug-Ins --sign "${CODE_SIGN}" ./test.pkg

productbuild --distribution distribution.xml --sign "${CODE_SIGN}" ./installer.pkg

總結的時候,感覺不是很難,但是這個問題糾結了好幾天,坑。Apple的官方文件說的不明不白的感覺,難道是我英文太差了嗎。。。

官方連結