Skip to content

Improve Errata process + fixes for Handling Location Changes in the Background #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

# Xcode user settings
**/xcuserdata/
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ http://shop.oreilly.com/product/0636920034254.do

If you have any questions, you can contact me directly
at vandad.np@gmail.com
Similarly, if you find an error in these sample codes, simply
report them to O'Reilly at the following URL:

If you find an error in these code samples, please fork the Github
repository and submit a pull request with the improvements.

You can also report errors on the O’Reilly website:
http://www.oreilly.com/catalog/errata.csp?isbn=0636920034254
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,47 @@ import CoreLocation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,
// The following is the protocol for location updates
CLLocationManagerDelegate {

var window: UIWindow?
var locationManager: CLLocationManager! = nil
var isExecutingInBackground = false

func locationManager(manager: CLLocationManager!,
didUpdateToLocation newLocation: CLLocation!,
fromLocation oldLocation: CLLocation!){
if isExecutingInBackground{
// LocationManager will call this handler for all location updates
func locationManager(manager: CLLocationManager!,
didUpdateToLocation newLocation: CLLocation!,
fromLocation oldLocation: CLLocation!) {

if (newLocation != nil) {
println("New Location: \(newLocation.coordinate.latitude), \(newLocation.coordinate.longitude)")
}
if isExecutingInBackground {
/* We are in the background. Do not do any heavy processing */
} else {
/* We are in the foreground. Do any processing that you wish */
}
}
}

func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
didFinishLaunchingWithOptions
launchOptions: NSDictionary?) -> Bool {

locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest

// Uses CLLocationManagerDelegate protocol
locationManager.delegate = self

// Asks user for app permission to receive background location changes
locationManager.requestAlwaysAuthorization()

// This can be called immediately but not updates will not
// be received until the user has approved access
locationManager.startUpdatingLocation()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this line itself will ask for permission. I don't think we have to manually ask for permission. Please clarify 👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a change between iOS 7 and iOS 8 that it became required:

http://stackoverflow.com/questions/24062509/ios-8-location-services-not-working

I'd suggest adding a further comment saying that it was necessary as of iOS
8 to make that doubly clear.

On Sun, Oct 19, 2014 at 3:54 AM, Vandad Nahavandipoor <
notifications@github.com> wrote:

In chapter-concurrency/Handling Location Changes in the
Background/Handling Location Changes in the Background/AppDelegate.swift:

 locationManager.delegate = self
  • // Asks user for app permission to receive background location changes
  • locationManager.requestAlwaysAuthorization()
  • // This can be called immediately but not updates will not
  • // be received until the user has approved access
    locationManager.startUpdatingLocation()

I believe this line itself will ask for permission. I don't think we have
to manually ask for permission. Please clarify [image: 👍]


Reply to this email directly or view it on GitHub
https://github.com/vandadnp/iOS-8-Swift-Programming-Cookbook/pull/2/files#r19059447
.

http://www.twitter.com/brunobowden
http://www.facebook.com/brunobowden
http://www.linkedin.com/in/brunobowden

return true
}



func applicationDidEnterBackground(application: UIApplication) {
isExecutingInBackground = true

Expand All @@ -68,11 +82,5 @@ func locationManager(manager: CLLocationManager!,
detection accuracy */
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}






}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>&lt;Message to explain why app needs always on location&gt;</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
Expand Down