extension TemplateBackgroundLocationUpdate {
public init(){
self.init()
locationManager=CLLocationManager()
#if hd || express
NotificationCenter.default.addObserver(self, selector: #selector(TemplateBackgroundLocationUpdate.appWillResignActive(_:)), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(TemplateBackgroundLocationUpdate.appWillEnterForeground(_:)), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
#endif
self.locationManager.delegate = self;
self.locationManager.startUpdatingLocation()
locationManager.pausesLocationUpdatesAutomatically = true
let configuration=URLSessionConfiguration.background(withIdentifier: String.randomString())
configuration.sessionSendsLaunchEvents=true
standardSession = Foundation.URLSession.shared
previousTime = Date()
oldLocation=CLLocation()
date=Date()
newDate=Date()
}
func application(
_ application: UIApplication,
handleEventsForBackgroundURLSession identifier: String,
completionHandler: @escaping () -> Void) {
Task.init{
await GeoreferenceQueue.shared.setBackgroundCompletionHandler(completionHandler)
}
}
func loggedConnect(to urlRequest: URLRequest, interval timeout: TimeInterval, withCompletionBlock: @escaping SpeedTestCompletionHandler){
bytesExchanged = 0
speedTestCompletionBlock = withCompletionBlock
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForResource = timeout
session = URLSession.init(configuration: configuration, delegate: self, delegateQueue: nil)
session!.dataTask(with: urlRequest).resume()
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void){
completionHandler(.allow)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64){
speedTestCompletionBlock?(Float(totalBytesSent), Float(totalBytesExpectedToSend), nil, false)
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
if let JSONString = String(data: data, encoding: String.Encoding.utf8)
{
print(JSONString)
}
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
print("did complete with error \(String(describing: error?.localizedDescription))")
speedTestCompletionBlock?(0, 0, nil, true)
}
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession){
Task.init {
await GeoreferenceQueue.shared.executeCompletionHandler()
}
}
func backgroundCompleted(data: Data?, error: Error?=nil){
print("top")
}
Topic:
Developer Tools & Services
SubTopic:
Xcode
Tags: