こんにちは私は、アプリがバックグラウンドにあるときでさえ、その情報をdbにアップロードしたいと思っていて、ユーザーが再びアプリケーションを実行すると、彼が訪れたすべての場所を表示します。だから私は記事https://www.raywenderlich.com/92428/background-modes-ios-swift-tutorialを読んで、作者が言ったすべてのことをやったのですが、バックグラウンドモードの場所の更新を加えた機能で、私はinfo.plistアプリケーションをバックグラウンドで実行しませんでしたが、私のアプリはまだバックグラウンドで実行できません問題?locationManager didUpdateToLocationをバックグラウンドから呼び出す方法は?
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
var i = 0
@IBOutlet var map: MKMapView!
var backendless = Backendless.sharedInstance()
lazy var locationManager: CLLocationManager = {
let locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.distanceFilter = kCLDistanceFilterNone
return locationManager
}()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.startUpdatingLocation()
getLocations()
self.map.showsUserLocation = true
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: -LocationManagerDelegate
//func locationMana
func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
let loc = MKPointAnnotation()
loc.coordinate = newLocation.coordinate
savelocation(loc.coordinate.longitude, latitude: loc.coordinate.latitude)
//loc.coordinate.latitude
//map.showAnnotations(loc, animated: true)
}
func savelocation(longitude: Double, latitude: Double){
print("I work \(i)")
i++
let loc1 = locations()
loc1.latitude = String(latitude)
loc1.longitude = String(longitude)
// print(loc1.latitude)
// print(loc1.longitude)
let datastore = backendless.data.of(locations.ofClass())
var error: Fault?
let result = datastore.save(loc1, fault: &error) as? locations
if error == nil{
print("data was saved")
}else{
print("data was not saved")
}
}
func callAlert(message:String!){
let alertController=UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
let OKButton=UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(OKButton)
self.presentViewController(alertController, animated: true, completion: nil)
}
func getLocations(){
var arr = [MKPointAnnotation]()
//var temp: MKPointAnnotation!
let dataStore = Backendless.sharedInstance().data.of(locations.ofClass())
var error: Fault?
let result = dataStore.findFault(&error)
if error == nil{
let locs = result.getCurrentPage()
// print(cities)
let locs1 = locs as! [locations]
if locs1.count - 1 >= 0{
for index in 0...locs1.count - 1{
let temp = MKPointAnnotation()
temp.coordinate.latitude = Double(locs1[index].latitude!)!
temp.coordinate.longitude = Double(locs1[index].longitude!)!
arr.append(temp)
}
self.map.addAnnotations(arr)
}
}else{
self.callAlert("There is an error retrieving data")
}
}
}
私はappdelegate applicationDidEnterBackgroundメソッドも変更しましたが、手動でそのメソッドを呼び出す方法はわかりません。どんな助け?ありがとう。 :) P.s.私はそれが違法だと知っているアプリはそれ自体を呼び出す必要がありますが、どうやって違うことができるのか分かりません
私はうまく動作する同様のアプリケーションを持っています。あなたのコードはうまく見える - あなたのinfo.plistに "Privacy - Location Always Usage Description"( 'NSLocationAlwaysUsageDescription')がありますか? – Grimxn
はい私はそれを持っています –