2016年3月22日 星期二

一個好用的iOS BLE開源第三方framework

https://github.com/HarveyHu/BLEHelper

BLEHelper

一個可以優雅支援一對多BLE裝置(Bluetooth Low Energy)操作的開源框架。(swift 2.2)

安裝(Installation)

CocoaPods

如下設定你的Podfile:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'BLEHelper', '~> 1.0'
設定好後就執行這個指令:
$ pod install

Carthage

使用Carthage安裝,把下面這行加入你的Cartfile中:
github "HarveyHu/BLEHelper" ~> 1.0.0
設定好後就執行這個指令:
$ carthage update --platform iOS
Xcode的設定請參考Carthage的官方網頁。

用法(Usage)

先在你的檔案導入BLEHelper框架:
import BLEHelper
然後把它作為一個已經初始化的property:
let bleHelper = BLECentralHelper()

掃瞄(Scan)

掃瞄附近的BLE裝置:
bleHelper.scan(1.0, serviceUUID: nil) { (devices) -> (Void) in
            //TODO: show your devices
        }

Connect

用已存在的device物件來連線:
bleHelper.connect(yourPeripheral) { (peripheral, error) -> (Void) in
            //TODO: do something when connected
        }
用 deviceUUID (peripheral.identifier) 的陣列 來一次連線多顆裝置:
self.bleHelper.retrieve(deviceUUIDs: [deviceUUIDString], completion: {(peripheral, error) -> (Void) in
            if error != nil {
                prettyLog("error: \(error?.description)")
                completion?(success: false)
                return
            }
            prettyLog("connect with \(peripheral)")
        })

操作(Operation)

讀取(read):
bleHelper.readValue("yourDeviceUUID", serviceUUID: "yourServiceUUID", characteristicUUID: "youCharacteristicUUID") { (success) -> (Void) in
            prettyLog("is read success: \(success)")
    }
設定是否開啟通知(enable notification):
bleHelper.enableNotification(true, deviceUUID: "yourDeviceUUID", serviceUUID: "yourServiceUUID", characteristicUUID: "youCharacteristicUUID") { (success) -> (Void) in
        prettyLog("set notify success: \(success)")
    }
寫入(write):
let command = "yourCommand"
if let data = command.dataUsingEncoding(NSUTF8StringEncoding) {
        bleHelper.writeValue(data, deviceUUID: "yourDeviceUUID", serviceUUID: "yourServiceUUID", characteristicUUID: "youCharacteristicUUID", withResponse: true) { (success) -> (Void) in
            prettyLog("is write success: \(success)")
        }
    }

委派(Delegate)

首先要先在你的Class開頭宣告符合BLECentralHelperDelegate。
這個委派包含兩個Callback方法:
一個是在裝置斷線時callback:
func bleDidDisconnectFromPeripheral(peripheral: CBPeripheral) {
    //TODO: do something...
}
另一個是用來接收裝置所回傳的資料:
func bleCentralDidReceiveData(data: NSData?, peripheral: CBPeripheral, characteristic: CBCharacteristic) {
    //TODO: do something...
}

範例(Example)

用Xcode打開 BLEHelper.xcworkspace , 把 Scheme 設為 "BLEHelperExample." 後就可以在你的iPhone或iPad上面執行啦!

License

MIT License

2016年3月10日 星期四

修改預設物件中所包含的物件的外觀

//取得它的appearance代理物件,可以修改它允許的外觀
例如:
想要修改UIPageViewController中的pageControll的背景色
但它沒有開給你pageControl物件的接口,就可以用以下的方式取得
let pageControlAppearance = UIPageControl.appearanceWhenContainedInInstancesOfClasses([TutorialViewController.self])
pageControl.backgroundColor = UIColor.redColor()

2016年3月8日 星期二

2016年3月1日 星期二

Choosing a Developer Membership

Developer Program:
  1. Individuals(個人)
      • 可上架App Store
      • 提供100組uuid數作實機測試
      • 只可個人使用(單一帳號)
      • 99 USD
  2. Organizations(組織):要提供公司的D-U-N-S Number(鄧白氏編碼)才可申請
    1. Companys(公司)
      • 可上架App Store
      • 提供100組uuid數作實機測試
      • 可多人一起使用(多帳號)
      • 99 USD
    2. Enterprises(企業)
      • 不可上架App Store
      • 提供不限制實機安裝數目
      • 可多人一起使用(多帳號)
      • 299 USD

2016年2月19日 星期五

如何在自己的專案中加入其他專案

有時候你想要引用一個第三方專案,但卻發現它沒有整理到Carthage或Cocoapods中,
這時候你要怎麼辦呢?

其實只有一句話:「將它的xcodeproj檔拉到你的專案的navigator之中,再把相關的dependency設定好」就行了。

設定的方式也很簡單,(當下使用版本為Xcode 7.2.1)

  1. 到專案檔的Build Phrases頁籤下的Target Dependencies中按"+"加入你要用的第三方專案的target。
  2. 到專案檔的General頁籤下的Embedded Binaries中,加你的第三方framework,這時候下面的Linked Frameworks and Libraries 應該會順便自動加入,如果沒有的話,你再將它手動加入一次就好。
簡單二個步驟,Build Success!



2016年2月18日 星期四

Carthage好用到回不去了

Carthage跟Cocoapods不同,它是單純的幫你把framework準備好,要用的你再自己拉進去你的project之中,再也不用看到額外的workspace檔案了!

跟Cocoapods 一樣,Carthage也需要一個Cartfile來告訴它你想生成那些framework。
不過因為Carthage目前只支援git,所以格式也相對簡單
github "ReactiveCocoa/ReactiveCocoa"
這樣就可以抓到最近版的framework,但你也可以指定你要的版本或是branch:
# Require version 2.3.1 or later
github "ReactiveCocoa/ReactiveCocoa" >= 2.3.1

# Require version 1.x
github "Mantle/Mantle" ~> 1.0    # (1.0 or later, but less than 2.0)

# Require exactly version 0.4.1
github "jspahrsummers/libextobjc" == 0.4.1

# Use the latest version
github "jspahrsummers/xcconfigs"

# Use the branch
github "jspahrsummers/xcconfigs" "branch"

# Use a project from GitHub Enterprise
github "https://enterprise.local/ghe/desktop/git-error-translations"

# Use a project from any arbitrary server, on the "development" branch
git "https://enterprise.local/desktop/git-error-translations2.git" "development"

# Use a local project
git "file:///directory/to/project" "branch"
等你準備好Cartfile之後,用terminal在你的專案資料夾鍵入
carthage update --platform iOS
它就會幫你把framework建好放在./Carthage/Build的資料夾中(framework的source code會在./Carthage/Checkouts中)。
你將Build資料夾中的framework拉進你的Xcode的Linked Frameworks and Libraries之中就可以用了。

最後,雖然這樣就可以使用,但如果你想要Run在Simulator時會發現出現如下的錯誤
Command /bin/sh failed with exit code 1

所以如果你想要用Simulator做測試的話,要多做下列的步驟:
  1. 到你的application的target頁面選Build Phrases頁籤。
  2. 按下"+"選"New Run Script Phase"新增一個Run Script。
  3. 在shell下的黑色區域,輸入下列指令/usr/local/bin/carthage copy-frameworks
  4. 最後在input files把你的frameworks的部份加進去
$(SRCROOT)/Carthage/Build/iOS/YourFramework1.framework
$(SRCROOT)/Carthage/Build/iOS/YourFramework2.framework

大功告成,現在你可以看到Simulator彈出來嘍!

P.S.你也可以在update時使用下列參數
carthage update --platform iOS --use-submodules --no-use-binaries

第一個參數說明你只要建立iOS平台的資料夾。
第二個參數讓這個專案以git submodules的型態加入。
第三個參數確保你的framework是在本機Build出來的。

P.S.你也可以用bootstrap指令來抓frameworks
bootstrap和update的差別在於:
update是從你寫的Cartfile中抓取你想到的framework之後,才建立Cartfile.resolved檔案。
而bootstrap是不管你的Cartfile,直接去讀上次建立的Cartfile.resolved檔案,來抓frameworks。