About the function of starting the menu bar application for macOS when logging in using SwiftUI

Asked 2 years ago, Updated 2 years ago, 320 views

I am studying the menu bar application for macOS using SwiftUI.See here, but the implementation of SMLoginItemSetEnabled() is not SwiftUI, but StoryBoard.

In order to start the helper application when logging in, I registered the helper application to the login item using SMLoginItemSetEnabled(), but I didn't know how to call SMLoginItemSetEnabled(), so I wrote it on the body of View as follows.Now the function itself has been realized, but I don't know if it's written correctly. (I don't think it's correct.)

Therefore, I would appreciate it if you could tell me the correct way to import a framework and call a function, not just this case.Thank you for your cooperation.

import SwiftUI
import Service Management

structureSettingsView:View {
    
    @ AppStorage("launchAtLogin") var launchAtLogin=false
    
    private struct Constants {
        // Helper Application Bundle Identifier
        static let helperBundleID="jp.xxxxxxx.AutoLauncher"
    }
    
    varbody:someView {
        
        let theStatus=SMLoginItemSetEnabled (Constants.helperBundleID as CFSstring, launchAtLogin)
        
        let_=print(theStatus)
        
        VStack {
            Toggle(isOn:$launchAtLogin){
                Text ("Launch at Login")
            } .frame (width: 600, height:350, alignment: .center)
        }
    }
}

swift macos

2022-10-15 09:42

1 Answers

No matter how short the code is or how related it is to View, it's disgusting to say that the code in View is naked, so I chose a different class with reference to here.


2022-10-15 09:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.