swift3.0 后臺(tái)定位功能

公司需求后臺(tái)定位,間隔五分鐘后臺(tái)上傳坐標(biāo)信息,這里主要把后臺(tái)定位功能整理了一下。
plist 的權(quán)限設(shè)置 這里就不具體說了。

import UIKit
import CoreLocation
class LocationUpdateManager: NSObject, CLLocationManagerDelegate{
    
    var standardlocationManager:CLLocationManager?
    
    var lastTimestamp:NSDate?
    
    var timer: Timer?
    
    static let sharedStandardInstance = LocationUpdateManager()
    
    private override init() {
        super.init()
        self.standardlocationManager = CLLocationManager()
        self.standardlocationManager?.delegate = self
        self.standardlocationManager?.desiredAccuracy = kCLLocationAccuracyBest
        self.standardlocationManager?.distanceFilter = 100
        self.standardlocationManager?.pausesLocationUpdatesAutomatically = false
        
        if #available(iOS 8.0, *) {
            self.standardlocationManager?.requestAlwaysAuthorization()
        }
        
        if #available(iOS 9.0, *) {
            self.standardlocationManager?.allowsBackgroundLocationUpdates = true
        }
        
    }
    
    func startStandardUpdatingLocation()  {
        self.standardlocationManager?.startUpdatingLocation()
    }
    
    func stopStandardUpdatingLocation(){
        self.standardlocationManager?.stopUpdatingLocation()
    }
    
    //定位代理函數(shù)
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let mostRecentLocation = locations.last
        if timer == nil {
            timer = Timer(fireAt: Date(), interval: 10, target: self, selector: #selector(self.printCurrentTime), userInfo: mostRecentLocation, repeats: true)
            RunLoop.current.add(timer!, forMode: .defaultRunLoopMode)
        }
    }
    func printCurrentTime() {
        
        let mostRecentLocation = timer?.userInfo as! CLLocation
        
        print("經(jīng)度\(mostRecentLocation.coordinate.latitude)")
        print("緯度\(mostRecentLocation.coordinate.longitude)")
        
    }

}

最后就是關(guān)于后臺(tái)定位審核問題,如果只是把定位數(shù)據(jù)傳給后臺(tái)是會(huì)被蘋果拒絕的,需要把定位得坐標(biāo)展現(xiàn)在地圖上,或者用個(gè)tableview把定位得信息列出來。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容