具有类似 Dropbox 行为的 StatusBarIcon
StatusBarIcon with Dropbox like behavior
我想制作一个只有 MenuBar 的应用程序,它每 30 秒检查一次 API 并根据标志的状态更改 StatusBarIcon。我一直在关注本教程:https://nsrover.wordpress.com/2014/10/10/creating-a-os-x-menubar-only-app/ 但我只能在 applicationDidFinishLaunching
函数中第一次做到这一点。
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
statusBarItem = NSStatusBar.system().statusItem(withLength: NSSquareStatusItemLength)
statusBarItem?.image = #imageLiteral(resourceName: "locked")
self.fetchOccupied { (occupied) in
if (occupied) {
self.statusBarItem?.image = #imageLiteral(resourceName: "locked")
} else {
self.statusBarItem?.image = #imageLiteral(resourceName: "unlocked")
}
}
}
在那之后,就像我说的,我想每 30 秒再次检查一次,而不必单击 StatusBarItem,但我不能将循环放在该函数中,因为那样应用程序将永远不会启动。
我不知道将循环放在哪里,因为我没有主 ViewController 或任何东西。我只有 AppDelegate class 和 XIB 文件。
在哪里可以创建函数来放置循环?也许我可以将该循环异步放入 applicationDidFinishLaunching
函数中?
谢谢。
var timer: DispatchSourceTimer?
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
self.timer = DispatchSource.makeTimerSource(flags: DispatchSource.TimerFlags(), queue: DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated))
self.timer?.scheduleRepeating(deadline: DispatchTime.now(), interval: DispatchTimeInterval.seconds(30), leeway: DispatchTimeInterval.seconds(0))
self.timer?.setEventHandler(handler: {
//loop body here
print("timer fire", separator: "", terminator: "\n")
})
self.timer?.resume()
}
您只需运行一个时钟,每 30 秒收到一次通知。
func applicationDidFinishLaunching(_ aNotification: Notification) {
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(updateClock), userInfo: nil, repeats: true)
}
func updateClock() {
let date = NSDate()
let calendar = NSCalendar.current
let components = calendar.dateComponents([.second],from:date as Date) as NSDateComponents
if components.second == 0 || components.second == 30 {
makeStatusMenu()
}
}
func makeStatusMenu() {
}
我想制作一个只有 MenuBar 的应用程序,它每 30 秒检查一次 API 并根据标志的状态更改 StatusBarIcon。我一直在关注本教程:https://nsrover.wordpress.com/2014/10/10/creating-a-os-x-menubar-only-app/ 但我只能在 applicationDidFinishLaunching
函数中第一次做到这一点。
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
statusBarItem = NSStatusBar.system().statusItem(withLength: NSSquareStatusItemLength)
statusBarItem?.image = #imageLiteral(resourceName: "locked")
self.fetchOccupied { (occupied) in
if (occupied) {
self.statusBarItem?.image = #imageLiteral(resourceName: "locked")
} else {
self.statusBarItem?.image = #imageLiteral(resourceName: "unlocked")
}
}
}
在那之后,就像我说的,我想每 30 秒再次检查一次,而不必单击 StatusBarItem,但我不能将循环放在该函数中,因为那样应用程序将永远不会启动。
我不知道将循环放在哪里,因为我没有主 ViewController 或任何东西。我只有 AppDelegate class 和 XIB 文件。
在哪里可以创建函数来放置循环?也许我可以将该循环异步放入 applicationDidFinishLaunching
函数中?
谢谢。
var timer: DispatchSourceTimer?
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
self.timer = DispatchSource.makeTimerSource(flags: DispatchSource.TimerFlags(), queue: DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated))
self.timer?.scheduleRepeating(deadline: DispatchTime.now(), interval: DispatchTimeInterval.seconds(30), leeway: DispatchTimeInterval.seconds(0))
self.timer?.setEventHandler(handler: {
//loop body here
print("timer fire", separator: "", terminator: "\n")
})
self.timer?.resume()
}
您只需运行一个时钟,每 30 秒收到一次通知。
func applicationDidFinishLaunching(_ aNotification: Notification) {
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(updateClock), userInfo: nil, repeats: true)
}
func updateClock() {
let date = NSDate()
let calendar = NSCalendar.current
let components = calendar.dateComponents([.second],from:date as Date) as NSDateComponents
if components.second == 0 || components.second == 30 {
makeStatusMenu()
}
}
func makeStatusMenu() {
}