title: How can I make iOS background app notify based on location info
body: <p>I am trying to implement a location-baed navigation app on iOS.  I will have a device in my pocket and want it notifies when criteria are met based on location information, such as Google Maps navigation notifies when you make a turn on street.</p>
  
  <p>I assume I can make it with CoreLocation and local notification.</p>
  
  <p>Here are what I have done, expect and actually got.</p>
  
  <ul>
  <li>XCode Version 7.1 (7B91b) on OS X El Capitan 10.11.1</li>
  <li>In Xcode, File -> New -> Project -> iOS Application -> Single View Application</li>
  <li>Add CoreLocation framework to project</li>
  <li>Enable "Location updates" background mode</li>
  <li>Add NSLocationAlwaysUsageDescription key with value of "hello" to info.plist</li>
  <li>Leave ViewController.swift untouched</li>
  <li><p>Edit AppDelegate.swift as follows;</p>
  
  <pre><code>import UIKit
  import CoreLocation
  
  @UIApplicationMain
  class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
      var window: UIWindow?
      lazy var locationManager: CLLocationManager! = {
          let manager = CLLocationManager()
          manager.desiredAccuracy = kCLLocationAccuracyBest
          manager.pausesLocationUpdatesAutomatically = false
          manager.delegate = self
          manager.requestAlwaysAuthorization()
          return manager
      }()
      var lastNotifictionDate = NSDate()
  
      func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
          let settings = UIUserNotificationSettings(forTypes: [.Alert], categories: nil)
          UIApplication.sharedApplication().registerUserNotificationSettings(settings)
          locationManager.startUpdatingLocation()
          return true
      }
  
      func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
          // Do something interesting.
          // I make this function return immediately until 10sec passed after previous notification.
          let now = NSDate()
          if lastNotifictionDate.dateByAddingTimeInterval(10).compare(now) == .OrderedDescending {
              //NSLog("Not yet")
              return
          }
          //NSLog("It's time")
          lastNotifictionDate = now
  
          // Fire local notification, without sound configured for simplicity.
          let notification = UILocalNotification()
          notification.alertBody = "Hello"
          UIApplication.sharedApplication().presentLocalNotificationNow(notification)
      }
  
      // ... other methods are untouched ...
  }
  </code></pre></li>
  </ul>
  
  <p>The app works on simulator as I expect, namely;</p>
  
  <ul>
  <li>Run the app in iPhone 5 Simulator and allow location and notification services</li>
  <li>Press home button (Shift-Command-H).</li>
  <li>In the simulator menu, Debug -> Location -> Free drive</li>
  <li>The app displays a local notification alert every 10sec</li>
  </ul>
  
  <p>The app acts different on my device, iPhone 5 with iOS 9.1;</p>
  
  <ul>
  <li>Run the app via Xcode and allow location and notification services</li>
  <li>Pressed home button</li>
  <li>The device does not display notification alert either on home screen or notification center.</li>
  <li>Then, I tap the app icon on the device to bring the app foreground, opened the notification center and found two notifications.  I expect 5 notifications newly appear if I wait for 50 seconds, but no notification newly appear.</li>
  </ul>
  
  <p>I understand Simulator behaves differently than the actual devices. For example, Simulator shows NSLog() and print() outputs on console during background, while the actual device does not.</p>
  
  <p>I guess I did not configure something for devices or I am trying to do in unfeasible way, but I cannot find what it is.  Could you please tell me what is wrong and how I can fix it?</p>