Error adding app to login items using SMAppService

Hello all,

I am trying to use the new SMAppService added in MacOS Ventura to add my app to login items. This is the code I am using:

if (@available(macOS 13.0, *)) {
        SMAppService* service = [SMAppService mainAppService];
        NSError* error = nil;
        BOOL output = false;
        if (setEnabled)
           output = [service registerAndReturnError:&error];
        else
           output = [service unregisterAndReturnError:&error];
}

However when I run the app I see this error: Domain=SMAppServiceErrorDomain Code=1 "Operation not permitted" UserInfo={NSLocalizedFailureReason=Operation not permitted} 2022-12-25 11:14:16.628 TimeDoctor2[16823:159849] Error adding or removing item Error Domain=SMAppServiceErrorDomain Code=1 "Operation not permitted" UserInfo={NSLocalizedFailureReason=Operation not permitted}

I have tried to check the error detail using this command on another terminal tab:

sudo log stream --debug --info --predicate "process in { '<my process>', 'smd', 'backgroundtaskmanagementd'} and sender in {'ServiceManagement', 'BackgroundTaskManagement', 'smd', 'backgroundtaskmanagementd'}"

I don´t really understand the output:

Filtering the log data using "process IN {"<my process>", "smd", "backgroundtaskmanagementd"} AND sender IN {"ServiceManagement", "BackgroundTaskManagement", "smd", "backgroundtaskmanagementd"}"
Timestamp            Thread   Type    Activity       PID  TTL  
2022-12-25 11:01:57.444537+0100 0x25272  Activity  0x514e0       306  0  smd: (BackgroundTaskManagement) BTMManager.getEffectiveDisposition
2022-12-25 11:01:57.445383+0100 0x25271  Default   0x514e0       491  0  backgroundtaskmanagementd: [com.apple.backgroundtaskmanagement:main] effectiveItemDisposition: appURL=<private>, type=app, url=(null), config=(null)
2022-12-25 11:01:57.452448+0100 0x25272  Error    0x514e0       306  0  smd: (BackgroundTaskManagement) [com.apple.backgroundtaskmanagement:main] getEffectiveDisposition: error: Error Domain=BTMErrorDomain Code=-98 "(null)"
2022-12-25 11:01:57.452479+0100 0x25272  Error    0x0         306  0  smd: [com.apple.xpc.smd:SMAppService] Unable to get disposition of item: <private> error: Error Domain=NSPOSIXErrorDomain Code=3
2022-12-25 11:01:57.452378+0100 0x25271  Error    0x514e0       491  0  backgroundtaskmanagementd: [com.apple.backgroundtaskmanagement:main] effectiveItemDisposition: failed to construct identifier with parameters: appURL=/Applications/Time Doctor 2.app, url=(null), type=app, config=(null)
2022-12-25 11:01:57.452495+0100 0x25272  Default   0x0         306  0  smd: [com.apple.xpc.smd:all] Found status: 3 for <private>
2022-12-25 11:01:57.842135+0100 0x25534  Activity  0x514e1       306  0  smd: (BackgroundTaskManagement) BTMManager.getEffectiveDisposition
2022-12-25 11:01:57.842254+0100 0x25271  Default   0x514e1       491  0  backgroundtaskmanagementd: [com.apple.backgroundtaskmanagement:main] effectiveItemDisposition: appURL=<private>, type=app, url=(null), config=(null)
2022-12-25 11:01:57.849707+0100 0x25271  Error    0x514e1       491  0  backgroundtaskmanagementd: [com.apple.backgroundtaskmanagement:main] effectiveItemDisposition: failed to construct identifier with parameters: appURL=/Applications/Time Doctor 2.app, url=(null), type=app, config=(null)
2022-12-25 11:01:57.849804+0100 0x25534  Error    0x514e1       306  0  smd: (BackgroundTaskManagement) [com.apple.backgroundtaskmanagement:main] getEffectiveDisposition: error: Error Domain=BTMErrorDomain Code=-98 "(null)"
2022-12-25 11:01:57.849828+0100 0x25534  Error    0x0         306  0  smd: [com.apple.xpc.smd:SMAppService] Unable to get disposition of item: <private> error: Error Domain=NSPOSIXErrorDomain Code=3
2022-12-25 11:01:57.849839+0100 0x25534  Default   0x0         306  0  smd: [com.apple.xpc.smd:all] Found status: 3 for <private>
2022-12-25 11:01:58.020633+0100 0x25534  Activity  0x514e2       306  0  smd: (BackgroundTaskManagement) BTMManager.getEffectiveDisposition
2022-12-25 11:01:58.020762+0100 0x25271  Default   0x514e2       491  0  backgroundtaskmanagementd: [com.apple.backgroundtaskmanagement:main] effectiveItemDisposition: appURL=<private>, type=app, url=(null), config=(null)
2022-12-25 11:01:58.025436+0100 0x25534  Error    0x514e2       306  0  smd: (BackgroundTaskManagement) [com.apple.backgroundtaskmanagement:main] getEffectiveDisposition: error: Error Domain=BTMErrorDomain Code=-98 "(null)"
2022-12-25 11:01:58.025334+0100 0x25271  Error    0x514e2       491  0  backgroundtaskmanagementd: [com.apple.backgroundtaskmanagement:main] effectiveItemDisposition: failed to construct identifier with parameters: appURL=/Applications/Time Doctor 2.app, url=(null), type=app, config=(null)
2022-12-25 11:01:58.304370+0100 0x25271  Error    0x514e4       491  0  backgroundtaskmanagementd: [com.apple.backgroundtaskmanagement:main] effectiveItemDisposition: failed to construct identifier with parameters: appURL=/Applications/Time Doctor 2.app, url=(null), type=app, config=(null)

Could someone help me please?

Replies

Error Domain=BTMErrorDomain Code=-98`

BTMErrorDomain is a private error domain associated with the BTM (‘background task management’) subsystem that underlies the SMAppService API [1]. Error -98 translates (I think :-) to ‘invalid parameter’, which isn’t very helpful.

If you test this on a ‘clean’ machine — I typically use a VM for that, refreshing to a fresh snapshot between tests — does the problem reproduce?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] You can see more evidence for this is the sfltool command-line tool, which has dumpbtm and resetbtm subcommands.