LoginSignup
16
17

More than 5 years have passed since last update.

iBeacon Receiver in Swift 2.0

Last updated at Posted at 2015-09-24

iBeacon Receiver in Swift 2.0

iBeaconを利用したアプリを開発する際に色々と調査が必要で面倒だったので,最小限のiBeacon受信用の実装(ViewController.swift)を公開します.

ViewController.swift

ViewController.swift
import UIKit
import CoreLocation

class FirstViewController: UIViewController, UIWebViewDelegate, CLLocationManagerDelegate {

    let manager: CLLocationManager = CLLocationManager()
    // replace uuid and identifier
    let region: CLBeaconRegion = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "ef5b1eff-7a78-4e1c-a63d-60b54c077561")!, identifier: "com.example")

    override func viewDidLoad() {
        super.viewDidLoad()
        manager.delegate = self
        // AlwaysAuthorization is required to receive iBeacon
        let status = CLLocationManager.authorizationStatus()
        if status != CLAuthorizationStatus.AuthorizedAlways {
            manager.requestAlwaysAuthorization()
        }
    }

    // MARK: - CLLocationManagerDelegate
    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        // checking authorization status for using user location to start or stop monitoring for region
        switch status {
        case CLAuthorizationStatus.AuthorizedAlways:
            print("didChangeAuthorizationStatus: startMonitoringForRegion")
            manager.startMonitoringForRegion(self.region)
        default:
            print("didChangeAuthorizationStatus: stopMonitoringForRegion")
            manager.stopMonitoringForRegion(self.region)
        }
    }

    func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion) {
        // checking if you are inside or outside of designated regions to start or stop ranging beacons in the region 
        switch state {
        case CLRegionState.Inside:
            print("didDetermineState: startRangingBeaconsInRegion")
            manager.startRangingBeaconsInRegion(self.region)
        case CLRegionState.Outside, CLRegionState.Unknown:
            print("didDetermineState: stopRangingBeaconsInRegion")
            manager.stopRangingBeaconsInRegion(self.region)
        }

    }

    func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
        // implement whatever you wanna do with recwived iBeacons like
        print(beacons)
    }
}

使い方

Single View Application でプロジェクトを立ち上げるとすぐにある,ViewController.swiftにこのコードをコピペすれば動くはずです.

後で調査

didDetermineStateでどのCLBeaconRegionを用いればいいのか簡単に判別する方法

現在のDelegateから渡される引数のregionからは判別できないような気がする.

P.S. #1

使い方

位置情報を利用するに当たり,利用目的をInfo.plistに登録する必要があります.
Key: NSLocationAlwaysUsageDescription
Type: String
Value: Your usage description is here ...

Bug

アプリを起動した際に以下の条件が満たされていても,iPadはiBeacon受信を自動で開始しない.
- 位置情報の利用が許可されている
- Bluetoothがアクティブである

16
17
0

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
16
17