AllowBackgroundUpdates in MobileLocation

How is AllowBackgroundUpdates supposed to work and how does it work, or modify MobileLocation.AuthorizationStates.AuthorizedAlways? If I set the authorizationState to Always is
AllowBackgroundUpdates (= True) even necessary?

Anywhere I put MyLocation.AllowBackgroundUpdates = True it immediately crashes the app…

Are you asking permission first?

Yup

Crashes even when I use:

If MyLocation.AuthorizationState = MobileLocation.AuthorizationStates.AuthorizedAlways Then
  myLocation.AllowBackgroundUpdates = True
end if

This is in Xojo 2023r4

That’s not what I mean. There’s a RequestAuthorization method that needs to be called as well as a string that needs to be set to indicate why you want the user’s location.

You can set the string by selecting the iOS build target and then on the advanced panel turn on Location under Capabilities and click the options button and set the strings.

Sorry I was incomplete in my previous post. Here is


If MyLocation.AuthorizationState = MobileLocation.AuthorizationStates.AuthorizedAlways Then
  ' we've got our requested authorization state, start getting LocationChanged events
  if GPSon and mbTileSet <> nil then MyLocation.Start
Else
  ' we don't have authorization yet, so ask for it
  MyLocation.RequestUsageAuthorization(MobileLocation.UsageTypes.Always)
End If

If MyLocation.AuthorizationState = MobileLocation.AuthorizationStates.AuthorizedAlways Then
  myLocation.AllowBackgroundUpdates = True
end if

Even though I am requesting alsays, the system gives me “AppInUse” (I know this because the message shown is the string I use for while app in use and not the string that I use for always access location and because in the System Setting locations permissions it shows while app in use for my app. This may be because a previous version of my app only used while in use for locations permissions. But even if I go to system settings and deny permission to access locations, the next time I run my app it asks only for permissions while in use, not always.

And if I manually setting locations permissions for my app to always in the system settings and then run the above code, it crashes on the device when it gets to myLocation.AllowBackgroundUpdates = True

Your code is still incorrect. RequestUsageAuthorization Is an asynchronous call and you have to wait until the event fires before you can call anything else. Once the user responds, the AuthorizationStateChanged event should fire and tell you what they selected.

1 Like

Thanks, Greg, for your insight. Right now, request authorization is in the Open Event of MyLocation (the Xojo example project has it in the open event of the Window that contains MyLocation). If I understand correctly,

If MyLocation.AuthorizationState = MobileLocation.AuthorizationStates.AuthorizedAlways Then
  myLocation.AllowBackgroundUpdates = True
end if

should go in the AuthorizationStateChanged event but the request authorization can stay in the open event?

Okay, I’m using the example project “Location” in the iOS examples folder.

In the Screen1 Opening Event, where it request authorization, I change AppInUse to Always:

If MyLocation.AuthorizationState = MobileLocation.AuthorizationStates.AuthorizedAlways Then
  // we've got our requested authorization state, start getting LocationChanged events
  myLocation.Start
Else
  // we don't have authorization yet, so ask for it
  myLocation.RequestUsageAuthorization(MobileLocation.UsageTypes.Always)
  MyLocation.Start
End If

The iOS system message only shows that I asked for AppInFront not Always.


I click Allow while using app because Always is not an option, however, the debugger shows that the authorization state is actually Always (= 3).

I add an AuthorizationStateChanged Event to MyLocation and in that event, I add:

if state = MobileLocation.AuthorizationStates.AuthorizedAlways then
  me.AllowBackgroundUpdates = True
end if

“me.AllowBackgroundUpdates = True” hard crashes in the simulator and on a device every time. No Xojo error message in the simulator crash, the app simple quits.

So it appears that I’m being even more clueless than my average state but I just don’t see where/how to use AllowBackgroundUpdates to turn on background updates.

By the way, the Visits Project in the iOS examples folder immediately crashes the simulator on launch as well without any modification. It appears to have AllowBackgroundUpdates set as well from the comments but I’m not sure where that is done.

I’m happy to report this as a bug if I’m not using it correctly but I’d like to know whether it really is a bug, first.

You should look at the console logs for the simulator you are using. The answer will probably be there.

Here’s the crash report. It happens as soon as


if myLocation.AuthorizationState = MobileLocation.AuthorizationStates.AuthorizedAlways then
  myLocation.AllowBackgroundUpdates = True
end if

is executed in the AuthorizationStateChanged event of myLocation.

As far as I can tell, this is a Xojo bug but, if anyone has a snippet of code that actually works to allow background updates, I’d love to see it.

-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Incident Identifier: 0D3F63E0-8879-44B1-8944-5C13B2CDC124
CrashReporter Key:   67CDF09E-C984-4750-FCEE-D466BA1C6166
Hardware Model:      Mac14,15
Process:             iOSLocation.debug [69886]
Path:                /Users/USER/Library/Developer/CoreSimulator/Devices/40FE56B4-5529-4794-8662-571B179498CE/data/Containers/Bundle/Application/EDB35A17-2C58-4E1C-B72A-82064FDE0608/iOSLocation.debug.app/iOSLocation.debug
Identifier:          com.example.ioslocation
Version:             1.0.0 (1)
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd_sim [15690]
Coalition:           com.apple.CoreSimulator.SimDevice.40FE56B4-5529-4794-8662-571B179498CE [18945]
Responsible Process: SimulatorTrampoline [943]

Date/Time:           2024-01-18 09:59:16.9708 -0500
Launch Time:         2024-01-18 09:59:16.7520 -0500
OS Version:          macOS 14.2.1 (23C71)
Release Type:        User
Report Version:      104

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Termination Reason: SIGNAL 6 Abort trap: 6
Terminating Process: iOSLocation.debug [69886]

Triggered by Thread:  0

Last Exception Backtrace:
0   CoreFoundation                	       0x1803e1174 __exceptionPreprocess + 216
1   libobjc.A.dylib               	       0x180193384 objc_exception_throw + 56
2   Foundation                    	       0x180746648 _userInfoForFileAndLine + 0
3   CoreLocation                  	       0x183af8d40 0x183ae6000 + 77120
4   iOSLocation.debug             	       0x1020c0d24 MobileLocation.AllowBackgroundUpdates.Set%%o<MobileLocation>i4b + 20
5   iOSLocation.debug             	       0x10214f570 Screen1.Screen1.MyLocation_AuthorizationStateChanged%%o<Screen1.Screen1>o<MobileLocation>i8<MobileLocation.AuthorizationStates> + 604 (/Screen1:236)
6   iOSLocation.debug             	       0x10214fd6c Delegate.IM_Invoke%%o<MobileLocation>i8<MobileLocation.AuthorizationStates> + 68
7   iOSLocation.debug             	       0x10214fdc8 AddHandler.Stub.2%%i8<MobileLocation.AuthorizationStates> + 72
8   rbframework.dylib             	       0x1034d7a34 0x103458000 + 522804
9   CoreLocation                  	       0x183b05470 0x183ae6000 + 128112
10  CoreLocation                  	       0x183b04dd0 0x183ae6000 + 126416
11  CoreLocation                  	       0x183ae98f0 0x183ae6000 + 14576
12  LocationSupport               	       0x188dd1ea0 0x188dcd000 + 20128
13  CoreFoundation                	       0x180350030 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
14  CoreFoundation                	       0x18034f2f4 __CFRunLoopDoBlocks + 408
15  CoreFoundation                	       0x180349bb0 __CFRunLoopRun + 764
16  CoreFoundation                	       0x1803493a8 CFRunLoopRunSpecific + 572
17  GraphicsServices              	       0x18c03c5ec GSEventRunModal + 160
18  UIKitCore                     	       0x184d937ac -[UIApplication _run] + 992
19  UIKitCore                     	       0x184d982e8 UIApplicationMain + 112
20  rbframework.dylib             	       0x1034fae44 0x103458000 + 667204
21  iOSLocation.debug             	       0x102060c68 Xojo._RuntimeRun + 36
22  iOSLocation.debug             	       0x1021519c4 _Main + 656 (/#main:42)
23  iOSLocation.debug             	       0x102150c70 main + 36
24  dyld_sim                      	       0x1028e1ca0 start_sim + 20
25  dyld                          	       0x1022d60e0 start + 2360
26  ???                           	0x320a000000000000 ???

Why are you not using the “state” parameter from the event?

I’ve tried it both ways and either one produces a hard crash

Did you set the strings for getting the users location under Capabilities?

Yes, the strings are set and I use a different one for Always versus when in front…

I’ve submitted a feedback issue (#75341)

In the capabilities again, under “background” did you set Location in there?

2 Likes

Bingo! That seems to have done the trick. Thanks very much, Greg!