{"id":6777,"date":"2020-07-30T08:15:30","date_gmt":"2020-07-30T08:15:30","guid":{"rendered":"https:\/\/www.rlogical.com\/?p=6777"},"modified":"2025-03-31T18:42:55","modified_gmt":"2025-03-31T13:12:55","slug":"how-to-integrate-geofencing-in-an-ios-app","status":"publish","type":"post","link":"https:\/\/www.rlogical.com\/blog\/how-to-integrate-geofencing-in-an-ios-app\/","title":{"rendered":"How to integrate Geofencing in an iOS App?"},"content":{"rendered":"<h2><span style=\"color: #003366\">What is a Geofence?<\/span><\/h2>\n<p align=\"justify\">A Geo-fence is a feature that defines a <strong>virtual boundary around a real world geographical area<\/strong>. Every time the user enters or exits the boundary of a certain area, actions can be triggered in a location enabled device. Usually the user will receive a notification with certain information based on its location in real time.<\/p>\n<p align=\"justify\">The main advantage of this technology is that it creates a fusion between the virtual world and the real one. At <strong>Lateral View we make use of Geofencing in several projects, particularly in the health industry<\/strong>.<\/p>\n<p align=\"justify\"><strong>Geofencing notifies your app<\/strong> when its <strong>device enters or leaves geographical regions you set up.<\/strong> It lets you make cool apps that can trigger a notification whenever you leave home, or greet users with the latest and greatest deals whenever favorite shops are nearby.<\/p>\n<p align=\"justify\">In this geofencing tutorial, you\u2019ll learn how to use region monitoring in<a href=\"https:\/\/www.rlogical.com\/mobile-app-development\/iphone-app-development\/\" target=\"_blank\" rel=\"noopener noreferrer\"> <strong>iOS with Swift<\/strong><\/a> \u2013 using the <strong>Region Monitoring API from Core Location.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #003366\"><b>Applications of Geofencing in Mobile Development<\/b><\/span><\/h2>\n<p><strong>Geofencing can be used several applications such as:<\/strong><\/p>\n<ul style=\"list-style-type: disc\">\n<li style=\"list-style-type: none\"><\/li>\n<li><span style=\"color: #003366\"><b>Marketing<\/b>:<\/span> a clothing store can trigger a push notification with the day\u2019s offers and discounts when a customer is nearby.<\/li>\n<\/ul>\n<ul style=\"list-style-type: disc\">\n<li style=\"list-style-type: none\"><\/li>\n<li><span style=\"color: #003366\"><b>Reminders<\/b>:<\/span> an app that reminds the fans of a football club the address of the gates they should go to enter the stadium while they are in the surroundings.<\/li>\n<\/ul>\n<ul style=\"list-style-type: disc\">\n<li style=\"list-style-type: none\"><\/li>\n<li><b><span style=\"color: #003366\">Arrivals and departures from a certain location<\/span>:<\/b> an airline can send a notification wishing the user a safe trip after the plane has taken off or a welcome message when the plane has landed.<\/li>\n<\/ul>\n<ul style=\"list-style-type: disc\">\n<li style=\"list-style-type: none\"><\/li>\n<li><span style=\"color: #003366\"><b>Child Tracking<\/b><\/span>: an app can send a notification telling a parent that his child has left or entered a certain area.<\/li>\n<\/ul>\n<ul style=\"list-style-type: disc\">\n<li style=\"list-style-type: none\"><\/li>\n<li><b><span style=\"color: #003366\">Security<\/span>:<\/b> the app can enable or disable features when entering or leaving a region.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #003366\"><b>Advantages Of Geofencing App<\/b><\/span><\/h2>\n<ul style=\"list-style-type: disc\">\n<li style=\"list-style-type: none\"><\/li>\n<li>Customer engagement by providing real time offers, discounts and promotions.<\/li>\n<\/ul>\n<ul style=\"list-style-type: disc\">\n<li style=\"list-style-type: none\"><\/li>\n<li>Enhance user experience.<\/li>\n<\/ul>\n<ul style=\"list-style-type: disc\">\n<li style=\"list-style-type: none\"><\/li>\n<li>Send location specific notifications to users.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2><strong><span style=\"color: #003366\">Disadvantages of Geofencing<\/span><\/strong><\/h2>\n<ul style=\"list-style-type: disc\">\n<li style=\"list-style-type: none\"><\/li>\n<li>Geofencing requires the approval of the user. If the user rejects or disables the location services, geofencing won\u2019t work until enabled from the app settings.<\/li>\n<\/ul>\n<ul style=\"list-style-type: disc\">\n<li style=\"list-style-type: none\"><\/li>\n<li>Location tracking takes too much battery life and this is the first cause for users turning off location services in the device.<\/li>\n<\/ul>\n<ul style=\"list-style-type: disc\">\n<li style=\"list-style-type: none\"><\/li>\n<li>Only appropriate for wide range areas.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #003366\"><b>How to integration in iOS app?<\/b><\/span><\/h2>\n<p>&nbsp;<\/p>\n<p><strong>1) In order to work with geofences we need to import CoreLocation framework.<\/strong><\/p>\n<p><strong>2) Please add \u2018Background Modes\u2019 in signing &amp; compatibility and enable location service . Like below image<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6782 size-full\" src=\"https:\/\/www.rlogical.com\/wp-content\/uploads\/2023\/03\/geo.webp\" alt=\"Geofencing\" width=\"1027\" height=\"555\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>3) Add below key in <span style=\"color: #333399\">Info.plist<\/span> files<\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>&lt;key&gt;<\/strong>NSLocationAlwaysAndWhenInUseUsageDescription<strong>&lt;\/key&gt;<\/strong>\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>&lt;string&gt;<\/strong>Allow location access for geofencing goodness<strong>&lt;\/string&gt;<\/strong>\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>&lt;key&gt;<\/strong>NSLocationAlwaysUsageDescription<strong>&lt;\/key&gt;<\/strong>\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>&lt;string&gt;<\/strong>Allow location access for geofencing goodness<strong>&lt;\/string&gt;<\/strong>\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>&lt;key&gt;<\/strong>NSLocationWhenInUseUsageDescription<strong>&lt;\/key&gt;<\/strong>\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>&lt;string&gt;<\/strong>Allow location access for geofencing goodness<strong>&lt;\/string&gt;<\/strong>\r\n\r\n\r\n<\/span><\/pre>\n<p>After that, We define a<strong> locationManager in our Appdelegate<\/strong> and we make the <strong>Appdelegate a CLLocationManagerDelegate:<\/strong><\/p>\n<ul style=\"list-style-type: disc\">\n<li style=\"list-style-type: none\"><\/li>\n<li><span style=\"color: #000000\"><strong>Import Corelocation in AppDelegate<\/strong><\/span><\/li>\n<\/ul>\n<pre><span style=\"color: #333399\">import CoreLocation\r\n<\/span><\/pre>\n<ul style=\"list-style-type: disc\">\n<li style=\"list-style-type: none\"><\/li>\n<li><span style=\"color: #000000\"><strong>Create Global variable for Location and Geofence<\/strong><\/span><\/li>\n<\/ul>\n<pre><span style=\"color: #333399\"><strong>var<\/strong> locationManager : CLLocationManager!\u00a0\u00a0\u00a0\u00a0 \/\/FOR LOCATION<\/span>\r\n<span style=\"color: #333399\"><strong>var<\/strong> geofenceRegion = CLCircularRegion()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/FOR GEOFENCE\r\n\r\n<\/span><\/pre>\n<ul style=\"list-style-type: disc\">\n<li><strong>Implement Location service in \u201cdidFinishLaunchingWithOptions\u201d in AppDelegate<\/strong><\/li>\n<\/ul>\n<pre><strong style=\"color: #333399\">class<\/strong><span style=\"color: #333399\"> AppDelegate: UIResponder, UIApplicationDelegate {<\/span>\r\n<span style=\"color: #333399\">\u00a0 \u00a0 \u00a0 \u00a0 <strong>var<\/strong> locationManager: CLLocationManager?<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>var<\/strong> notificationCenter: UNUserNotificationCenter?<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>var<\/strong> geofenceRegion = CLCircularRegion()\u00a0\u00a0\u00a0 \/\/FOR GEOFENCE<\/span><span style=\"color: #333399\"> \u00a0 <\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>func<\/strong> application(<strong>_<\/strong> application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: <strong>Any<\/strong>]?) -&gt; Bool {<\/span>\r\n\r\n<span style=\"color: #333399\">  \u00a0  <\/span>\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>\/\/ step 1<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>self<\/strong>.locationManager = CLLocationManager()<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>self<\/strong>.locationManager!.delegate = <strong>self<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>self<\/strong>.locationManager.desiredAccuracy = kCLLocationAccuracyBest;<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>self<\/strong>.locationManager.distanceFilter = kCLDistanceFilterNone;<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>self<\/strong>.locationManager.requestAlwaysAuthorization()<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>self<\/strong>.locationManager.requestWhenInUseAuthorization()<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>self<\/strong>.locationManager.startUpdatingLocation()<\/span>\r\n\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong> \/\/ get the singleton object<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>self<\/strong>.notificationCenter = UNUserNotificationCenter.current()<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ register as it's delegate<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 notificationCenter.delegate = <strong>self<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>\/\/ define what do you need permission to use<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>let<\/strong> options: UNAuthorizationOptions = [.alert, .sound]<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong> \/\/ request permission<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 notificationCenter.requestAuthorization(options: options) { (granted, error) <strong>in<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>if<\/strong> !granted {<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(\"Permission not granted\")<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span>\r\n\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>\/\/SET REGION AND SET GEOFENCE<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>self<\/strong>.setRegionListAndGeoFencing()<\/span>\r\n\r\n<span style=\"color: #333399\"> <strong>return<\/strong> <strong>true<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0 }<\/span>\r\n\r\n<strong><span style=\"color: #333399\">Setup Region and setup Geofence<\/span><\/strong>\r\n\r\n<span style=\"color: #333399\">\u00a0<strong>func<\/strong> setRegionListAndGeoFencing()\u00a0 {<\/span>\r\n\r\n<span style=\"color: #333399\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><strong><span style=\"color: #333399\">\/\/Fillup Region List (Add Latitude and Longitude for perticular region), You can set manually or dynamic value for region<\/span>\r\n<\/strong>\r\n<span style=\"color: #333399\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 regionList.add(\"23.0413432,72.5672913\")\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/INCOMETAX, Ahmedabad<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 regionList.add(\"51.5919139,-0.4031427\")\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/Harrow, UK<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 regionList.add(\"3.157917,101.7075007\")\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/Petronas Twin Towers, Malaysia<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 regionList.add(\"-33.8386865,151.0493946\")\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\/\/Sydney Olympic Park, Sydney, Austrlia<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 regionList.add(\"37.7531667,-122.4939421\")\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/Golden Gate Park, San Francisco, CA, USA<\/span>\r\n\r\n<span style=\"color: #333399\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>\/\/SET GEOFENCE FOR ABOVE REGION<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>self<\/strong>.generateGeofenceRegion()<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0 }<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0 <strong>\/\/MARK:- For Geofencing<\/strong><\/span>\r\n\r\n \r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0 <strong>func<\/strong> generateGeofenceRegion()<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0 {<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 geofenceRegion.notifyOnExit = <strong>true<\/strong>;<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 geofenceRegion.notifyOnEntry = <strong>true<\/strong>;<\/span>\r\n\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>for<\/strong> i <strong>in<\/strong> 0..&lt;regionList.count<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>let<\/strong> region = regionList.object(at: i) <strong>as<\/strong>! String<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>let<\/strong> regionArr = region.components(separatedBy: \",\")<\/span>\r\n\r\n<span style=\"color: #333399\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span>\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>let<\/strong> lat = (regionArr[0] <strong>as<\/strong> NSString).doubleValue<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>let<\/strong> long = (regionArr[1] <strong>as<\/strong> NSString).doubleValue<\/span>\r\n\r\n<span style=\"color: #333399\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span>\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>let<\/strong> cellradius = 500<\/span>\r\n\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>let<\/strong> geofenceRegionCenter = CLLocationCoordinate2DMake(lat, long);<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 geofenceRegion = CLCircularRegion(<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 center: geofenceRegionCenter,<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 radius: CLLocationDistance(cellradius),<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 identifier:region<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 );<\/span>\r\n\r\n<span style=\"color: #333399\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span>\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 geofenceRegion.notifyOnExit = <strong>true<\/strong>;<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 geofenceRegion.notifyOnEntry = <strong>true<\/strong>;<\/span>\r\n\r\n<span style=\"color: #333399\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>self<\/strong>.locationManager.startMonitoring(for: geofenceRegion)<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0 }<\/span>\r\n\r\n\r\n<\/pre>\n<h3><span style=\"text-decoration: underline\"><strong><span style=\"color: #333399;text-decoration: underline\">&#8211;\u00a0 Added Location delegate Method<\/span><\/strong><\/span><\/h3>\n<pre><span style=\"color: #333399\">\r\nextension AppDelegate: CLLocationManagerDelegate {<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0 func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if region is CLCircularRegion {<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Do what you want if this information<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.handleEventForExitRegion(forRegion: region)<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0 }<\/span>\r\n\r\n \r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0 <strong>\/\/ called when user Enters a monitored region<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0 func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if region is CLCircularRegion {<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong> \/\/ Do what you want if this information<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.handleEventForEnterRegion(forRegion: region)<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0 }<\/span>\r\n\r\n \r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0 func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(\"didChangeAuthorization\")<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(CLLocationManager.authorizationStatus())<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span>\r\n\r\n<span style=\"color: #333399\"> \u00a0\u00a0 <\/span>\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 let locValue = manager.location!.coordinate<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(\"locations = \\(locValue.latitude) \\(locValue.longitude)\")<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span>\r\n\r\n<span style=\"color: #333399\"> \u00a0\u00a0\u00a0\u00a0 <\/span>\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 func locationManager(_ manager: CLLocationManager, didFinishDeferredUpdatesWithError error: Error?) {<\/span>\r\n\r\n<span style=\"color: #333399\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(\"update failure \\(String(describing: error))\")<\/span>\r\n\r\n<span style=\"color: #333399\"> \u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span>\r\n<span style=\"color: #333399\">}<\/span>\r\n\r\n<\/pre>\n<h3><span style=\"text-decoration: underline\"><strong><span style=\"color: #333399;text-decoration: underline\">&#8211;\u00a0 Schedule Local Notification<\/span><\/strong><\/span><\/h3>\n<pre><span style=\"color: #333399\"><strong>\r\nfunc<\/strong> handleEventForEnterRegion(forRegion region: CLRegion!) {<\/span>\r\n\r\n<span style=\"color: #333399\">\/\/ customize your notification content<\/span>\r\n\r\n<span style=\"color: #333399\"><strong>let<\/strong> content = UNMutableNotificationContent()<\/span>\r\n\r\n<span style=\"color: #333399\">content.title = \"Awesome title\"<\/span>\r\n\r\n<span style=\"color: #333399\">content.body = \"Well-crafted body message\"<\/span>\r\n\r\n<span style=\"color: #333399\">content.sound = UNNotificationSound.default()<\/span>\r\n\r\n<strong><span style=\"color: #333399\">\/\/ when the notification will be triggered<\/span><\/strong>\r\n\r\n<span style=\"color: #333399\"><strong>var<\/strong> timeInSeconds: TimeInterval = (60 * 15) \/\/ 60s * 15 = 15min<\/span>\r\n\r\n<strong><span style=\"color: #333399\">\/\/ the actual trigger object<\/span><\/strong>\r\n\r\n<span style=\"color: #333399\"><strong>let<\/strong> trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInSeconds,<\/span>\r\n\r\n<span style=\"color: #333399\">repeats: <strong>false<\/strong>)<\/span>\r\n<span style=\"color: #333399\">\r\n<strong>\/\/ notification unique identifier, for this example, same as the region to avoid duplicate notifications<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\"><strong>let<\/strong> identifier = region.identifier<\/span>\r\n\r\n<strong><span style=\"color: #333399\">\/\/ the notification request object<\/span><\/strong>\r\n\r\n<span style=\"color: #333399\"><strong>let<\/strong> request = UNNotificationRequest(identifier: identifier,<\/span>\r\n\r\n<span style=\"color: #333399\">content: content,<\/span>\r\n\r\n<span style=\"color: #333399\">trigger: trigger)<\/span>\r\n\r\n<strong><span style=\"color: #333399\">\/\/ trying to add the notification request to notification center<\/span><\/strong>\r\n\r\n<span style=\"color: #333399\">notificationCenter.add(request, withCompletionHandler: { (error) <strong>in<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\"><strong>if<\/strong> error != <strong>nil<\/strong> {<\/span>\r\n\r\n<span style=\"color: #333399\">print(\"Error adding notification with identifier: \\(identifier)\")<\/span>\r\n\r\n<span style=\"color: #333399\">}<\/span>\r\n\r\n<span style=\"color: #333399\">})<\/span>\r\n\r\n<span style=\"color: #333399\">}<\/span>\r\n<span style=\"color: #333399\"><strong>func handleEventForExitRegion(forRegion region: CLRegion!) {<\/strong><\/span>\r\n\r\n<span style=\"color: #333399\">}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #003366\"><strong>How Rlogical Techsoft helps for develops Geofencing App?<\/strong><\/span><\/h2>\n<p>Rlogical Techsoft is a leading <strong>IT company<\/strong> known for <a href=\"https:\/\/www.rlogical.com\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>web &amp; mobile app development in India.<\/strong><\/a> We have expertise in providing<strong> app solutions based on Geolocation<\/strong> leveraging <strong>Geolocation based mobile app development and solutions.<\/strong><\/p>\n<h3>Contact us with your requirements for development of <a href=\"https:\/\/www.rlogical.com\/mobile-app-development\/iphone-app-development\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>Geofencing apps in iOS<\/strong><\/a>.<\/h3>\n<h4><\/h4>\n<h4><\/h4>\n<p>&nbsp;<\/p>\n<h4>References:<\/h4>\n<p><a href=\"https:\/\/medium.com\/academyufpe\/geofencing-in-ios-swift-for-noobs-29a1c6d15dcc\">https:\/\/medium.com\/academyufpe\/geofencing-in-ios-swift-for-noobs-29a1c6d15dcc<\/a><\/p>\n<p><a href=\"https:\/\/www.raywenderlich.com\/5470-geofencing-with-core-location-getting-started\">https:\/\/www.raywenderlich.com\/5470-geofencing-with-core-location-getting-started<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is a Geofence? A Geo-fence is a feature that defines a virtual boundary around a real world geographical area. Every time the user enters or exits the boundary of a certain area, actions can be triggered in a location enabled device. Usually the user will receive a notification with certain information based on its [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":13754,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[39],"tags":[230,229,225,226,227,224,228],"class_list":["post-6777","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios-app-development","tag-geo-loaction","tag-geofence","tag-geofencing-app","tag-geofencing-app-in-ios","tag-geofencing-ios-app-development","tag-ios-app-development","tag-swift"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/posts\/6777","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/comments?post=6777"}],"version-history":[{"count":1,"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/posts\/6777\/revisions"}],"predecessor-version":[{"id":13757,"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/posts\/6777\/revisions\/13757"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/media\/13754"}],"wp:attachment":[{"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/media?parent=6777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/categories?post=6777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rlogical.com\/wp-json\/wp\/v2\/tags?post=6777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}