iOS SDK

Setup the Woosmap Geofencing SDK using iOS SDK

  1. Install SDK
  2. Dependencies
  3. Initialize SDK

The Woosmap Geofencing SDK allows you to monitor Geofences, track your user’s location and connect with the Woosmap Search and Distance APIs.

Learn how to integrate the iOS SDK below. You can also explore the source code on GitHub to get deeper into it.

Install SDK

You can install the SDK in four different ways.

We recommend installing the SDK via Swift Package Manager.

Swift Package Manager

To integrate Woosmap Geofencing SDK into your project using Swift Package Manager, you can add the library as a dependency in Xcode (11 and above) – see adding package dependencies to your app on Apple documentation. The package repository URL is:

https://github.com/woosmap/woosmap-geofencing-ios-sdk.git

Carthage

Install Carthage. To Woosmap Geofencing SDK as a github origin, add the following to your Cartfile:

github "woosmap/woosmap-geofencing-ios-sdk" ~> 1.3.1

Then run carthage update and use the framework in Carthage/Build/<platform>.

CocoaPods

Install CocoaPods, a dependency manager for Cocoa projects. If you don’t have an existing Podfile, run pod init in your project directory. Add the following to your Podfile: For usage and installation instructions, visit their website. To integrate Woosmap Geofencing SDK into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'WoosmapGeofencing', :git => 'https://github.com/woosmap/woosmap-geofencing-ios-sdk.git'

Add manually

You can also add the SDK to your project manually.

Dependencies

The SDK depends on two external libraries:

It also requires the Apple’s CoreLocation framework. In your target settings, go to General -> Frameworks, Libraries, and Embedded Content and add CoreLocation if you haven’t already.

The SDK currently supports iOS 10 and higher.

Initialize SDK

To retrieve your user’s location data when your app starts, follow these steps:

First, set delegate of protocol Location in the AppDelegate method.

import UIKit
import CoreLocation
import WoosmapGeofencing

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate  {

    var window: UIWindow?
    let dataLocation = DataLocation()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        WoosmapGeofencing.shared.getLocationService().locationServiceDelegate = dataLocation
        if (CLLocationManager.authorizationStatus() != .notDetermined) {
            WoosmapGeofencing.shared.startMonitoringInBackground()
        }
        return true
    }
}

In order to avoid loosing data, you also need to call startMonitoringInBackground in the proper AppDelegate method:

func applicationDidEnterBackground(_ application: UIApplication) {
    if (CLLocationManager.authorizationStatus() != .notDetermined) {
        WoosmapGeofencing.shared.startMonitoringInBackground()
    }
}

To keep the SDK up to date with user’s location data, you need to call didBecomeActive in the proper AppDelegate method too.

func applicationDidBecomeActive(_ application: UIApplication) {
    WoosmapGeofencing.shared.didBecomeActive()
    // Restart any tasks that were paused (or not yet started) while the application was inactive. 
    // If the application was previously in the background, optionally refresh the user interface.
}

The SDK tracks position by default. To disable location collection, just change the value in the settings of the SDK as follows:

WoosmapGeofencing.shared.setTrackingEnable(enable: false)

Finally, to effectively retrieve the location data, add this in your class delegate:

public class DataLocation:LocationServiceDelegate  {
    public init() {}
    public func tracingLocation(location: Location) {
        NSLog("location: \(location)")
    }
    public func tracingLocationDidFailWithError(error: Error) {
        NSLog("\(error)")
    }
    public func readLocations()-> [Location] {
        return Locations.getAll()
    }
    public func eraseLocations() {
        Locations.deleteAll()
    }
}
Was this article helpful?
Have more questions? Submit a request