Accessing SafariServices from Sandboxed LaunchAgent

I'm attempting to reload a Safari Content Blocker from within a sandboxed command-line tool configured as a LaunchAgent. However, when I use SFContentBlockerManager to reload the content blocker, I encounter the error SFErrorDomain Code=1: Unavailable error.

Is it possible to reload a content blocker from a LaunchAgent? If so, how can it be done?

//
//  main.swift
//  BlockerUpdater
//
//  Created by Sebastian Livoni on 30/06/2024.
//

import Foundation
import SafariServices

// Function to reload content blocker asynchronously
func reloadContentBlocker() async {
    NSLog("Hello, World!")
    
    do {
        try await SFContentBlockerManager.reloadContentBlocker(withIdentifier: "me.livoni.blocker.dns")
        NSLog("Reload complete")
    } catch {
        NSLog("Failed to reload content blocker: \(error.localizedDescription)")
    }
}

// Main entry point for async code
@main
struct BlockerUpdater {
    static func main() async {
        await reloadContentBlocker()
    }
}
Answered by Engineer in 793588022

This is not a supported flow for Safari Extensions. They should be part of an app. You are welcome to file an enhancement request for explicit support.

This is not a supported flow for Safari Extensions. They should be part of an app. You are welcome to file an enhancement request for explicit support.

Accepted Answer

Alright. I believe the only solution is to create an empty sandboxed app, launch it as an agent, and add the Content Blocker targets to that.

Accessing SafariServices from Sandboxed LaunchAgent
 
 
Q