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?

UILocalNotificationのregion通知(Geofencing)において、出た時もしくは入った時のみ通知を出す方法 #iOS #Swift

More than 3 years have passed since last update.

iPhone(Swift)でGeofencing!

通常SwiftでGeofencingをやろうとするとUILocalNotificationのscheduleLocalNotificationを活用した例が出てきます。

しかしこの状態だと出入りした時2つのイベントを拾ってしまいます。
場合によっては、入った時もしくは出た時のイベントのみ欲しいと時もあると思います。

AppDelegate.swift
//指定したフェンスに出入りした時に通知を出す

func notifications() {
    let coordinate = CLLocationCoordinate2DMake(character.latitude,character.longitude)
    let radius = 3000.0
    let identifier = character.id
    let notification = UILocalNotification()
    notification.regionTriggersOnce = false
    notification.alertBody = "指定したフェンスを出入りしたよ!"
    notification.regionTriggersOnce = true
    notification.region = CLCircularRegion(center: coordinate, radius: radius, identifier: identifier)

    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

解決方法

CLRegionのコードを読んだら定義を発見しました。

CLRegion.m
    /*
     *  notifyOnEntry
     *
     *  Discussion:
     *    App will be launched and the delegate will be notified via locationManager:didEnterRegion:
     *    when the user enters the region. By default, this is YES.
     */
    @available(iOS 7.0, *)
    public var notifyOnEntry: Bool

    /*
     *  notifyOnExit
     *
     *  Discussion:
     *    App will be launched and the delegate will be notified via locationManager:didExitRegion:
     *    when the user exits the region. By default, this is YES.
     */
    @available(iOS 7.0, *)
    public var notifyOnExit: Bool

CLCircularRegionの(notifyOnExit)もしくは(notifyOnEntry)をそれぞれ設定するだけ

.swift
let region = CLCircularRegion(center: coordinate, radius: radius, identifier: identifier)
    region.notifyOnExit = false
    region.notifyOnEntry = true

notifyOnExitとnotifyOnEntryを実装したコード

AppDelegate.swift
//指定したフェンスに入った時のみ通知をだす

func notifications() {
    let coordinate = CLLocationCoordinate2DMake(character.latitude,character.longitude)
    let radius = 3000.0
    let identifier = character.id
    let region = CLCircularRegion(center: coordinate, radius: radius, identifier: identifier)
    region.notifyOnExit = false
    region.notifyOnEntry = true
    let notification = UILocalNotification()
    notification.regionTriggersOnce = false
    notification.alertBody = "指定したフェンスに入ったよ!"
    notification.regionTriggersOnce = true
    notification.region = region

    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

参考資料

Remove fired Location-Based notification when user exits region

flatfisher
釣りが好きです
mixi
全ての人に心地よいつながりを
http://mixi.co.jp
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