지금은 앱의 위치 배경 모드를 구현하려고합니다. 사용자 위치가 업데이트되면 알림을 예약 할 예정입니다. (결국 조건부로 일정을 잡을 예정이지만, 지금은 일정이 잡혀서 작동하는지 알 수 있습니다.) 나는 다음과 같은 코드가 있습니다 :앱이 종료되면 위치 백그라운드 모드가 신속하게 작동합니까?
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
을 복용 시도하고이 문제 가능
배경이 아닌 상태, 즉 앱이 종료되었을 때'startUpdatingLocation'을 어떻게 실행합니까? – LFHS
배경색이 아닌 상태 란 무엇입니까? 그런 상태에 어떻게 가야한다고 생각하니? – Honey
앱이 종료 될 때, 사용자가 앱을 완전히 닫을 때 – LFHS