LoginSignup
0

More than 3 years have passed since last update.

Swift:MIDIのBluetooth構成ウィンドウを開く

Last updated at Posted at 2019-05-26

概要

MacとMIDI機器を接続する時に開くウィンドウ↓ をプログラムのコードから呼び出したかった。

ble.png

方法

CoreAudioKitCABTLEMIDIWindowControllerを使えば良いようです。

func open() {
    // すでに開いていた場合は新規にはインスタンスを作らないように注意
    if let window = NSApp.windows.first(where: { $0.title.contains("Bluetooth") }) {
        window.orderFrontRegardless()
        return
    }
    NSApp.activate(ignoringOtherApps: true)
    let wc = CABTLEMIDIWindowController()
    wc.window?.hidesOnDeactivate = false // ここが重要ポイント
    wc.showWindow(nil)
}

注意点として,hidesOnDeactivate = falseをしないとスクリーン上からはウィンドウが消えているのに、windowのインスタンスが残っておりクラッシュするということがあり得ます。また、CABTLEMIDIWindowController()をどこかで保持するのはやめた方が良さそうです。MIDI機器との接続が意図せず切れた時に、AppDelegateにてThread 1: EXC_BAD_ACCESSで原因不明死することがあったりします。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0