我目前正在开发一个iOS应用程序,该应用程序在后台获取该位置。我的模拟器的iOS版本是11.1。我不知道其他人是否面临这个问题?
我在第一个ViewController上初始化我的CLLocationManager并在初始化后终止应用程序。
如果我按下主页按钮并转到主屏幕(这使我的应用程序在后台工作,该位置已正确发送到我的节点服务器,但每当我从多任务处理屏幕手动终止应用程序。位置提取甚至停止我从模拟器的设置中将位置模式设置为高速公路驱动。)
我还添加了plist隐私说明。以下 图片。
这是我的代码,
//
// ViewController.swift
// BackgroundDeneme
//
// Created by Alican Yilmaz on 01/11/2017.
// Copyright © 2017 Alican Yilmaz. All rights reserved.
//
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
var manager:CLLocationManager!
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.manager = CLLocationManager()
self.manager.delegate = self
manager.requestAlwaysAuthorization()
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.pausesLocationUpdatesAutomatically = false
manager.allowsBackgroundLocationUpdates = true
self.mapView.showsUserLocation = true
manager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.first
let span = MKCoordinateSpan(latitudeDelta: 0.01,longitudeDelta: 0.01)
let myLocation = CLLocationCoordinate2DMake((location?.coordinate.latitude)!, (location?.coordinate.longitude)!)
let region = MKCoordinateRegion(center: myLocation, span: span)
mapView.setRegion(region, animated: true)
let longitude = String(describing: location!.coordinate.longitude)
let latitude = String(describing: location!.coordinate.latitude)
let speed = String(describing: location!.speed)
let accuracy = String(describing: " buviewdengelme \(String(describing: location!.verticalAccuracy)) \(location!.horizontalAccuracy)")
UpdateLocation.entryLocation(longitude: longitude,latitude: latitude,speed: speed,accuracy: accuracy)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("location Almada problem var: \(error)" )
}
}
class UpdateLocation{
static func entryLocation(longitude:String, latitude:String, speed:String, accuracy:String){
let todoEndpoint: String = "http://172.27.108.114:4000/location"
guard let url = NSURL(string: todoEndpoint) else {
return
}
var urlRequest = URLRequest(url: url as URL)
urlRequest.httpMethod = "POST"
let postString = "longitude=\(longitude)&latitude=\(latitude)&speed=\(speed)&accuracy=\(accuracy)"
urlRequest.httpBody = postString.data(using: .utf8)
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: urlRequest, completionHandler: { (data, response, error) in
if (error != nil) {
print("ERROR VAR: \(error)")
}
})
task.resume()
}
}
提前致谢,