xcodemacospermissionsinstallationproductbuild

Permission to access /Library/Application Support from Mac App bundle


I have an app bundle (built with Unity 3d if that is relevant) that I am able to create a .pkg installer using productbuild and distribute on the App Store without issue. However, the app downloads and caches a fair amount of media and has some optional configuration files that need to be shared between all users on the machine. According to Apple's documentation the configuration files should probably go in the /Library/Application Support directory and the media in /Library/Caches. I've made a modified version of that app that uses those directories instead of the ones that the sandboxed app can access, but it doesn't have permission to the /Library directory unless I run the app as root, which isn't a realistic option.

I've searched google for several hours, but I can't seem to find anything about creating such an installer. I did read this answer which has a screenshot of an installer that has the option to install for all users, but either I'm missing some option to enable that or that screenshot is just outdated because I can't seem to create a .pkg that gives me that option.

So I guess my question boils down to this: how do I package my app so it can be installed for all users, and have permission to read and write to /Library/Application Support/{app name}, or is there another preferred way to share configuration files and/or media between multiple users on the same machine?


Solution

  • For anyone else who has a similar problem, the correct answer is that you can't do this using productbuild, but you can using pkgbuild.

    My productbuild build step for the app store looks like this:

    productbuild --component "{mystoreapp.app/}" /Applications --sign "{signing identity}" "{mystorepkg.pkg}"
    

    The corresponding packing command for pkgbuild looks like this:

    pkgbuild --component "{mymodifiedapp.app/}" --sign "{signing identity}" --ownership preserve --scripts "{path/to/my/scripts}" --identifier {com.yourcompany.yourapp} --version "{versionNumber}" --install-location /Applications "{mymodifiedpkg.pkg}"
    

    Note that signing is optional here as it will be distributed outside the store. where {path/to/my/scripts} has a file called postinstall in it that looks like this:

    #this function creates a directory if it doesn't exist
    create_directory() {
        if [[ ! -d "$1" ]]
        then
                if [[ ! -L "$1" ]]
                then
                        echo "directory $1 doesn't exist. Creating now"
                        mkdir "$1"
    
                        echo "directory $1 created"
                else
                        echo "directory $1 exists"
                fi
        fi
    }
    
    #this function gives all users read and write (and execute) access to the directory
    fix_dir_permissions() {
        chmod 777 "$1"
    }
    
    baseDirName="/Library/Application Support/{your app name bere}"
    
    subDirs[0]="$baseDirName/{sub dir here}"
    
    #create base directory
    create_directory "$baseDirName"
    
    #create all subdirectories and give permissions
    for subDir in "${subDirs[@]}"
    do
        create_directory "$subDir"
        fix_dir_permissions "$subDir"
    done
    
    exit 0
    

    This script will run after the install is over, and will create your application support directory as well as any subdirectories you need and change the permissions on them so all users have access to them.