LoginSignup
4
5

More than 5 years have passed since last update.

ビーコン領域への接近を知らせる(CoreLocation::CLLocationManager.startMonitoringForRegion)

Posted at

ビーコン領域の観測

領域観測準備

Info.plistのRequired device capabilitiesにlocation-services追加
Info.plistのInformation Property ListにNSLocationAlwaysUsageDescriptionを追加して、許可を求めるダイアログに表示する文字列を設定
画面左のファイル一覧からプロジェクト(一番上のルート)を選択し、Capabilitiesタブ->Background ModesをONにし、Location updatesをチェック

ViewController.swift
import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    var myLocationManager: CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()

        if !CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion) {
            return
        }

        let status = CLLocationManager.authorizationStatus()
        if status == CLAuthorizationStatus.Restricted || status == CLAuthorizationStatus.Denied {
            return
        }

        myLocationManager = CLLocationManager()
        myLocationManager.delegate = self

        if status == CLAuthorizationStatus.NotDetermined {
            myLocationManager.requestAlwaysAuthorization()
        }

        if !CLLocationManager.locationServicesEnabled() {
            return
        }

        if UIApplication.sharedApplication().backgroundRefreshStatus != UIBackgroundRefreshStatus.Available {
            return
        }

ビーコン領域の生成・登録

ViewController.swift
        if UIApplication.sharedApplication().backgroundRefreshStatus != UIBackgroundRefreshStatus.Available {
            return
        }

        let myProximityUUID = NSUUID.init(UUIDString: "YourUUID")
        let beaconRegion = CLBeaconRegion.init(proximityUUID: myProximityUUID!, major: 0, minor: 0, identifier: "YourUniqueIdentifier")
        beaconRegion.notifyOnEntry = true
        beaconRegion.notifyOnExit = true

        myLocationManager.startMonitoringForRegion(beaconRegion)

        myLocationManager.requestStateForRegion(beaconRegion)

状態表示用ラベル作成

ViewController.swift
class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var statusLabel: UILabel!

境界横断イベントの処理

ViewController.swift
        myLocationManager.requestStateForRegion(beaconRegion)

    }

    func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion) {
        if state == CLRegionState.Inside {
            statusLabel.text = "範囲内"
        } else {
            statusLabel.text = "範囲外"
        }
    }

    func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
        statusLabel.text = "侵入"
    }

    func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
        statusLabel.text = "離脱"
    }

    func locationManager(manager: CLLocationManager, monitoringDidFailForRegion region: CLRegion?, withError error: NSError) {
        statusLabel.text = "失敗"
    }

ビーコン領域の登録時に、ビーコン領域のnotifyEntryStateOnDisplayプロパティの値をtrue(そしてnotifyOnEntryプロパティをfalse)に設定すると、ユーザがデバイスのディスプレイをオンにするまで、進入の通知を遅らせる

ユーザに通知が何回も送信されるのを防止するには、領域への進入ごとに1回だけ位置に関する通知を配信するようにする

参考情報

位置情報とマッププログラミングガイド

4
5
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
4
5