Created
July 13, 2016 06:12
-
-
Save JonMercer/75b3f45cda16ee5d1e0955b2492f66dd to your computer and use it in GitHub Desktop.
I took apart apple's example to only show relevant information on how to get the device's current GPS coordinates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (C) 2016 Apple Inc. All Rights Reserved. | |
See LICENSE.txt for this sample’s licensing information | |
Abstract: | |
Contains the PotlocViewController which shows how to use CLLocationManager and WCSession together to communicate between an iPhone and an Apple Watch. | |
*/ | |
import UIKit | |
import CoreLocation | |
class PotlocViewController: UIViewController, CLLocationManagerDelegate { | |
/// Location manager used to start and stop updating location. | |
let manager = CLLocationManager() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
manager.delegate = self | |
manager.requestWhenInUseAuthorization() | |
//start updating | |
manager.allowsBackgroundLocationUpdates = true | |
manager.startUpdatingLocation() | |
//stop updating | |
manager.stopUpdatingLocation() | |
manager.allowsBackgroundLocationUpdates = false | |
} | |
/** | |
Increases that location count by the number of locations received by the | |
manager. Updates the batch count with the added locations. | |
*/ | |
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | |
receivedLocationCount = receivedLocationCount + locations.count | |
locationBatchCount = locationBatchCount + locations.count | |
locationBatchSizeLabel.text = String(locationBatchCount) | |
receivedLocationCountLabel.text = String(receivedLocationCount) | |
} | |
/// Log any errors to the console. | |
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { | |
print("Error occured: \(error.localizedDescription).") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment