使用Swift开发一个MacOS的菜单状态栏App

点击上方“程序员大咖”,选择“置顶公众号”

关键时刻,第一时间送达!


下面开始介绍如何使用Swift开发一个Mac Menu Bar(Status Bar) App。通过做一个简单的天气App。天气数据来源于OpenWeatherMap


完成后的效果如下: 




01

开始建立工程


打开Xcode,Create a New Project or File -  New - Project -  Application - Cocoa Application ( OS X 这一栏)。点击下一步。 




02

开始代码工作


1.打开MainMenu.xib,删除默认的windows和menu菜单。因为我们是状态栏app,不需要菜单栏,不需要主窗口。 




2.添加一个Menu菜单 




删除其中默认的2个子菜单选项,仅保留1个。并将保留的这个改名为“Quit”。


3.打开双视图绑定Outlet


  • 将Menu Outlet到AppDelegate,命名为statusMenu 




  • 将子菜单Quit绑定Action到AppDelegate,命名为quitClicked 




  • 你可以删除 @IBOutlet weak var window: NSWindow! ,这个app中用不上。


4.代码


  • 在AppDelegate.swift中statusMenu下方添加


let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength)


  • applicationDidFinishLaunching函数中添加


statusItem.title = "WeatherBar"

statusItem.menu = statusMenu


  • 在quitClicked中添加


NSApplication.sharedApplication().terminate(self)


  • 此时你的代码应该如下




运行,你可以看到一个状态栏了。


03

进阶一步,让App变得更好


你应该注意到了,当你运行后,底部Dock栏里出现了一个App启动的Icon。但实际上我们也不需要这个启动icon,打开Info,添加 “Application is agent (UIElement)”为YES。 




运行一下,不会出现dock启动icon了。


04

添加状态栏Icon


状态栏icon尺寸请使用18x18, 36x36(@2x), 54x54(@3x),添加这1x和2x两张图到Assets.xcassets中。 




在applicationDidFinishLaunching中,修改为如下:


let icon = NSImage(named: "statusIcon")

icon?.template = true // best for dark mode

statusItem.image = icon

statusItem.menu = statusMenu


运行一下,你应该看到状态栏icon了。


05

重构下代码


如果我们进一步写下去,你会发现大量代码在AppDelegate中,我们不希望这样。下面我们为Menu创建一个Controller来管理。


  • 新建一个NSObject的StatusMenuController.swift, File - New File - OS X Source - Cocoa Class - Next




代码如下:


// StatusMenuController.swift


import Cocoa


class StatusMenuController: NSObject {

    @IBOutlet weak var statusMenu: NSMenu!


    let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength)


    override func awakeFromNib() {

        let icon = NSImage(named: "statusIcon")

        icon?.template = true // best for dark mode

        statusItem.image = icon

        statusItem.menu = statusMenu

    }


    @IBAction func quitClicked(sender: NSMenuItem) {

        NSApplication.sharedApplication().terminate(self)

    }

}


  • 还原AppDelegate,修改为如下:


// AppDelegate.swift


import Cocoa


@NSApplicationMain

class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidFinishLaunching(aNotification: NSNotification) {

        // Insert code here to initialize your application

    }

    func applicationWillTerminate(aNotification: NSNotification) {

        // Insert code here to tear down your application

    }

}


注意,因为删除了AppDelegate中的Outlet注册,所以你需要重新连Outlet,但在这之前我们需要先做一件事。(你可以试试连接StatusMenuController中的Outlet,看看会怎么样?)


  • 打开MainMenu.xib,添加一个Object。 




  • 将该Object的Class指定为StatusMenuController 




  • 重建Outlet到StatusMenuController,注意删除之前连接到AppDelegate的Outlet 




当MainMenu.xib被初始化的时候,StatusMenuController下的awakeFromNib将会被执行,所以我们在里面做初始化工作。


运行一下,保证你全部正常工作了。


06

天气API


我们使用 OpenWeatherMap的天气数据,所以你得注册一个账号,获取到免费的API Key。


  • 添加WeatherAPI.swift, File - New File - OS X Source - Swift File - WeatherAPI.swift,加入如下代码,并使用你自己的API Key。


import Foundation


class WeatherAPI {

    let API_KEY = "your-api-key-here"

    let BASE_URL = "http://api.openweathermap.org/data/2.5/weather"


    func fetchWeather(query: String) {

        let session = NSURLSession.sharedSession()

        // url-escape the query string we're passed

        let escapedQuery = query.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())

        let url = NSURL(string: "(BASE_URL)?APPID=(API_KEY)&units=imperial&q=(escapedQuery!)")

        let task = session.dataTaskWithURL(url!) { data, response, err in

            // first check for a hard error

            if let error = err {

                NSLog("weather api error: (error)")

            }


            // then check the response code

            if let httpResponse = response as? NSHTTPURLResponse {

                switch httpResponse.statusCode {

                case 200: // all good!

                    let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding) as! String

                    NSLog(dataString)

                case 401: // unauthorized

                    NSLog("weather api returned an 'unauthorized' response. Did you set your API key?")

                default:

                    NSLog("weather api returned response: %d %@", httpResponse.statusCode, NSHTTPURLResponse.localizedStringForStatusCode(httpResponse.statusCode))

                }

            }

        }

        task.resume()

    }

}


  • 添加一个Update子菜单到Status Menu。 




绑定Action到StatusMenuController.swift,取名为updateClicked


  • 开始使用WeatherAPI, 在StatusMenuController中let statusItem下面加入: 

let weatherAPI = WeatherAPI(), 

  • 在updateClicked中加入: 

weatherAPI.fetchWeather("Seattle")


注意OSX 10.11之后请添加NSAppTransportSecurity,保证http能使用。


运行一下,然后点击Update菜单。你会收到一个json格式的天气数据。


  • 我们再调整下StatusMenuController代码, 添加一个updateWeather函数,修改后如下:


import Cocoa


class StatusMenuController: NSObject {

    @IBOutlet weak var statusMenu: NSMenu!


    let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength)

    let weatherAPI = WeatherAPI()


    override func awakeFromNib() {

        statusItem.menu = statusMenu

        let icon = NSImage(named: "statusIcon")

        icon?.template = true // best for dark mode

        statusItem.image = icon

        statusItem.menu = statusMenu


        updateWeather()

    }


    func updateWeather() {

        weatherAPI.fetchWeather("Seattle")

    }


    @IBAction func updateClicked(sender: NSMenuItem) {

        updateWeather()

    }


    @IBAction func quitClicked(sender: NSMenuItem) {

        NSApplication.sharedApplication().terminate(self)

    }

}


07

解析JSON


你可以使用 SwiftyJSON,但本次我们先不使用第三方库。我们得到的天气数据如下:


{

    "coord": {

        "lon": -122.33,

        "lat": 47.61

    },

    "weather": [{

        "id": 800,

        "main": "Clear",

        "description": "sky is clear",

        "icon": "01n"

    }],

    "base": "cmc stations",

    "main": {

        "temp": 57.45,

        "pressure": 1018,

        "humidity": 59,

        "temp_min": 53.6,

        "temp_max": 62.6

    },

    "wind": {

        "speed": 2.61,

        "deg": 19.5018

    },

    "clouds": {

        "all": 1

    },

    "dt": 1444623405,

    "sys": {

        "type": 1,

        "id": 2949,

        "message": 0.0065,

        "country": "US",

        "sunrise": 1444659833,

        "sunset": 1444699609

    },

    "id": 5809844,

    "name": "Seattle",

    "cod": 200

}


  • 在WeatherAPI.swift添加天气结构体用于解析son


struct Weather {

    var city: String

    var currentTemp: Float

    var conditions: String

}


  • 解析son


 func weatherFromJSONData(data: NSData) -> Weather? {

        typealias JSONDict = [String:AnyObject]

        let json : JSONDict


        do {

            json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! JSONDict

        } catch {

            NSLog("JSON parsing failed: (error)")

            return nil

        }


        var mainDict = json["main"] as! JSONDict

        var weatherList = json["weather"] as! [JSONDict]

        var weatherDict = weatherList[0]


        let weather = Weather(

            city: json["name"] as! String,

            currentTemp: mainDict["temp"] as! Float,

            conditions: weatherDict["main"] as! String

        )


        return weather

    }


  • 修改fetchWeather函数去调用weatherFromJSONData


let task = session.dataTaskWithURL(url!) { data, response, error in

        // first check for a hard error

    if let error = err {

        NSLog("weather api error: (error)")

    }


    // then check the response code

    if let httpResponse = response as? NSHTTPURLResponse {

        switch httpResponse.statusCode {

        case 200: // all good!

            if let weather = self.weatherFromJSONData(data!) {

                NSLog("(weather)")

            }

        case 401: // unauthorized

            NSLog("weather api returned an 'unauthorized' response. Did you set your API key?")

        default:

            NSLog("weather api returned response: %d %@", httpResponse.statusCode, NSHTTPURLResponse.localizedStringForStatusCode(httpResponse.statusCode))

        }

    }

}


如果此时你运行,你会收到


2016-07-28 11:25:08.457 WeatherBar[49688:1998824] Optional(WeatherBar.Weather(city: "Seattle", currentTemp: 51.6, conditions: "Clouds"))


  • 给Weather结构体添加一个description


struct Weather: CustomStringConvertible {

    var city: String

    var currentTemp: Float

    var conditions: String


    var description: String {

        return "(city): (currentTemp)F and (conditions)"

    }

}


再运行试试。


08

Weather用到Controller中


  • 在 WeatherAPI.swift中增加delegate协议


protocol WeatherAPIDelegate {

    func weatherDidUpdate(weather: Weather)

}


  • 声明var delegate: WeatherAPIDelegate?


  • 添加初始化


init(delegate: WeatherAPIDelegate) {

    self.delegate = delegate

}


  • 修改fetchWeather


let task = session.dataTaskWithURL(url!) { data, response, error in

    // first check for a hard error

    if let error = err {

        NSLog("weather api error: (error)")

    }


    // then check the response code

    if let httpResponse = response as? NSHTTPURLResponse {

        switch httpResponse.statusCode {

        case 200: // all good!

            if let weather = self.weatherFromJSONData(data!) {

                self.delegate?.weatherDidUpdate(weather)

            }

        case 401: // unauthorized

            NSLog("weather api returned an 'unauthorized' response. Did you set your API key?")

        default:

            NSLog("weather api returned response: %d %@", httpResponse.statusCode, NSHTTPURLResponse.localizedStringForStatusCode(httpResponse.statusCode))

        }

    }

}


  • StatusMenuController添加WeatherAPIDelegate


class StatusMenuController: NSObject, WeatherAPIDelegate {

...

  var weatherAPI: WeatherAPI!


  override func awakeFromNib() {

    ...

    weatherAPI = WeatherAPI(delegate: self)

    updateWeather()

  }

  ...

  func weatherDidUpdate(weather: Weather) {

    NSLog(weather.description)

  }

  ...


  • Callback实现,修改WeatherAPI.swift中fetchWeather: 

func fetchWeather(query: String, success: (Weather) -> Void) { 

修改fetchWeather内容


let task = session.dataTaskWithURL(url!) { data, response, error in

    // first check for a hard error

    if let error = err {

        NSLog("weather api error: (error)")

    }


    // then check the response code

    if let httpResponse = response as? NSHTTPURLResponse {

        switch httpResponse.statusCode {

        case 200: // all good!

            if let weather = self.weatherFromJSONData(data!) {

                success(weather)

            }

        case 401: // unauthorized

            NSLog("weather api returned an 'unauthorized' response. Did you set your API key?")

        default:

            NSLog("weather api returned response: %d %@", httpResponse.statusCode, NSHTTPURLResponse.localizedStringForStatusCode(httpResponse.statusCode))

        }

    }

}


  • 在controller中


func updateWeather() {

    weatherAPI.fetchWeather("Seattle, WA") { weather in

        NSLog(weather.description)

    }

}


运行一下,确保都正常。


09

显示天气



在MainMenu.xib中添加子菜单 “Weather”(你可以添加2个Separator Menu Item用于子菜单分割线) 




在updateWeather中,替换NSLog:


if let weatherMenuItem = self.statusMenu.itemWithTitle("Weather") {

    weatherMenuItem.title = weather.description

}


运行一下,看看天气是不是显示出来了。


10

创建一个天气视图


打开MainMenu.xib,拖一个Custom View进来。


  • 拖一个Image View到Custom View中,设置ImageView宽高度为50。 




  • 拖两个Label进来,分别为City和Temperature 




  • 创建一个名为WeatherView的NSView,New File ⟶ OS X Source ⟶ Cocoa Class 

  • 在MainMenu.xib中,将Custom View的Class指定为WeatherView 




  • 绑定WeatherView Outlet:


import Cocoa


class WeatherView: NSView {

    @IBOutlet weak var imageView: NSImageView!

    @IBOutlet weak var cityTextField: NSTextField!

    @IBOutlet weak var currentConditionsTextField: NSTextField!

}


  • 并添加update:


func update(weather: Weather) {

    // do UI updates on the main thread

    dispatch_async(dispatch_get_main_queue()) {

        self.cityTextField.stringValue = weather.city

        self.currentConditionsTextField.stringValue = "(Int(weather.currentTemp))°F and (weather.conditions)"

        self.imageView.image = NSImage(named: weather.icon)

    }

}


注意这里使用dispatch_async调用UI线程来刷新UI,因为后面调用此函数的数据来源于网络请求子线程。


  • StatusMenuController添加weatherView outlet


class StatusMenuController: NSObject {

    @IBOutlet weak var statusMenu: NSMenu!

    @IBOutlet weak var weatherView: WeatherView!

    var weatherMenuItem: NSMenuItem!

    ...


  • 子菜单Weather绑定到视图


weatherMenuItem = statusMenu.itemWithTitle("Weather")

weatherMenuItem.view = weatherView


  • update中:


func updateWeather() {

    weatherAPI.fetchWeather("Seattle, WA") { weather in

        self.weatherView.update(weather)

    }

}


运行一下。


11

添加天气图片


先添加天气素材到Xcode,天气素材可以在http://openweathermap.org/weather-conditions 这里找到。这里我已经提供了一份icon zip, 解压后放Xcode。




  • WeatherAPI.swift的Weather struct中,添加 var icon: String


  • 在weatherFromJSONData中:


let weather = Weather(

    city: json["name"] as! String,

    currentTemp: mainDict["temp"] as! Float,

    conditions: weatherDict["main"] as! String,

    icon: weatherDict["icon"] as! String

)


  • 在weatherFromJSONData:


let weather = Weather(

    city: json["name"] as! String,

    currentTemp: mainDict["temp"] as! Float,

    conditions: weatherDict["main"] as! String,

    icon: weatherDict["icon"] as! String

)


  • 在WeatherView的update中:


imageView.image = NSImage(named: weather.icon)


运行一下,Pretty!




12

添加设置


在MainMenu.xib MenuItem中,添加一个Menu Item命名为“Preferences…” 

并绑定action,命名为“preferencesClicked”


  • 添加NSWindowController命名为PreferencesWindow.swift New - File - OS X Source - Cocoa Class , 勾选同时创建XIB.在XIB中添加Label和Text Field。效果如下: 




Outlet cityTextField到PreferencesWindow.swift


  • 在PreferencesWindow.swift中添加:


override var windowNibName : String! {

    return "PreferencesWindow"

}


  • windowDidLoad()中修改:


self.window?.center()

self.window?.makeKeyAndOrderFront(nil)

NSApp.activateIgnoringOtherApps(true)


  • 最终PreferencesWindow.swift如下:


import Cocoa


class PreferencesWindow: NSWindowController {

    @IBOutlet weak var cityTextField: NSTextField!


    override var windowNibName : String! {

        return "PreferencesWindow"

    }


    override func windowDidLoad() {

        super.windowDidLoad()


        self.window?.center()

        self.window?.makeKeyAndOrderFront(nil)

        NSApp.activateIgnoringOtherApps(true)

    }

}


  • StatusMenuController.swift中添加preferencesWindow 

var preferencesWindow: PreferencesWindow!


  • awakeFromNib中,注意在updateWeather()之前: 

preferencesWindow = PreferencesWindow()


  • preferencesClicked中: 

preferencesWindow.showWindow(nil)


  • 下面为 preferences window 添加NSWindowDelegate,刷新视图。 

class PreferencesWindow: NSWindowController, NSWindowDelegate { 

并增加


func windowWillClose(notification: NSNotification) {

    let defaults = NSUserDefaults.standardUserDefaults()

    defaults.setValue(cityTextField.stringValue, forKey: "city")

}


增加协议:


protocol PreferencesWindowDelegate {

    func preferencesDidUpdate()

}


增加delegate:


var delegate: PreferencesWindowDelegate?


在windowWillClose最下面调用


delegate?.preferencesDidUpdate()


回到StatusMenuController中,添加PreferencesWindowDelegate


class StatusMenuController: NSObject, PreferencesWindowDelegate {


实现代理:


func preferencesDidUpdate() {

    updateWeather()

}


awakeFromNib中:


preferencesWindow = PreferencesWindow()

preferencesWindow.delegate = self


  • 在StatusMenuController中增加默认城市 

let DEFAULT_CITY = “Seattle, WA”


  • 修改updateWeather


func updateWeather() {

    let defaults = NSUserDefaults.standardUserDefaults()

    let city = defaults.stringForKey("city") ?? DEFAULT_CITY

    weatherAPI.fetchWeather(city) { weather in

        self.weatherView.update(weather)

    }

}


  • 咱们也可以在PreferencesWindow.swift windowDidLoad中设置city默认值


let defaults = NSUserDefaults.standardUserDefaults()

let city = defaults.stringForKey("city") ?? DEFAULT_CITY

cityTextField.stringValue = city


运行。一切OK。


其他: 

- 你也可以试试使用NSRunLoop.mainRunLoop().addTimer(refreshTimer!, forMode: NSRunLoopCommonModes) 来定时updateWeather. 

- 试试点击天气后跳转到天气中心 NSWorkspace.sharedWorkspace().openURL(url: NSURL)) 

- 完整工程: WeatherBar


参考

  • http://footle.org/WeatherBar/

  • 来自:CSDN-Cocos2der

  • http://blog.csdn.net/cocos2der/article/details/52054107

  • 程序员大咖整理发布,转载请联系作者获得授权

【点击成为Python大神】

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Xcode中,你可以使用Process类来运行终端命令。 例如,下面的代码可以用来执行终端命令 `ls`: ``` import Foundation let task = Process() task.launchPath = "/usr/bin/env" task.arguments = ["ls"] task.launch() task.waitUntilExit() ``` 这段代码会创建一个新的 `Process` 实例,并设置它的 `launchPath` 属性为 `/usr/bin/env`,这是 Unix 系统中用于查找并执行命令的工具。然后,我们将要执行的命令(在这个例子中是 `ls`)作为参数传给进程。最后,我们调用 `launch` 方法来启动进程,并调用 `waitUntilExit` 方法来等待进程结束。 希望这能帮到你! ### 回答2: 在macOS使用Xcode构建macOS应用程序,并通过Swift直接运行终端指令,可以通过使用Process类实现。 Process是Foundation框架提供的一个类,它允许在应用程序内部执行命令行指令。要使用Process执行终端指令,需要以下步骤: 1. 导入Foundation模块,以便可以使用Process类: `import Foundation` 2. 创建一个Process实例: `let process = Process()` 3. 设置Process的属性,包括执行路径和命令行参数。例如,如果想执行ls命令,可以设置`process.launchPath`为"/bin/ls",如果有参数可以设置`process.arguments`。 4. 调用Process的launch()方法来执行终端指令: `process.launch()` 5. 如果需要获取命令行输出,可以使用Pipe来捕获标准输出,然后将其连接到Process的standardOutput属性上: ```swift let pipe = Pipe() process.standardOutput = pipe ``` 6. 等待命令执行完成,并获取输出结果: ```swift process.waitUntilExit() let data = pipe.fileHandleForReading.readDataToEndOfFile() if let output = String(data: data, encoding: .utf8) { print(output) } ``` 以上步骤可以在macOS应用程序中的任何地方执行,可以根据需要将其包装成一个函数或方法。 这样,通过使用Xcode构建的macOS应用程序可以直接运行终端指令,并获取执行结果。请注意,某些命令可能需要在应用程序的沙盒权限中运行。 ### 回答3: 在macOS使用Xcode构建macOS应用程序,并通过Swift直接运行终端指令,你可以通过使用Shell命令来实现。下面是一个简单的示例,说明如何在Swift中执行终端指令: 1. 创建一个新的macOS应用程序项目,并打开Xcode。 2. 在Xcode项目中选择合适的位置,创建一个新的Swift文件(例如,你可以将其命名为TerminalCommand.swift)。 3. 在TerminalCommand.swift文件中,你可以使用以下代码来执行终端指令: ```swift import Foundation func runTerminalCommand(command: String) -> String { let task = Process() let pipe = Pipe() task.launchPath = "/usr/bin/env" task.arguments = ["bash", "-c", command] task.standardOutput = pipe task.launch() let data = pipe.fileHandleForReading.readDataToEndOfFile() let output = String(data: data, encoding: .utf8) return output ?? "Command execution failed" } // 示例:执行终端指令"ls -l"并将结果打印出来 let commandOutput = runTerminalCommand(command: "ls -l") print(commandOutput) ``` 4. 现在,你可以在你的macOS应用程序中的任何地方调用`runTerminalCommand`函数,并传入你想要执行的终端指令作为参数。函数将返回一个包含终端指令输出的字符串。 5. 在你的macOS应用程序中,构建和运行项目。当应用程序运行时,终端指令将会执行,并将结果打印在Xcode控制台窗口中。 总结而言,你可以利用Swift的Foundation框架中的Process类和Pipe类,通过调用Shell命令来在macOS应用程序中执行终端指令。此代码示例中,我们使用了`/usr/bin/env`作为任务的启动路径,并传入一个bash命令作为参数。你可以根据需要使用其他启动路径和不同的参数来执行终端指令。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值