2017-11-11 1 views
0

위치 기반 탐색 앱이 거의 완성되었습니다. 알림을 받으면 소리를 내며, Google지도로 이동할 때 백그라운드에서 계속 열려 있어야합니다. 장거리 나 장기간 여행 한 후 앱을 다시 방문하여 중단 한 부분을 다시 찾을 수 있습니다. 그러나 이것은 Location UsageLocation Always Usage을 정의하고 요청 했음에도 불구하고 작동하지 않습니다. 백그라운드로 들어가면 Xcode 디버거에서 서버의 지정된 JSON이 수신되었음을 알 수 있지만 사운드는 재생되지 않습니다. 또한, 사용자가 길게 간다면, 좌표가 콘솔에서 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])에서 인쇄되는 동안에도, 앱이 종료되고 사용자가 초기보기 컨트롤러로 다시 걷어차집니다. 어떻게 그걸 막을 수 있니? 다른 앱이 백그라운드에서 열린 채로있는 것을 확인한 후 iOS에서 파란색 상태 표시 줄이 표시되면 "[이 앱은 사용자의 위치를 ​​사용 중입니다." 내 앱을 180 초 이상 열어 둘 수없는 경우 사용자가 그/그녀가 중단 한 곳에서 다시 시작할 수 있도록 현재 데이터를 어떻게 저장할 수 있습니까?백그라운드에서 위치를 업데이트해도 백그라운드에서 앱이 계속 실행되지 않습니다.

var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid 
var locationManager = CLLocationManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view. 

    navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil) 
    mapView.showsUserLocation = true 
    mapView.delegate = self 

    if let userLocation = mapView.userLocation.location { // because sometimes there is no coordinate and it will unwrap nil. 
     mapView.centerCoordinate = userLocation.coordinate 
     let region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 9484.1, 9484.1) 
     mapView.setRegion(region, animated: true) 
    } 

    UIApplication.shared.isIdleTimerDisabled = true 

    locationManager = CLLocationManager() 
    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.requestAlwaysAuthorization() 

} 

func mapView(_ mapView: MKMapView, didUpdate: MKUserLocation) { 
    let userLocation = mapView.userLocation 
    mapView.centerCoordinate = userLocation.location!.coordinate 
    //let region = MKCoordinateRegionMakeWithDistance(frontedUserLocation, 1855, 1855) 
    let region = MKCoordinateRegionMakeWithDistance(mapView.centerCoordinate, 9484.1, 9484.1) 
    mapView.setRegion(region, animated: true) 
} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    guard let mostRecentLocation = locations.last else { 
     return 
    } 

    // Add another annotation to the map. 
    let annotation = MKPointAnnotation() 
    annotation.coordinate = mostRecentLocation.coordinate 

    // Also add to our map so we can remove old values later 

    // Remove values if the array is too big 

    if UIApplication.shared.applicationState == .active { 

    } else { 
     print("App is backgrounded. New location is %@", mostRecentLocation) 
     print("Background time remaining = \(UIApplication.shared.backgroundTimeRemaining) seconds") 
     if (UIApplication.shared.backgroundTimeRemaining < 175.2) { 
      print("Time low. Re-registering...") 
      locationManager.stopUpdatingLocation() 
      locationManager.startUpdatingLocation() 
      self.registerBackgroundTask() 
     } 
    } 
} 

/*func operate() { 
    //call endBackgroundTask() on completion.. 
    switch UIApplication.shared.applicationState { 
    case .active: 
     print("App is active.") 
    case .background: 
     print("App is in background.") 
     print("Background time remaining = \(UIApplication.shared.backgroundTimeRemaining) seconds") 
     if (UIApplication.shared.backgroundTimeRemaining < 175.2) { 
      print("Time low. Re-registering...") 
      self.registerBackgroundTask() 
     } 
    case .inactive: 
     endBackgroundTask() 
     self.registerBackgroundTask() 
     break 
    } 
}*/ 

func updateCoords() { 
    let updateCoordinates = CloudOperation() // edit this to accept a different parameter 
    let userLocation = self.mapView.userLocation.location!.coordinate 
    updateCoordinates.latit = userLocation.latitude 
    updateCoordinates.longi = userLocation.longitude 
    locationManager.allowsBackgroundLocationUpdates = true 
    locationManager.pausesLocationUpdatesAutomatically = false 
    locationManager.startUpdatingLocation() 

    /*backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in 
     self?.endBackgroundTask() 
    } 
    assert(backgroundTask != UIBackgroundTaskInvalid)*/ 

    //self.registerBackgroundTask() 
    print("Trying to update...") 

    dump(self.phase) 
} 

고마워요!

Capabilities Capabilities 2

+0

을 당신은 당신이 당신의 위치 관리자 이것은 아이폰 7에서 작동 – Paulw11

답변

1

당신의 CLLocationManager에 당신이 활성화 한 배경 모드를 확인하십시오 :

locationManager.allowsBackgroundLocationUpdates = true 
+0

설정 코드를 표시해야합니다, iPhone 6의 iOS 10은 아닙니다. –

관련 문제