SpaceDog SpaceDog - 4 months ago 25
iOS Question

Deleting expired provisioning profiles

Using exclusively terminal, how can one identify and delete the expired provisioning profiles from

~/Library/MobileDevice/Provisioning Profiles


Is there a way to do that just from terminal?

Answer

You can write a shell script that will loop through the files, grab the date from the mobileprovision file, and check it against the current date.

#!/bin/sh

for provisioning_profile in ~/Library/MobileDevice/Provisioning\ Profiles/*.mobileprovision;
do
    printf "Checking ${provisioning_profile}... "

    # pull the expiration date from the plist
    expirationDate=`/usr/libexec/PlistBuddy -c 'Print :ExpirationDate' /dev/stdin <<< $(security cms -D -i "${provisioning_profile}")`

    # convert it to a format we can use to see if it is in the past (YYYYMMDD)
    read dow month day time timezone year <<< "${expirationDate}"
    ymd_expiration=`date -jf"%a %e %b %Y" "${dow} ${day} ${month} ${year}" +%Y%m%d`

    # compare it to today's date
    ymd_today=`date +%Y%m%d`

    if [ ${ymd_today} -ge ${ymd_expiration} ];
    then
        echo "EXPIRED"
        # rm -f "${provisioning_profile}"
    else
        echo "not expired"
    fi

done

You can use the security command and plist buddy to extract the ExpirationDate from the file. Then for simplicity I just convert that date to a easily comparable format (YYYMMDD) and compare it to today's date in the same format. I print out the status of each. Note: I do not do the delete, because I want you to verify the script results before you uncomment the removal line. I ran it on mine, and threw in an old profile. It correctly identified the expired profile in my tests.

Comments