Skip to content

Commit

Permalink
fix(setLocation): post new location notification twice (workaround). (#…
Browse files Browse the repository at this point in the history
…95)

The new location notification is posted twice as a workaround to overcome the issue of not notifying the app for the new location that was set on the first attempt after setting a new location permissions.

We post the new location notifications twice, and the location is set only on the second attempt.

The mentioned bug also happens when setting the location directly through the Simulator app (without AppleSimUtils).
  • Loading branch information
asafkorem authored Jul 12, 2022
1 parent fdf3248 commit 9c74907
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions applesimutils/applesimutils/SetSimulatorLocation.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,39 @@

#import "SetSimulatorLocation.h"

static NSString * const kLocationNotificationLatitudeKey = @"simulateLocationLatitude";
static NSString * const kLocationNotificationLongitudeKey = @"simulateLocationLongitude";
static NSString * const kLocationNotificationDevicesKey = @"simulateLocationDevices";
static NSString * const kLocationNotificationName = @"com.apple.iphonesimulator.simulateLocation";

@implementation SetSimulatorLocation

+ (void)setLatitude:(double)latitude longitude:(double)longitude forSimulatorUDIDs:(NSArray<NSString*>*)udids
{
[NSDistributedNotificationCenter.defaultCenter postNotificationName:@"com.apple.iphonesimulator.simulateLocation" object:nil userInfo:@{
@"simulateLocationLatitude": @(latitude),
@"simulateLocationLongitude": @(longitude),
@"simulateLocationDevices": udids,
}];
+ (void)setLatitude:(double)latitude longitude:(double)longitude
forSimulatorUDIDs:(NSArray<NSString *> *)udids {
[self postNewLocationNotification:@{
kLocationNotificationLatitudeKey: @(latitude),
kLocationNotificationLongitudeKey: @(longitude),
kLocationNotificationDevicesKey: udids
}];
}

+ (void)clearLocationForSimulatorUDIDs:(NSArray<NSString *> *)udids {
[self postNewLocationNotification:@{
kLocationNotificationDevicesKey: udids
}];
}

+ (void)clearLocationForSimulatorUDIDs:(NSArray<NSString *> *)udids
{
[NSDistributedNotificationCenter.defaultCenter postNotificationName:@"com.apple.iphonesimulator.simulateLocation" object:nil userInfo:@{
@"simulateLocationDevices": udids,
}];
/// Post the new location configurations over the distributed notification center.
///
/// @note The notification is posted twice as a workaround to overcome the issue of not notifying
/// the app for the new location that was set on the first attempt after setting a new location
/// permissions. We post the new location notifications twice, and the location is set on the
/// second attempt. The mentioned bug also happens when setting the location directly through the
/// Simulator app (without AppleSimUtils).
+ (void)postNewLocationNotification:(NSDictionary *)info {
auto notificationCenter = NSDistributedNotificationCenter.defaultCenter;
[notificationCenter postNotificationName:kLocationNotificationName object:nil userInfo:info];
[notificationCenter postNotificationName:kLocationNotificationName object:nil userInfo:info];
}

@end

0 comments on commit 9c74907

Please sign in to comment.