I have got this error message:
2017-01-02 15:29:07.886534 TimeStamp[665:191103] [LogMessageLogging] 6.1 Unable to retrieve CarrierName. CTError: domain-2, code-5, errStr:((os/kern) failure)
I suppose wanting to make an app update my location to server every one minute.
i also set these two key on plist:
Privacy - Location Always Usage Description
Privacy - Location When In Use Usage Description
and turned on "background mode", "location updates" in "capibilities"
i googled and found that a lot of people've encountered this problem after updating Xcode to 8.1. and this problem only appear when using MapKit. i want to know how to fix it? i am a newbie. i hope someone can help me fix this. thank you so much.
import UIKit
import MapKit
import APScheduledLocationManager
import CoreLocation
class MapViewController: UIViewController, MKMapViewDelegate, APScheduledLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var manager: APScheduledLocationManager!
let radius = 1000.0
let locations = [
["title": "New York, NY", "latitude": 40.713054, "longitude": -74.007228, "image": UIImage(named: "tram")?.resizeImage(newWidth: 40) as Any],
["title": "Los Angeles, CA", "latitude": 34.052238, "longitude": -118.243344, "image": UIImage(named: "dark")?.resizeImage(newWidth: 40) as Any],
["title": "Chicago, IL", "latitude": 41.883229, "longitude": -87.632398, "image": UIImage(named: "star")?.resizeImage(newWidth: 40) as Any],
["title": "Home", "latitude": 22.2831319172855, "longitude": 114.126703455693, "image": UIImage(named: "work")?.resizeImage(newWidth: 40) as Any],
["title": "Max", "latitude": 22.282369430502, "longitude": 114.127022599629, "image": UIImage(named: "home")?.resizeImage(newWidth: 40) as Any],
["title": "Ann's home", "latitude": 22.3597674705159, "longitude": 114.127513034534, "image": UIImage(named: "logo")?.resizeImage(newWidth: 40) as Any]
]
override func viewDidLoad() {
super.viewDidLoad()
manager = APScheduledLocationManager(delegate: self)
mapView.delegate = self
mapView.showsUserLocation = true
switch CLLocationManager.authorizationStatus() {
case .authorizedAlways:
manager.startUpdatingLocation(interval: 60, acceptableLocationAccuracy: 100)
default:
manager.requestAlwaysAuthorization()
}
}
@IBAction func changeMapType(_ sender: Any) {
if mapView.mapType == MKMapType.standard {
mapView.mapType = MKMapType.satellite
} else if mapView.mapType == MKMapType.satellite {
mapView.mapType = MKMapType.hybrid
} else if mapView.mapType == MKMapType.hybrid {
mapView.mapType = MKMapType.standard
}
}
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
let photoAnnotations = locations.map {
PhotoAnnotation(title: $0["title"] as? String, image: $0["image"] as? UIImage, coordinate: CLLocationCoordinate2D(latitude: $0["latitude"] as! CLLocationDegrees, longitude: $0["longitude"] as! CLLocationDegrees))
}
mapView.addAnnotations(photoAnnotations)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? PhotoAnnotation {
let identifier = "pin"
let view: PhotoAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? PhotoAnnotationView {
dequeuedView.annotation = annotation
dequeuedView.image = annotation.image
view = dequeuedView
} else {
view = PhotoAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.canShowCallout = false
view.image = annotation.image
}
return view
}
return nil
}
func scheduledLocationManager(_ manager: APScheduledLocationManager, didChangeAuthorization status: CLAuthorizationStatus){
switch status {
case .notDetermined:
print("NotDetermined")
case .restricted:
print("Restricted")
case .denied:
print("Denied")
case .authorizedAlways:
print("AuthorizedAlways")
manager.startUpdatingLocation(interval: 60, acceptableLocationAccuracy: 100)
case .authorizedWhenInUse:
print("AuthorizedWhenInUse")
}
}
func scheduledLocationManager(_ manager: APScheduledLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last!
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, radius, radius)
mapView.setRegion(coordinateRegion, animated: true)
print(locations.last!.coordinate.latitude)
print(locations.last!.coordinate.longitude)
}
func scheduledLocationManager(_ manager: APScheduledLocationManager, didFailWithError error: Error) {
print("Failed to initialize GPS: ", error)
}
}