I'm a developer using Lazarus Pascal, so converting ObjC and Swift comes with its challenges.
I'm trying to figure how to properly use SMAppService to add my application as a login item for the App Store.
I have learned that the old method (< macOS 13) uses a helper tool, included in the app bundle, which calls the now deprecated SMLoginItemSetEnabled. Now this is already quite a pain to deal with if you're not using XCode, not to mention converting the headers being rather complicated when you're not experienced with doing this.
The "new" method (as of macOS 13) is using SMAppService. Can anyone explain how to use this? The documentation (for me anyway) is a not very clear about that and neither are examples that can be found all over the Internet.
My main question is:
Can I now use the SMAppService functions to add/remove a login item straight in my application, or is a helper tool still required?
Nice! I was one of the last Pascal holdouts on traditional Mac OS. I only moved to C when I joined Apple. I was never happy with that, and that’s one of the reasons why I’m a huge fan of Swift.
Sure. I’m gonna focus on Swift, because that’ll be the most useful to a general audience, but I’ll come back to your specific tools later on.
To add your app as a login item using SMAppService
:
-
Import the framework.
-
Create a service for your own app.
-
Call
register()
on it.
Here’s what that looks like:
import ServiceManagement func registerAppServiceAsLoginItem() throws { let service = SMAppService.mainApp try service.register() }
You do this straight from your app; there’s no indirection required.
Indeed, there was no indirection required by SMLoginItemSetEnabled
either. I suspect that this indirection was introduced by your tooling.
Speaking of which, do your tools have a mechanism for calling native C code? This is often called a foreign function interface (FFI).
If so, my advice would be to write an Objective-C version of the above code [1], wrap that in a C function, and then call that from your app’s actual code.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] Here’s the C header:
#ifndef SMAppServiceRegister_h #define SMAppServiceRegister_h extern int registerAppServiceAsLoginItem(void); #endif /* SMAppServiceRegister_h */
And here’s the Objective-C implementation:
#include "SMAppServiceRegister.h" @import ServiceManagement; extern int registerAppServiceAsLoginItem(void) { SMAppService * service = [SMAppService mainAppService]; BOOL success = [service registerAndReturnError:NULL]; return success; }