ionic-team / capacitor-plugins

Official plugins for Capacitor ⚑️
520 stars 585 forks source link

[@capacitor/geolocation] not working with ios #2023

Closed dominikse closed 8 months ago

dominikse commented 8 months ago

Bug Report

Plugin(s)

capacitor/geolocation@5.0.7

Capacitor Version

Latest Dependencies:
  @capacitor/cli: 5.6.0
  @capacitor/core: 5.6.0
  @capacitor/android: 5.6.0
  @capacitor/ios: 5.6.0

Installed Dependencies:
  @capacitor/android: not installed
  @capacitor/cli: 5.6.0
  @capacitor/core: 5.6.0
  @capacitor/ios: 5.6.0

[success] iOS looking great! πŸ‘Œ

Platform(s)

iOS

Current Behavior

Ionic blank starter app with only geolocation plugin for test purposes.

In web (Google Chrome via "ionic serve") it prompts for location permissions and if granted it resolves the location. Works like expected in web.

With iOS it isn't working at all and this statement "let coord = await Geolocation.getCurrentPosition();" is not resolving maybe because permissions are not being prompted? Also no error is being thrown/catched.

Even if trying to re-prompt the permissions manually by calling "Geolocation.requestPermissions();" nothing shows up and getCurrentPosition is not resolving at all, so no result.

Not working in iOS Simulators (17.2) and also not on actual iOS devices (17.2.1).

Expected Behavior

Coordinates should be returned/resolved like in web environment (Google Chrome).

Code Reproduction

https://github.com/dominikse/ionic-cap-geotest

Other Technical Details

iOS platform details (cap sync):

capacitor sync [capacitor] βœ” Copying web assets from www to ios/App/App/public in 284.51ms [capacitor] βœ” Creating capacitor.config.json in ios/App/App in 634.58ΞΌs [capacitor] βœ” copy ios in 300.65ms [capacitor] βœ” Updating iOS plugins in 2.66ms [capacitor] [info] Found 5 Capacitor plugins for ios: [capacitor] @capacitor/app@5.0.7 [capacitor] @capacitor/geolocation@5.0.7 [capacitor] @capacitor/haptics@5.0.7 [capacitor] @capacitor/keyboard@5.0.8 [capacitor] @capacitor/status-bar@5.0.7 [capacitor] βœ” Updating iOS native dependencies with pod install in 1.99s [capacitor] βœ” update ios in 2.02s [capacitor] βœ” copy web in 3.29ms [capacitor] βœ” update web in 219.00ΞΌs [capacitor] [info] Sync finished in 2.689s

jcesarmobile commented 8 months ago

You are missing the required usage description

<key>NSLocationWhenInUseUsageDescription</key>
<string>I want to know where the user is</string>

In older iOS versions Xcode would tell you or even would make your app crash, but it looks like on iOS 17 it no longer tells you the usage descriptions are missing, so maybe the plugin should reject if no usage description is present.

dominikse commented 8 months ago

You are right, thanks a lot! Great! This resolved the issue. I've had only supplied "NSLocationAlwaysAndWhenInUseUsageDescription" assuming this will cover all the usage descriptions.

jcesarmobile commented 8 months ago

@capacitor/geolocation doesn't work in the background, so you only need NSLocationWhenInUseUsageDescription.

NSLocationAlwaysUsageDescription is in the docs I linked, but it's deprecated and we have removed it in the Capacitor 6 docs and documenting it needs to be removed. That's why I only mentioned NSLocationWhenInUseUsageDescription.

So you don't need NSLocationAlwaysAndWhenInUseUsageDescription but NSLocationWhenInUseUsageDescription, unless you have another geolocation plugin that works in background.

ionitron-bot[bot] commented 8 months ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of the plugin, please create a new issue and ensure the template is fully filled out.