Skip to content

Commit 704308d

Browse files
committed
[[ Bug 22999 ]] Allow background location updates on iOS
This patch adds a new command "iphoneAllowBackgroundLocationUpdates" that can allow/disallow location updates when the app is suspended.
1 parent ae4936f commit 704308d

File tree

10 files changed

+114
-1
lines changed

10 files changed

+114
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Name: iphoneAllowBackgroundLocationUpdates
2+
3+
Type: command
4+
5+
Syntax: iphoneAllowBackgroundLocationUpdates <pAllow>
6+
7+
Summary:
8+
Allows the app to receive location updates when it is suspended
9+
10+
Introduced: 9.6.3
11+
12+
OS: ios
13+
14+
Platforms: mobile
15+
16+
Example:
17+
mobileStartTrackingSensor "location"
18+
iphoneAllowBackgroundLocationUpdates "true"
19+
20+
Example:
21+
iphoneAllowBackgroundLocationUpdates "false"
22+
23+
Parameters:
24+
pAllow (boolean):
25+
A boolean value specifying if the app should continue to receive
26+
location updates when put in the background.
27+
28+
29+
Description:
30+
Use the <iphoneAllowBackgroundLocationUpdates> command to allow (or disallow) the app
31+
receiving location updates when it is suspended.
32+
33+
This command has an effect only if "Location Update" is checked in the
34+
"Background Execution" section in the iOS standalone settings.
35+
36+
37+
38+
References: mobileStartTrackingSensor (command), mobileStopTrackingSensor (command),
39+
40+

docs/notes/bugfix-22999.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Fix background location updates on iOS
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Allow background location updates
2+
3+
A new command `iphoneAllowBackgroundLocationUpdates` has been added, which can be used to
4+
allow/disallow location updates when the app is suspended. This command has an effect only
5+
if "Location Update" is checked in the "Background Execution" section in the iOS standalone
6+
settings.

engine/rsrc/mobile-template.plist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@
5252
<key>UIBackgroundModes</key>
5353
<array>
5454
${PLAY_AUDIO_WHEN_IN_BACKGROUND}
55+
${BACKGROUND_LOCATION_UPDATE}
56+
${BACKGROUND_VOIP}
57+
${BACKGROUND_NEWSSTAND_DOWNLOADS}
58+
${EXTERNAL_ACC_COMM}
59+
${USE_BT_LE}
60+
${ACTS_AS_BT_LE}
61+
${BACKGROUND_FETCH}
62+
${REMOTE_NOTIFICATIONS}
5563
</array>
5664
<key>UIViewControllerBasedStatusBarAppearance</key>
5765
<false/>

engine/src/exec-sensor.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ void MCSensorExecStopTrackingSensor(MCExecContext& ctxt, intenum_t p_sensor)
101101
}
102102
}
103103

104+
void MCSensorAllowBackgroundLocationUpdates(MCExecContext& ctxt, bool p_allow)
105+
{
106+
MCSystemAllowBackgroundLocationUpdates(p_allow);
107+
}
108+
104109
void MCSensorGetSensorAvailable(MCExecContext& ctxt, intenum_t p_sensor, bool& r_available)
105110
{
106111
MCSystemGetSensorAvailable((MCSensorType)p_sensor, r_available);

engine/src/exec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3914,6 +3914,7 @@ extern MCExecEnumTypeInfo *kMCSensorTypeTypeInfo;
39143914
void MCSensorExecStartTrackingSensor(MCExecContext& ctxt, intenum_t p_sensor, bool p_loosely);
39153915
void MCSensorExecStopTrackingSensor(MCExecContext& ctxt, intenum_t p_sensor);
39163916
void MCSensorGetSensorAvailable(MCExecContext& ctxt, intenum_t p_sensor, bool& r_available);
3917+
void MCSensorAllowBackgroundLocationUpdates(MCExecContext& ctxt, bool p_allow);
39173918

39183919
void MCSensorGetDetailedLocationOfDevice(MCExecContext& ctxt, MCArrayRef &r_detailed_location);
39193920
void MCSensorGetLocationOfDevice(MCExecContext& ctxt, MCStringRef &r_location);

engine/src/mblandroidsensor.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ bool MCSystemGetSensorAvailable(MCSensorType p_sensor, bool& r_available)
7474
return true;
7575
}
7676

77+
void MCSystemAllowBackgroundLocationUpdates(bool p_allow)
78+
{
79+
// not implemented
80+
}
81+
7782
////////////////////////////////////////////////////////////////////////////////
7883
/*
7984
bool MCSystemStartTrackingSensor(MCSensorType p_sensor, bool p_loosely)

engine/src/mblhandlers.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,30 @@ Exec_stat MCHandleSensorAvailable(void *p_context, MCParameter *p_parameters)
13361336
return ES_ERROR;
13371337
}
13381338

1339+
Exec_stat MCHandleAllowBackgroundLocationUpdates(void *p_context, MCParameter *p_parameters)
1340+
{
1341+
MCExecContext ctxt(nil, nil, nil);
1342+
ctxt . SetTheResultToEmpty();
1343+
1344+
bool t_allow_background_location_updates = false;
1345+
1346+
if (p_parameters)
1347+
{
1348+
MCAutoValueRef t_value;
1349+
MCAutoBooleanRef t_bool;
1350+
p_parameters->eval_argument(ctxt, &t_value);
1351+
if(ctxt . ConvertToBoolean(*t_value, &t_bool))
1352+
t_allow_background_location_updates = MCValueIsEqualTo(*t_bool, kMCTrue);
1353+
}
1354+
1355+
MCSensorAllowBackgroundLocationUpdates(ctxt, t_allow_background_location_updates);
1356+
1357+
if (!ctxt . HasError())
1358+
return ES_NORMAL;
1359+
1360+
return ES_ERROR;
1361+
}
1362+
13391363
Exec_stat MCHandleCanTrackLocation(void *p_context, MCParameter *p_parameters)
13401364
{
13411365
MCExecContext ctxt(nil, nil, nil);
@@ -4555,6 +4579,7 @@ static const MCPlatformMessageSpec s_platform_messages[] =
45554579

45564580
// MM-2012-02-11: Added support old style senseor syntax (iPhoneEnableAcceleromter etc)
45574581
/* DEPRECATED */ {false, "iphoneCanTrackLocation", MCHandleCanTrackLocation, nil},
4582+
{false, "iphoneAllowBackgroundLocationUpdates", MCHandleAllowBackgroundLocationUpdates, nil},
45584583

45594584
// PM-2014-10-07: [[ Bug 13590 ]] StartTrackingLocation and StopTrackingLocation must run on the script thread
45604585
/* DEPRECATED */ {true, "iphoneStartTrackingLocation", MCHandleLocationTrackingState, (void *)true},

engine/src/mbliphonesensor.mm

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ static void requestAlwaysAuthorization(void)
255255

256256
NSString *t_location_authorization_when_in_use;
257257
t_location_authorization_when_in_use = [t_info_dict objectForKey: @"NSLocationWhenInUseUsageDescription"];
258-
258+
259259
if (t_location_authorization_always)
260260
{
261261
[s_location_manager requestAlwaysAuthorization];
@@ -294,6 +294,27 @@ static void initialize_core_location(void)
294294

295295
////////////////////////////////////////////////////////////////////////////////
296296

297+
void MCSystemAllowBackgroundLocationUpdates(bool p_allow)
298+
{
299+
NSDictionary *t_info_dict;
300+
t_info_dict = [[NSBundle mainBundle] infoDictionary];
301+
302+
NSArray *t_background_modes_array;
303+
t_background_modes_array = [t_info_dict objectForKey: @"UIBackgroundModes"];
304+
305+
BOOL t_plist_can_allow_background_location_updates = [t_background_modes_array containsObject: @"location"];
306+
307+
if (t_plist_can_allow_background_location_updates)
308+
{
309+
initialize_core_location();
310+
311+
if (p_allow)
312+
s_location_manager.allowsBackgroundLocationUpdates = YES;
313+
else
314+
s_location_manager.allowsBackgroundLocationUpdates = NO;
315+
}
316+
}
317+
297318
bool MCSystemGetSensorAvailable(MCSensorType p_sensor, bool& r_available)
298319
{
299320
switch (p_sensor)

engine/src/mblsensor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ bool MCSystemStartTrackingRotationRate(bool p_loosely);
8080
bool MCSystemStopTrackingRotationRate();
8181

8282
bool MCSystemGetSensorAvailable(MCSensorType p_sensor, bool& r_available);
83+
void MCSystemAllowBackgroundLocationUpdates(bool p_allow);
8384

8485
bool MCSystemStartTrackingSensor(MCSensorType p_sensor, bool p_loosely);
8586
bool MCSystemStopTrackingSensor(MCSensorType p_sensor);

0 commit comments

Comments
 (0)