macos app签名和公证

签名脚本

bash 复制代码
function traverseCodesign() {
    targetDir="$1"

    echo "traverseCodesign in dir:$targetDir"
    echo "pwd:$(pwd)"

    # 检查目标目录是否存在
    if [ ! -d "$targetDir" ]; then
        echo "Error: Directory does not exist: $targetDir"
        return 1  # 返回错误码 1 表示目录不存在
    fi

    # 获取当前工作目录的名称
    current_dir=$(basename "${targetDir}")

    # 检查目录名称是否以 .framework 结尾
    if [[ "$current_dir" == *.framework ]]; then
        echo "codesign  ${targetDir}"
        codesign --force --options runtime --timestamp -s "${cert}" "${targetDir}"
        return 0
    fi

    # 进入目标目录
    cd "$targetDir" || return 1  # 如果 cd 失败,也返回错误码 1

    # 遍历当前目录下的所有文件和子目录
    for file in *; do
        # 使用 file 命令获取文件类型描述
        file_type=$(file "${file}")
        if [ -d "$file" ]; then
            # 如果是子目录,递归调用 traverseCodesign
            traverseCodesign "$file"
        # 签名动态库、静态库、可执行文件,其他文件不需要签名
        elif [[ "$file_type" =~ "Mach-O dynamically linked shared library" ]] || \
           [[ "$file_type" =~ "Mach-O 64-bit dynamically linked shared library" ]] || \
           [[ "$file_type" =~ "current ar archive" ]] || \
           [[ "$file_type" =~ "Mach-O executable" ]]; then
            # 移除可能存在的旧签名并重新签名
            codesign --remove-signature "$file" 2>/dev/null
            echo "codesign $file"
            codesign --force --options runtime --timestamp -s "${cert}" "$file"
        fi
    done

    # 返回上一级目录
    cd ..
}


codesign --deep --force --timestamp --options "runtime" -s "${cert}" ${apppath}/Contents/MacOS/appname
codesign --deep --force --timestamp --options "runtime" -s "${cert}" ${apppath}

验证签名是否生效的命令

bash 复制代码
codesign -vvv --deep ${apppath}
spctl  --verbose=4 --assess --type execute ${apppath}
codesign --verify --verbose ${apppath}

公证脚本

bash 复制代码
cp -RP name.app Applications
delete_if_exists Applications.zip
zip --symlinks -r -q -X Applications.zip ./Applications
echo "Submitting app for notarization..."
xcrun notarytool submit --apple-id "$USERNAME" --password "$PASSWORD" --team-id "$PROVIDER" --wait Applications.zip 2>&1 | tee tmp

# 检查提交是否成功
if [[ $? -ne 0 ]]; then
    echo "Failed to submit app for notarization."
    cat tmp
    exit 1
fi
echo tmp
# 提取 UUID
UUID=$(cat tmp | grep -Eo '\w{8}-(\w{4}-){3}\w{12}' | head -n 1)
echo "Submission successful. UUID: $UUID"

# 检查公证结果
while true; do
    echo "Checking notarization status..."
    xcrun notarytool info "$UUID" --apple-id "$USERNAME" --password "$PASSWORD" --team-id "$PROVIDER" 2>&1 | tee tmp
    cat tmp
    # 检查输出
    STATUS=$(cat tmp | grep "status" | awk '{print $2}' | tr -d '"')
    echo "Current status: $STATUS"

    if [[ "$STATUS" == "Accepted" ]]; then
        echo "Notarization successful!"
        break
    elif [[ "$STATUS" == "Invalid" ]]; then
        echo "Notarization failed."
        cat tmp
        exit 1
    else
        echo "Notarization not completed yet. Waiting 20 seconds..."
        sleep 20
    fi
done

# Staple 签名
echo "Stapling notarization ticket to app..."
xcrun stapler staple "$APP_NAME"

验证公证是否成功

bash 复制代码
xcrun stapler validate /path/to/YourApp.app
spctl -a -t open --context context:primary-signature -vvv /path/to/your.app
相关推荐
友善的猴子4 小时前
Bartender 5 for Mac 多功能菜单栏管理
macos
琉璃℡初雪5 小时前
配置mac mini M4 的一些软件
macos
友善的猴子5 小时前
JetBrains PhpStorm v2024.3.1 Mac PHP开发工具
macos·php·phpstorm
請你喝杯Java9 小时前
Mac 下载 PicGo 的踩坑指南
macos
leichangqing9 小时前
macos下 ragflow二次开发环境搭建
macos·二次开发·ragflow
行星00814 小时前
Mac M1管理多个Node.js版本
macos·node.js
友善的猴子1 天前
Adobe Photoshop 2025 Mac中文 Ps图像编辑
macos·adobe·photoshop
伊织code1 天前
macOS Chrome - 打开开发者工具,设置 Local storage
chrome·macos·设置·开发者工具·local storage
独隅1 天前
在 macOS 上修改 最大文件描述符限制(Too many open files) 和 网络端口相关参数 需要调整系统级配置的详细步骤
网络·macos
追风林1 天前
mac 解压 nsz 文件
macos