使用xcodebuild打包 導(dǎo)出ipa 上傳蒲公英或者提交AppStore

/Applications/Xcode.app/Contents/Applications/Application\ Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Support/altool
  • 使用時(shí)如報(bào)如下錯(cuò)誤
altool[] *** Error: 
Exception while launching iTunesTransporter: Transporter not found at path: /usr/local/itms/bin/iTMSTransporter. 
You should reinstall the application.
  • 建立軟鏈
ln -s /Applications/Xcode.app/Contents/Applications/Application\ Loader.app/Contents/itms /usr/local/itms
  • 如果想要在腳本中修改plist文件的內(nèi)容可以使用PlistBuddy,PlistBuddy是Mac里一個(gè)用于命令行下讀寫plist文件的工具,在/usr/libexec/下??梢酝ㄟ^它讀取或修改plist文件的內(nèi)容。
#!/bin/sh
# PlistBuddy程序的絕對(duì)路徑
PlistBuddyPath=/usr/libexec/PlistBuddy
appInfoPlistPath="/Volumes/SourceCode/showstart_ios/ShowStart_3.0/Info.plist"
bundleShortVersion=$($PlistBuddyPath -c "print CFBundleShortVersionString" ${appInfoPlistPath})
bundleVersion=$($PlistBuddyPath  -c "print CFBundleVersion" ${appInfoPlistPath})
echo "$bundleShortVersion"
echo "$bundleVersion"

buildNumber="3.69"

bundleVersion=$($PlistBuddyPath  -c "Set :CFBundleVersion $buildNumber" ${appInfoPlistPath})
bundleVersion=$($PlistBuddyPath  -c "print CFBundleVersion" ${appInfoPlistPath})
echo "$bundleVersion"

開始打包

  • 百度了一下大多數(shù)文章使用xcodeuildxcrun編譯導(dǎo)出ipa
xcodebuild -workspace XXX -scheme XXX -configuration Release
xcrun -sdk iphoneos PackageApplication -v "/XXX/XXX.app" -o "/XXX/XXX"
  • 另一種是xcodebuildarchive-exportArchive,然而最新需要使用--exportOptionsPlist選項(xiàng),而不再使用--exportFromat
  • 使用xcodebuild -help命令可查看--exportOptionsPlist選項(xiàng)具體描述

如何獲取工程的CODE_SIGN_IDENTITYPROVISIONING_PROFILE

  • 右鍵 xxx.xcodeproj 顯示包內(nèi)容 然后打開project.pbxproj文件
  • command F 搜索 CODE_SIGN_IDENTITY PROVISIONING_PROFILE
QQ20161106-0.png
QQ20161106-1.png
  • PROVISIONING_PROFILE 使用uuid或者名稱都可以

  • 注意 因?yàn)槲疫@里打包用的Release模式所以,我在查找CODE_SIGN_IDENTITYPROVISIONING_PROFILE都是找的Release模式配置

  • 使用atool時(shí) 最終輸出的xml 中包含 success-message表示成功,如果包含product-errors表示失敗

  • 執(zhí)行腳本

// 首先把兩個(gè)plist和腳本放到工程根目錄,然后打開terminal進(jìn)入項(xiàng)目
// 第一次需要設(shè)置腳本的執(zhí)行權(quán)限
chmod +x xcodebuild.sh
// 執(zhí)行腳本
./xcodebuild.sh

完整的腳本 其中 xxxx 替換為你自己的

#!/bin/sh

echo "~~~~~~~~~~~~~~~~開始執(zhí)行腳本~~~~~~~~~~~~~~~~"


# 開始時(shí)間
beginTime=`date +%s`
DATE=`date '+%Y-%m-%d-%T'`
#需要編譯的 targetName
TARGET_NAME="xxxx"
#編譯模式 工程默認(rèn)有 Debug Release 
CONFIGURATION_TARGET=Release
#編譯路徑
BUILDPATH=~/Desktop/${TARGET_NAME}_${DATE}
#archivePath
ARCHIVEPATH=${BUILDPATH}/${TARGET_NAME}.xcarchive
#輸出的ipa目錄
IPAPATH=${BUILDPATH}

#證書名
CODE_SIGN_IDENTITY="xxxxx"
#描述文件
PROVISIONING_PROFILE_NAME="xxxx"

#蘋果賬號(hào)
AppleID="xxxx"
AppleIDPWD="xxxx"

#導(dǎo)出ipa 所需plist
ADHOCExportOptionsPlist=./ADHOCExportOptionsPlist.plist
AppStoreExportOptionsPlist=./AppStoreExportOptionsPlist.plist

ExportOptionsPlist=${ADHOCExportOptionsPlist}


# 是否上傳蒲公英
UPLOADPGYER=false
# 是否上傳AppStore
UPLOADAPPSTore=false

echo "~~~~~~~~~~~~~~~~選擇打包方式~~~~~~~~~~~~~~~~"
echo "      1 ad-hoc (默認(rèn))"
echo "      2 AppStore "

# 讀取用戶輸入并存到變量里
read parameter
sleep 0.5
method="$parameter"

# 判讀用戶是否有輸入 
if [ -n "$method" ]
then
    if [ "$method" = "1" ]
    then 
    PROVISIONING_PROFILE_NAME="xxxx"
    ExportOptionsPlist=${ADHOCExportOptionsPlist}
    elif [ "$method" = "2" ]
    then
    UPLOADAPPSTore=true
    PROVISIONING_PROFILE_NAME="xxxx"
    ExportOptionsPlist=${AppStoreExportOptionsPlist}
    else
    echo "參數(shù)無效...."
    exit 1
    fi
else
    ExportOptionsPlist=${ADHOCExportOptionsPlist}
fi

if [ $UPLOADAPPSTore = false ]
then
    echo "~~~~~~~~~~~~~~~~是否上傳蒲公英~~~~~~~~~~~~~~~~"
    echo "      1 不上傳 (默認(rèn))"
    echo "      2 上傳 "
    read para
    sleep 0.5

    if [ -n "$para" ]
    then
        if [ "$para" = "1" ]
        then 
        UPLOADPGYER=false
        elif [ "$para" = "2" ]
        then
        UPLOADPGYER=true
        else
        echo "參數(shù)無效...."
        exit 1
        fi
    else
        UPLOADPGYER=false
    fi
fi


echo "~~~~~~~~~~~~~~~~開始編譯~~~~~~~~~~~~~~~~~~~"
echo "~~~~~~~~~~~~~~~~開始清理~~~~~~~~~~~~~~~~~~~"
# 清理 避免出現(xiàn)一些莫名的錯(cuò)誤
xcodebuild clean -workspace ${TARGET_NAME}.xcworkspace \
-configuration \
${CONFIGURATION} -alltargets

echo "~~~~~~~~~~~~~~~~開始構(gòu)建~~~~~~~~~~~~~~~~~~~"
#開始構(gòu)建
xcodebuild archive -workspace ${TARGET_NAME}.xcworkspace \
-scheme ${TARGET_NAME} \
-archivePath ${ARCHIVEPATH} \
-configuration ${CONFIGURATION_TARGET} \
CODE_SIGN_IDENTITY="${CODE_SIGN_IDENTITY}" \
PROVISIONING_PROFILE="${PROVISIONING_PROFILE_NAME}"

echo "~~~~~~~~~~~~~~~~檢查是否構(gòu)建成功~~~~~~~~~~~~~~~~~~~"
# xcarchive 實(shí)際是一個(gè)文件夾不是一個(gè)文件所以使用 -d 判斷
if [ -d "$ARCHIVEPATH" ]
then
echo "構(gòu)建成功......"
else
echo "構(gòu)建失敗......"
rm -rf $BUILDPATH
exit 1
fi
endTime=`date +%s`
ArchiveTime="構(gòu)建時(shí)間$[ endTime - beginTime ]秒"


echo "~~~~~~~~~~~~~~~~導(dǎo)出ipa~~~~~~~~~~~~~~~~~~~"

beginTime=`date +%s`

xcodebuild -exportArchive \
-archivePath ${ARCHIVEPATH} \
-exportOptionsPlist ${ExportOptionsPlist} \
-exportPath ${IPAPATH}

echo "~~~~~~~~~~~~~~~~檢查是否成功導(dǎo)出ipa~~~~~~~~~~~~~~~~~~~"
IPAPATH=${IPAPATH}/${TARGET_NAME}.ipa
if [ -f "$IPAPATH" ]
then
echo "導(dǎo)出ipa成功......"
else
echo "導(dǎo)出ipa失敗......"
# 結(jié)束時(shí)間
endTime=`date +%s`
echo "$ArchiveTime"
echo "導(dǎo)出ipa時(shí)間$[ endTime - beginTime ]秒"
exit 1
fi

endTime=`date +%s`
ExportTime="導(dǎo)出ipa時(shí)間$[ endTime - beginTime ]秒"

# 上傳AppStore
if [ $UPLOADAPPSTore = true ]
then    

    altoolPath="/Applications/Xcode.app/Contents/Applications/Application\ Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Versions/A/Support/altool"
    ${altoolPath} --validate-app \
    -f ${IPAPATH} \
    -u ${AppleID} \
    -p ${AppleIDPWD} \
    -t ios --output-format xml

        if [ $? = 0 ]
        then
        echo "~~~~~~~~~~~~~~~~驗(yàn)證ipa成功~~~~~~~~~~~~~~~~~~~"
            ${altoolPath} --upload-app \
            -f ${IPAPATH} \
            -u ${AppleID} \
            -p ${AppleIDPWD} \
            -t ios --output-format xml

            if [ $? = 0 ]
            then
            echo "~~~~~~~~~~~~~~~~提交AppStore成功~~~~~~~~~~~~~~~~~~~"
            else
            echo "~~~~~~~~~~~~~~~~提交AppStore失敗~~~~~~~~~~~~~~~~~~~"
            fi
        else
        echo "~~~~~~~~~~~~~~~~驗(yàn)證ipa失敗~~~~~~~~~~~~~~~~~~~"
        fi
else
    # 上傳蒲公英 
    if [ $UPLOADPGYER = true ]
    then
        echo "~~~~~~~~~~~~~~~~上傳ipa到蒲公英~~~~~~~~~~~~~~~~~~~"
        curl -F "file=@$IPAPATH" \
        -F "uKey=xxxxx" \
        -F "_api_key=xxxx" \
        -F "password=xxxxx" \
        -F "isPublishToPublic=xxxx" \
        https://www.pgyer.com/apiv1/app/upload --verbose

        if [ $? = 0 ]
        then
        echo "~~~~~~~~~~~~~~~~上傳蒲公英成功~~~~~~~~~~~~~~~~~~~"
        else
        echo "~~~~~~~~~~~~~~~~上傳蒲公英失敗~~~~~~~~~~~~~~~~~~~"
        fi
    fi
fi



echo "~~~~~~~~~~~~~~~~配置信息~~~~~~~~~~~~~~~~~~~"
echo "開始執(zhí)行腳本時(shí)間: ${DATE}"
echo "編譯模式: ${CONFIGURATION_TARGET}"
echo "導(dǎo)出ipa配置: ${ExportOptionsPlist}"
echo "打包文件路徑: ${ARCHIVEPATH}"
echo "導(dǎo)出ipa路徑: ${IPAPATH}"

echo "$ArchiveTime"
echo "$ExportTime"
exit 1

github

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容