Giter Club home page Giter Club logo

ios_monitor_location_region_in_background's Introduction

iOS_Monitor_Location_Region_In_Background

iOS: monitor entering/leaving a specified location region, in background, even screen off, app terminated.

##Capabilities

According to iOS API reference of CLLocationManager - Core Location | Apple Developer Documentation, an app can

Monitoring distinct regions of interest and generating location events when the user enters or leaves those regions

The awsome thing is:

For a terminated iOS app, this service relaunches the app to deliver events.

This means if you restart your iOS device, it works without you start the app manually. (iOS will wakeup your app for about 10 seconds when region events occurs)

###Cavets

According to manual of Region Monitoring

Regions are a shared system resource, and the total number of regions available systemwide is limited. For this reason, Core Location limits to 20 the number of regions that may be simultaneously monitored by a single app

And the event may be a little delayed( As my experience, may up to 4 minutes)

region events may not happen immediately after a region boundary is crossed. To prevent spurious notifications, iOS doesn’t deliver region notifications until certain threshold conditions are met. Specifically, the user’s location must cross the region boundary, move away from the boundary by a minimum distance, and remain at that minimum distance for at least 20 seconds before the notifications are reported.

##Detail Steps

  • Requests permission to use location services whenever the app is running

SampleFreeApp/AppDelegate.swift

The requestAlwaysAuthorization() will show a popup to let user choose "Allow" or "Not Allow"

    let locm = CLLocationManager()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))
        locm.delegate = self
        if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways) {
            myInit()
        } else {
            locm.requestAlwaysAuthorization()
        }
        return true
    }
  • start monitoring a specific location region

Just call startMonitoring(...a_region...), Please change the latitude,longitude,radius(in meater).

    var done_myInit = false;
    
    func myInit() {
        if (!done_myInit) {
            locm.startMonitoring(for: CLCircularRegion(
                center: CLLocationCoordinate2D(latitude:12.3456, longitude:78.9012),
                radius: CLLocationDistance(123),
                identifier: "Somewhere1"))
            done_myInit = true
        }
    }
  • Then receive region events

I just push local notifications when entering/leaving regions.

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        let dts = dateFormatter.string(from: Date())
        
        // create a corresponding local notification
        let notification = UILocalNotification()
        notification.alertBody = "Enter " + region.identifier + " @" + dts
        notification.alertAction = "open" // text that is displayed after "slide to..." on the lock screen - defaults to "slide to view"
        notification.fireDate = Date.init()
        notification.soundName = UILocalNotificationDefaultSoundName // play default sound
        
        UIApplication.shared.scheduleLocalNotification(notification)
    }
    
    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        // create a corresponding local notification
    }
    
    func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
        // create a corresponding local notification
    }

##Interesting things

  • The region you registered to monitor will be persisted in the iOS unless you call stopMonitoring(...the_region...).

Even if you update your app to monitor other place (with a different identifier string), the app will still be notified of the old region events!

So be careful, do not forget to call stopMonitoring(...the_region...) when not neccessary.

By the way, i'v found another more complete demo app which integrate Maps app so let you choose where you want to monitor. https://www.raywenderlich.com/136165/core-location-geofencing-tutorial

ios_monitor_location_region_in_background's People

Contributors

jjqq2013 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

obaby sssuourabh

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.