二、添加代码
在AppDelegate或有设置按钮的地方添加代码
@IBAction func appWhenLoginAction(_ sender: NSButton) {
let state = sender.state
if state == .on {
self.startupAppWhenLogin(startup: true)
} else {
self.startupAppWhenLogin(startup: false)
func startupAppWhenLogin(startup: Bool) {
let launcherAppIdentifier = "liwei.LwWallpapersHelper"
if SMLoginItemSetEnabled(launcherAppIdentifier as CFString, startup) {
if startup {
NSLog("添加登录项成功.")
} else {
NSLog("移除登录项成功.")
} else {
NSLog("添加失败.")
复制代码
在添加的target的AppDelegate添加代码
func applicationDidFinishLaunching(_ aNotification: Notification) {
var compoents = (Bundle.main.bundlePath as NSString).pathComponents as NSArray
compoents = compoents.subarray(with: NSMakeRange(0, compoents.count - 4)) as NSArray
let path = NSString.path(withComponents: compoents as! [String])
NSWorkspace.shared.launchApplication(path)
terminate()
@objc func terminate() {
NSApp.terminate(nil)
复制代码
然后导出APP运行一下,就可以了。
土豆崽xxx
macOS
-
1753
-
FreeCultureBoy
macOS
Node.js
-
6032
-
jasonboy7
React.js
Node.js
macOS