macOSでCoreBluetoothを使う際に注意しないといけないこと(Swift 3)

iOSと仕様が若干異なるので注意が必要。

CBCentralManagerにisScanningが無い

何故か無い。無いので自分で変数を用意して管理するしかない。

CBCentralManagerのdelegateの関数が呼ばれない

何故か初期化の際にdelegateを渡す必要があるみたい。 以下のようにするとdelegateの関数が呼ばれず動かない。

var centralManager = CBCentralManager()

override init() {
    super.init()
    centralManager.delegate = self //ここで渡しても呼ばれない
}

以下のようにイニシャライザでselfを渡すとちゃんと呼ばれる。

var centralManager:CBCentralManager!
override init() {
    super.init()
    centralManager = CBCentralManager(delegate: self, queue: nil)
}

didReadRSSIの関数が違う

以下のようにOSで処理を分けたりしないといけない。

#if os(OSX)
open func peripheralDidUpdateRSSI(_ peripheral: CBPeripheral, error: Error?) {
    if let orp = availableOrphe(peripheral: peripheral){
        orp.RSSI = peripheral.rssi!
        delegate?.didUpdateRSSI?(orphe: orp)
    }
}
#else
open func peripheral(_ peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: Error?){
    if let orp = availableOrphe(peripheral: peripheral){
        orp.RSSI = RSSI
        delegate?.didUpdateRSSI?(orphe: orp)
        NotificationCenter.default.post(name: .OrpheDidUpdateRSSINotification, object: nil, userInfo: [OrpheDataUserInfoKey:orp])
    }
}
#endif
»


Swift環境にGoogle AnalyticsをCocoaPodsで導入する

環境はXcode8.2.1, Swift3.0です。

CocoaPodsで必要なライブラリをインストール

インストールとかのやり方はこちらのページに丁寧に書いてあります。

このメモには簡単な手順を記載しています。

既にCocoaPodsでライブラリを入れたりしてるプロジェクトを前提にしてます。

ターゲットに “pod ‘Google/Analytics’“を追加してpod update

設定ファイルを取得

ここで必要事項を記入し、設定ファイルを取得する。

取得したGoogleService-Info.plistファイルをプロジェクトにドラッグ・アンド・ドロップ。コピーする。

Bridging-Headerを作成

iOSアプリでPureDataをサウンドエンジンとして利用するの「Bridging-Headerを作成」に書いてます。 Google AnalyticsのライブラリはObjective-Cで書かれているので。

設定ファイルの読み込み

AppDelegateでやってます。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    var configureError: NSError?
    GGLContext.sharedInstance().configureWithError(&configureError)
    assert(configureError == nil, "Error configuring Google services: \(configureError)")
    
    return true
}

トラッキングする関数を作る

UIViewControllerにextensionを追加するとトラックしたいViewControllerで関数を呼ぶだけで監視できて便利。 viewWillAppear辺りで呼べばOK。

extension UIViewController{
    func trackScreenView() {
        let tracker = GAI.sharedInstance().defaultTracker
        tracker?.set(kGAIScreenName, value: NSStringFromClass(type(of: self)))
        let builder: NSObject = GAIDictionaryBuilder.createScreenView().build()
        tracker?.send(builder as! [NSObject : AnyObject])
    }
}

これだけで完了。 あとはGoogle Analyticsの管理画面で監視できます。

»


tableViewの背景に画像を表示する

参考サイト:Transparent Table View With a Background Image

参考のサイトでは表示する画像の位置や大きさが自由に変更しづらかったので tableViewのbackgroundViewにUIViewを設定してそのviewにaddSubviewで画像を貼り付けることでやりたいことが出来た。

override func viewDidLayoutSubviews() {
        
        // make UIImageView instance
        let imgW:CGFloat = 200
        let imgH:CGFloat = imgW
        let imgX = (self.tableView.frame.width-imgW)/2
        let imgY = (self.tableView.frame.height-imgH)/2
        let imageView = UIImageView(frame: CGRect(x: imgX, y: imgY, width: imgW, height: imgH))
        // read image
        let image = UIImage(named: "imgae_name")
        // set image to ImageView
        imageView.image = image
        imageView.alpha = 1
        imageView.contentMode = .scaleAspectFit
        self.tableView.backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.frame.width, height: self.tableView.frame.height))
        self.tableView.backgroundView!.addSubview(imageView)
    }
    
»


pd-for-iosを使うにあたってつまづいたこと

iOSアプリでPureDataをサウンドエンジンとして利用するというメモを書いた後に機能を追加していく上で色々つまづいてしまったのでメモ。 テストするためにボタンをタップするとPdいろいろ送るプログラムを書きました。 コードはこちら

Pdが正しくlistを受け取ってくれない

原因はlibpd経由でlistを送った場合は最初に”list”という文字列が入ってくる仕様にあった。なのでレシーブオブジェクトの直下に[route list]を繋いでやる必要がある。 ほかのsendBangToReceiverとかsendFloatは値がそのまま送られるのでrouteで振り分ける必要はない。

サブパッチにしたところの処理が走らない

最初書いてたファイルを開く処理がこちら

PdBase.openFile("pd-patch/main.pd" path:Bundle.main.resourcePath)

のようにファイル名の引数にパスを渡すとmain.pdと同じ階層にあるサブパッチでも認識されないようで全く動作しなくなる。 正しくは

PdBase.openFile("main.pd", path: Bundle.main.resourcePath!+"/pd-patches")
»


.