apache / cordova-plugin-geolocation

Apache Cordova Geolocation Plugin
https://cordova.apache.org/
Apache License 2.0
632 stars 657 forks source link

iOS 16 CDVLocation.m throwing UI unresponsiveness warning in XCode - causing app to crash. #257

Open rolinger opened 1 year ago

rolinger commented 1 year ago

Bug Report

A new feature in Xcode 14 is displaying purple warnings about UI Unresponsiveness coming from CDVLocation.m#L89 - this issue seems specific to iOS 16x too. Its a part of the isLocationServicesEnabled function.

This method can cause UI unresponsiveness if invoked on the main thread. Instead, consider waiting for the-locationManagerDidChangeAuthorization:callback and checkingauthorizationStatusfirst.

The warning gets thrown about 50 times too (not certain why though). I and others (on different forums) are experience app freeze/crashes with some getting apps rejected from app store.

Problem

iOS 16 apparently has changed behavior regarding location permission checks - the specific code commented with iOS 4.x - so it looks like a fix for iOS 4 and has worked all the way through iOS 15 is now causing issues.

What is expected to happen?

Work cleanly without throwing warnings.

What does actually happen?

Throws the above warning.

Environment, Platform, Device

Mac 13.2.1 Ventura Xcode 14.2

Version information

Cordova 11 cordova-ios: 6.2.0 iOS: 16.x

Checklist

rolinger commented 1 year ago

Also on CDVLocation.m, XCode 14 is now throwing Alerts on line #216 Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior

216

if (!__locationStarted || (__highAccuracyEngaled != enableHIghAccuracy)) {

I think should be: if (!__locationStarted || (__highAccuracyEngaled != self enableHIghAccuracy)) {

snblackout commented 4 months ago

Has anyone found a fork out there with this fix or have any idea how to solve this? I have an app update being blocked from release at Apple because of this. I hope I don't have to do a full re-write...

breautek commented 4 months ago

Has anyone found a fork out there with this fix or have any idea how to solve this? I have an app update being blocked from release at Apple because of this. I hope I don't have to do a full re-write...

Not aware of any forks but a rewrite shouldn't be necessary. The warning is caused by some APIs that checks states on the location manager, which may block for a long time so it isn't desirable to call on the main thread. If it does block for a significant of time, it will make hte app unresponsive since it will block UI updates. So the solution is to not call on those checks on the main thread.

However it's not as trivial as simply wrapping the code in a background block. The CLLocationManager instance is only safe to use on the thread/dispatch queue it was created in.

So the solution is probably to create a dispatch queue for geolocation usage and have the CLLocationManager initialized in that queue, as well as any cordova calls to switch to that queue when performing location manager calls. And of course to return back to the main queue when calling back to the webview.

snblackout commented 4 months ago

Thanks for the quick response. I should add that this particular app of ours is on a very old code base of AngularJS and Cordova/Phonegap :) So, yeah, not sure if how you explain is even possible.

breautek commented 4 months ago

Thanks for the quick response. I should add that this particular app of ours is on a very old code base of AngularJS and Cordova/Phonegap :) So, yeah, not sure if how you explain is even possible.

My advice in that case is to first spend time updating everything to the latest versions. You may find that actually solves your crashing issue.

The UI unresponsive warning doesn't cause crashes in itself, despite the title, although if the call does block the UI for longer than like 5 seconds the OS may kill the app. I personally never had this happen or had reports of this happening in my own apps, so I haven't found an excuse to prioritise this issue in my own work place. But I'd do my best to support any PR that does what I described in https://github.com/apache/cordova-plugin-geolocation/issues/257#issuecomment-1883740721

snblackout commented 4 months ago

Thanks. I'm not getting a crash or a UI freeze either. When XCode runs the app below is what is posted to the XCode session debug pane.

When I open the CDVLocation.m file in XCode, the warning is on line 89 in the isLocationServicesEnabled function of the module. Also the below comes up before I can even choose "Only once" or "When running" in iOS.

This method can cause UI unresponsiveness if invoked on the main thread. Instead, consider waiting for the `-locationManagerDidChangeAuthorization:` callback and checking `authorizationStatus` first.

locationManager::didFailWithError
snblackout commented 4 months ago

Well, my location details were set to lat/lon of 0,0 so that explains the locationManager::didFailWithError. I may not be having an issue after all as when I set the location to Apple, it returned coordinates and all was well. I appreciate your assistance but I don't think I'm having an issue.