2017-11-14 16 views
0

지금은 앱의 위치 배경 모드를 구현하려고합니다. 사용자 위치가 업데이트되면 알림을 예약 할 예정입니다. (결국 조건부로 일정을 잡을 예정이지만, 지금은 일정이 잡혀서 작동하는지 알 수 있습니다.) 나는 다음과 같은 코드가 있습니다 :앱이 종료되면 위치 백그라운드 모드가 신속하게 작동합니까?

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    if let location = locations.first { 
     currentLocation = location 
    } 
    if UIApplication.shared.applicationState == .background { 
    // if the app is in the background 
     let notif = UNMutableNotificationContent() 
    notif.title = "Location was updated" 
    var message = " " 
    notif.body = message 
    let identifier = String(arc4random()) 
    var dc = DateComponents() 
    var date = Date() 
    date = Calendar.current.date(byAdding: .second, value: 3, to: date)! 
    dc.hour = Calendar.current.component(.hour, from: date) 
    dc.minute = Calendar.current.component(.minute, from: date) 
    dc.second = Calendar.current.component(.second, from: date) 
    let notifTrigger = UNCalendarNotificationTrigger(dateMatching: dc, repeats: false) 
    let request = UNNotificationRequest(identifier: identifier, content: notif, trigger: notifTrigger) 
    UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in 
     if error != nil { 
      print(error!) 
     } else { 
      print("Notification scheduled!") 
     } 
    }) 

앱이 배경이지만, 응용 프로그램이 종료됩니다 때, 내 함수가 호출되지 않으며, 알림이 전달되지 않아요 경우는 잘 작동합니다. 제 질문은 스위프트가 가능합니까? 또는 앱이 배경으로 표시되지만 종료되지 않은 경우에만 사용하는 locationManager 기능이 작동합니까?

사이드 참고 : 내가 if UIApplication.shared.applicationState == .background을 복용 시도하고이 문제 가능

답변

1

앱을 종료하면 배경이 아닌 상태에서 startUpdatingLocation을 실행할 때까지 정상 * 위치 서비스가 중지됩니다.


* 위치 서비스, 지역 모니터링, 중요한 위치 변경이 하지 정상 위치 추적하며 OS 수준하지 응용 프로그램 수준에서 관리를 참조하십시오.

비정상적인 위치 추적은 앱 종료 후 중단되지 않습니다.

+0

배경이 아닌 상태, 즉 앱이 종료되었을 때'startUpdatingLocation'을 어떻게 실행합니까? – LFHS

+0

배경색이 아닌 상태 란 무엇입니까? 그런 상태에 어떻게 가야한다고 생각하니? – Honey

+0

앱이 종료 될 때, 사용자가 앱을 완전히 닫을 때 – LFHS

0

가 해결되지 않습니다. 위치 업데이트가 실행되도록 앱을 이동합니다. 정확도 100m로 확인하고 장치를 옮기거나 위치에 대한 시뮬레이터 시뮬레이션을 사용하십시오. 이미 사용하고 있다고 가정합니다 사용 위치 항상 사용 허가

+0

예, 있습니다. 나는 시뮬레이터에 "고속도로 드라이브"를 실행 시켰는데, 이는 앱이 배경으로 설정되어 있지만 종료되지 않았을 때 작동하지만, 종료되면 알림이 전달되는 것을 멈 춥니 다. – LFHS

+0

시뮬레이터의 경우 디버거가 연결 해제됩니다. 이 장치를 사용해 보셨습니까? – ahmed

+0

디버거가 종료되므로 고속도로 드라이브도 종료됩니다. 동일한 시뮬레이터에서 다른 응용 프로그램을 실행하고 해당 응용 프로그램에서 위치를 시뮬레이션하십시오. – ahmed