Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

iBeacon Receiver in Swift 2.0

More than 5 years have passed since last update.

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がアクティブである

xshirade
学んだことを他の人に理解してもらえる記事にすることが,自分の力になると思って書き出しています.内容の間違いやコードの改善点などありましたらコメントを頂ければ助かります.
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away