From 9c749075519563baf1eb9620f7a7b49aae26e382 Mon Sep 17 00:00:00 2001 From: Asaf Korem <55082339+asafkorem@users.noreply.github.com> Date: Tue, 12 Jul 2022 19:02:43 +0300 Subject: [PATCH] fix(setLocation): post new location notification twice (workaround). (#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). --- .../applesimutils/SetSimulatorLocation.m | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/applesimutils/applesimutils/SetSimulatorLocation.m b/applesimutils/applesimutils/SetSimulatorLocation.m index 4a8b076..67b8790 100644 --- a/applesimutils/applesimutils/SetSimulatorLocation.m +++ b/applesimutils/applesimutils/SetSimulatorLocation.m @@ -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*)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 *)udids { + [self postNewLocationNotification:@{ + kLocationNotificationLatitudeKey: @(latitude), + kLocationNotificationLongitudeKey: @(longitude), + kLocationNotificationDevicesKey: udids + }]; +} + ++ (void)clearLocationForSimulatorUDIDs:(NSArray *)udids { + [self postNewLocationNotification:@{ + kLocationNotificationDevicesKey: udids + }]; } -+ (void)clearLocationForSimulatorUDIDs:(NSArray *)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