Friday, February 13, 2015

Location Services Immigration from iOS7 to iOS8

With iOS7, I developed an app tracking all the location with other information like date, time, etc. Yes, right, it's like the other apps like "Life360", which could share the location with other family members. The app is backed with a local server developed with NodeJS and Database by MongoDB. All the techs are pretty freshly popular in current days.

While, since iOS8 is pretty widely adapted by most of the users, I started to immigrate the app from iOS 7 to iOS 8. Here is what I did:

1st, I did nothing, simply run the source code on the iOS runned iPhone 5. The MapView shows exactly as before, which includes all the CLLocation fetched from the server. That means the history could be displayed fully on the Map. BUT, the current location annotation(That blue shining circle) doesn't show up. Also, my UIAlertView shows me the Location Service couldn't give me the location update even it tried to.

2nd, I read the Apple Reference, the most obviously differences I had found are the two newly coming functions which aim to request location service permission on the iDevice. I guess they must have the meaning to be here, I mean they are listed as the two functions in the first section on the page. So, I changed my code from - (void)startUpdatingLocation direct calling to having - (void)requestWhenInUseAuthorization first, then - (void)startUpdatingLocation in the delegate function - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status , when the status is kCLAuthorizationStatusAuthorized or kCLAuthorizationStatusAuthorizedWhenInUse, The result is the request function had been called but the status has been doomed to be kCLAuthorizationStatusNotDetermined .

3rd, After reading a post on StackOverflow, I found my answer. The reason I didn't have the NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist with a message to be displayed in the prompt.

4th, Everything is good, just like the old time.

Summary:
To Immigrate Location Service from iOS 7 to iOS 8, do these:

  1. Call requestWhenInUseAuthorization or requestAlwaysAuthorization first.
  2. Implement Delegate Function - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
  3. When status indicates the Location Service has been granted, Call startUpdatingLocation
  4. Important: Add string message for NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription in your APPNAME-Info.plist under your support file folder.
Good Luck.