简体   繁体   中英

Cannot get GPS in background of iOS (background mode plugin enabled)

Hi guys can you save me from the project's cliff.

Background:

I have a Ionic project based on Map template. What it does is tracking the walking route of user and draw it to google map.

Problem appears on iOS devices with background running. I cannot get the GPS data correctly. FYI: BackgroundMode plugin installed and set up.

Scenario 1: Use cordova-plugin-geolocation plugin

It does not work. No GPS data collected when turning off screen.

Scenario 2: Use plain JS with navigator.geolocation.getCurrentPosition

It works. GPS data collected but not correct . Data is somehow duplicated (!!) from previous ones. Thing get back to normal when I open the app again.

Any idea would help.


Plugins used:
cordova-plugin-background-mode 0.6.5
cordova-plugin-customurlscheme 4.1.5
cordova-plugin-device 1.1.1
cordova-plugin-geolocation 2.1.0
cordova-plugin-splashscreen 3.2.1
cordova-plugin-statusbar 2.1.2
cordova-plugin-whitelist 1.2.1
ionic-plugin-keyboard 2.0.1


Edited:
Target platform?
iOS 7+, and Android 4.1+ as required from Ionic.

Test device iOS version?
iOS 9.2 (13C75) on iPhone 5

Have you tested your App in the foreground?
Yes I do. To clarify, here is more detailed test:

I have two test functions: One array for gps collecting and one counter for background mode checking. Both are running under interval set.

(on iOS)
Foreground:
Counter works, number is increased.
GPS works, data is added to array.

Background:
Counter works, number is increased.
GPS not works, data is either not collected (with ngCordova geolocation plugin) or is oddly duplicated with JS navigator.geolocation

Have you enabled background location mode for your ios app? before doing the build, check your .plist file do ensure your app has "background modes" enabled and "location services" are checked/listed under "background modes".

在此处输入图片说明

Also ensure that your .plist file contains NSLocationAlwaysUsageDescription , without which the device will not ask for Always usage permission for location and location will only be available based on NSLocationWhenInUseDescription .

Update:
You will need one more step to ensure app correctly asks 'always' running in background permission.

How:
- Search in your xcode project for CDVLocation.m file, then find code block contain NSLocationAlwaysUsageDescription
- Reorder if else conditions, move NSLocationAlwaysUsageDescription condition to first line

确保应用要求始终后台运行许可

(optional)
If youre doing your builds via ionic cli, run ionic prepare ios , then open your project in xcode and confirm the above settings, as they usually get overwritten by cordova plugins, unless you've specifically set up scripts for after_install and other events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM