public func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL){
do {
//print("location=\(location)")
let data = try Data(contentsOf: location, options: .uncachedRead)
// Hold this file as an NSData and write it to the new location
//submitCompletion?(data, nil)
backgroundCompleted(data: data)
} catch {
backgroundCompleted(data: nil, error: error)
print("could not get data for error \(error)")
}
}
public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
Task.init{
let backgroundLocalizationActive=await GeoreferenceQueue.shared.getBackgroundLocalizationActive()
await MainActor.run {
if CLLocationManager.locationServicesEnabled() {
if manager.authorizationStatus == .authorizedAlways && backgroundLocalizationActive{
if #available(iOS 17.0, *) {
//let backgroundSession=CLBackgroundActivitySession()
} else {
// Fallback on earlier versions
}
self.locationManager.requestAlwaysAuthorization()
self.locationManager.showsBackgroundLocationIndicator=true
//let session=CLServiceSession(autorization: CLServiceSession.AuthorizationRequirement.always
//
} else if (manager.authorizationStatus == .authorizedWhenInUse){
self.locationManager.allowsBackgroundLocationUpdates = false
self.locationManager.desiredAccuracy = 5;
//self.locationManager!.desiredAccuracy=kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.headingFilter = kCLHeadingFilterNone
self.locationManager.pausesLocationUpdatesAutomatically=true;
}
}
}
print("start localizing")
}
}
func locationManager( _ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
let heading = newHeading.trueHeading > 0 ? newHeading.trueHeading : newHeading.magneticHeading
locateUserAtHeading(heading: heading)
}
func handleLocationUpdate(_ newLocation:CLLocation, moved: Bool) async throws { //virtual
}
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
newDate=Date()
//print("didUpdateLocation")
var moved=false
if let lastLocation=locations.first{
Task.init{
await SettingsActor.shared.setPresentLocation(lastLocation) //valore usato da discendenti
}
let timePast=newDate.timeIntervalSince(date)
if timePast>30 {
moved = lastLocation.distance(from: oldLocation)>50
date=newDate
oldLocation=lastLocation
}
Task.init{
try? await self.handleLocationUpdate(lastLocation, moved: moved)
}
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
Task.init{
await MainActor.run{
let title=NSLocalizedString("Your location", comment:"")
let message=String(format:NSLocalizedString( "I am not able to establish your location: please move somewhat possibly in an uncovered area", comment:""), CLLocation().altitude)
let dismiss=NSLocalizedString("Dismiss", comment:"")
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: dismiss, style: .cancel) { (action) in
}
alertController.addAction(cancelAction)
}
}
}
#if !limo
func triggerActiveLocationUpdate(_ entering:Bool){ //virtual
}
func appWillResignActive(_ notification: Notification){
triggerActiveLocationUpdate(false)
}
func appWillEnterForeground(_ notification: Notification){
triggerActiveLocationUpdate(true)
}
#endif
func debugNotification(_ message:String){
#if !NDDEBUG
//localNotification(message)
#endif
}
/*public func localNotification(_ message:String){ //for Objective -C
self.localNotification(message, sound:nil, badge:nil)
}*/
func stringUUID()->String{
let strUUID = UUID().uuidString
return strUUID
}
func localNotification(_ title:String, message:String, sound:UNNotificationSound?, badge:Int?, interval:TimeInterval, userInfo: [AnyHashable : Any]?, category:UNNotificationCategory?=nil){
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = title
content.body = message
content.categoryIdentifier = "alarm"
if category != nil{
UNUserNotificationCenter.current().setNotificationCategories([category!])
}
if let isUserInfo=userInfo{
content.userInfo = isUserInfo
}
content.sound = sound
//var dateComponents = DateComponents()
//dateComponents.hour = 10
//dateComponents.minute = 30
//let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
}
}