简体   繁体   中英

Minimal Cocoa/Swift menu bar app doesn't want to run

I'm trying to build a skeleton app in Swift where I basically only have a menu bar icon, and no window. Starting from a new Storyboard project in Xcode, it worked initially, but trying to get rid of the window, it doesn't seem to want to run anymore. I have the following:

import Cocoa
import AppKit

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var window = NSWindow()
    var statusBar = NSStatusBar.systemStatusBar()
    var statusBarItem : NSStatusItem = NSStatusItem()

    override func awakeFromNib() {
        statusBarItem = statusBar.statusItemWithLength(-1)
        statusBarItem.title = "Test"
    }

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        sleep(10);
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }

}

In AppDelegate.swift (based on this tutorial ). When running this through Xcode, I get some warnings:

2015-06-23 22:20:28.444 PENCloud[19491:3303755] Failed to connect (colorGridView) outlet from (NSApplication) to (NSColorPickerGridView): missing setter or instance variable
2015-06-23 22:20:28.444 PENCloud[19491:3303755] Failed to connect (view) outlet from (NSApplication) to (NSColorPickerGridView): missing setter or instance variable

From some Googling, it seems like I should be able to ignore these, but my statusBarItem no longer shows up. What am I missing?

You need to have main.swift with the code like below.

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {

    var statusBarItem : NSStatusItem!

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        statusBarItem = statusBar.statusItemWithLength(-1)
        statusBarItem.title = "Test"
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }

}

autoreleasepool { () -> () in
    let app = NSApplication.sharedApplication()
    let delegate = AppDelegate()
    app.delegate = delegate
    app.run()
}

The file name MUST be main.swift . Otherwise you will get the error, Expressions are not allowed at the top level , on the line of autoreleasepool.

I found the answer here: https://stackoverflow.com/a/26322464/338986

I have had the same problem before and I found out that when there is no storyboard or something else is missing, the app doesn't even start. I suggest setting up a new project, add your status bar code and then just do two things:

  • Add the "Application is UIElement (Agent)" key to your info.plist file and set it to true
  • Go to your storyboard, select the windowcontroller and unckeck "Initial view controller" on the right in the property inspector

If you have any other question regarding a status bar item I'd love to help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM