# Geofences

> Add places in the console and get an event when someone with your app enters or leaves one, with the opt-in geofence module for iOS and Android. The SDK never asks for location permission and never sends the device's location.

SDK 0.7.0 and later. Not released yet.

A geofence is a circle around a place, such as a store. When someone with your app enters or leaves one, the SDK sends a `track` event, `$geofence_enter` or `$geofence_exit`. You use those events like any other: in [segments](/docs/concepts/events-and-attributes), as an [in-app message trigger](/docs/in-app-messages/triggers-and-delivery), and in agent goals and targeting. There is no separate geofence campaign type.

## How it protects privacy

- The SDK never asks for location permission. Your app asks, with its own explanation, before it turns geofences on.
- The SDK reads no location until your app calls `enableGeofences()`.
- The SDK downloads the app's whole geofence list. It never sends the device's location to Herald to find nearby places.
- What reaches Herald is only the crossing: the geofence's id and name, as an event. The SDK sends no coordinates.
- Geofences are a separate module. An app that doesn't add it never links CoreLocation (iOS) or Play Services location (Android).

## 1. Add geofences in the console

Open Settings, **Geofences**, with the app in view. Each geofence has a name, a latitude and longitude, and a radius from 100 meters to 10 kilometers. An app can have up to 100. Type the coordinates (copy them from any map app) and use **Open in map** to check the point. Only admins can add, change or delete geofences.

## 2. Add the module

:::tabs
::tab ios
Add the `HeraldGeofence` library product from the Herald Swift package to your app target, next to `HeraldSDK`.

```swift
.product(name: "HeraldGeofence", package: "herald-ios"),
```
::tab android
```kotlin
// app/build.gradle.kts
dependencies {
    implementation("com.github.ender1311.herald-android:herald-geofence:0.7.0")
}
```

`herald-geofence` depends on Play Services location (`play-services-location` 21.3.0) and adds no permissions to your manifest. Your app declares `ACCESS_FINE_LOCATION` and `ACCESS_BACKGROUND_LOCATION` itself.
:::

## 3. Get permission, then turn it on

Ask for background location in your app's own flow: "Always" on iOS (with `NSLocationAlwaysAndWhenInUseUsageDescription` and `NSLocationWhenInUseUsageDescription` in Info.plist), and fine plus background location on Android 10 and later. Then call `enableGeofences()` after `configure`, at every launch, including when the system starts the app in the background for a crossing. On Android this is also what registers the geofences again after the phone restarts. Without the permission it logs a warning and does nothing.

:::tabs
::tab ios
```swift
import HeraldGeofence

// In application(_:didFinishLaunchingWithOptions:), after Herald.configure, once the person granted Always:
Herald.shared.enableGeofences()
```
::tab android
```kotlin
import com.herald.geofence.enableGeofences

// In Application.onCreate, after Herald.configure, once the person granted fine and background location:
Herald.enableGeofences()
```
:::

`disableGeofences()` stops monitoring until the next `enableGeofences()`. Call it when the person turns location features off in your app.

## What the SDK monitors

| | iOS | Android |
|---|---|---|
| How | Core Location region monitoring | Play Services `GeofencingClient` |
| How many | The 20 nearest (Apple's limit, minus regions your app monitors itself) | All of them: an app has at most 100, which is Google's limit |
| Picking the nearest | By distance to the edge, from the last known location, re-picked on significant location change when the app has more than fit | Not needed |
| Already inside when it starts | No event until the person leaves and comes back | Same |
| After a restart, or Location turned off and on | Keeps monitoring: iOS keeps the regions | Stops until the app next opens, which registers them again. Android drops an app's geofences on restart and when Location is turned off. |

The SDK fetches the list at each `enableGeofences()` and keeps the last one on the device, so a launch without a network uses it. Changes in the console reach a device the next time the app starts.

## The events

| Name | Properties |
|---|---|
| `$geofence_enter` | `geofence_id`, `geofence_name` (strings) |
| `$geofence_exit` | `geofence_id`, `geofence_name` (strings) |

The SDK sends at most one event per geofence and direction every 5 minutes, so a person walking along the edge doesn't send a burst. Crossings happen in the background, so they don't count as app activity (last active, active days or send-time checks). While the SDK is off (`setEnabled(false)` or `wipeData()`), crossings send nothing. At the next `enableGeofences()` or crossing, the SDK also stops monitoring and deletes the geofence data it kept on the device (the cached list and the last crossing times).

## Turn it off for every device

Settings, **SDK settings**, **Geofences**: set it to **Off** and the SDK stops monitoring on every device the next time the app starts, even in apps that call `enableGeofences()`. **Use app's value** and **On** both leave it to the app.

## Limits

- Up to 100 geofences per app, each with a radius of 100 to 10,000 meters and a name of up to 100 characters.
- Android: after the phone restarts, or Location is turned off and back on, geofences resume the next time the app opens. iOS keeps them.
- iOS and Android decide when to report a crossing. It can take a few minutes, and small circles in places with few Wi-Fi networks or cell towers are less reliable. Apple and Google recommend a radius of at least 100 to 150 meters.
- React Native, Flutter and Unity apps can use geofences from the native side of the app; the wrappers don't include them yet.

## Related

- [Data collection controls](/docs/data-and-privacy/data-collection-controls)
- [App store disclosures](/docs/data-and-privacy/app-store-disclosures)
- [Event payload](/docs/reference/event-payload#geofence-events)
- [GET /api/v1/sdk/geofences](/docs/reference/rest-endpoints#get-api-v1-sdk-geofences)
