chore: add mobile app

This commit is contained in:
2026-02-07 22:33:02 +01:00
parent db34640acc
commit e89384ad4e
29327 changed files with 3886944 additions and 12 deletions
@@ -0,0 +1,7 @@
// Copyright 2016-present 650 Industries. All rights reserved.
#import <ExpoLocation/EXBaseLocationRequester.h>
@interface EXBackgroundLocationPermissionRequester : EXBaseLocationRequester
@end
@@ -0,0 +1,157 @@
// Copyright 2016-present 650 Industries. All rights reserved.
#import <ExpoLocation/EXBackgroundLocationPermissionRequester.h>
#import <objc/message.h>
#import <CoreLocation/CLLocationManagerDelegate.h>
static SEL alwaysAuthorizationSelector;
@interface EXBackgroundLocationPermissionRequester ()
@property (nonatomic, assign) bool wasAsked;
@property (nonatomic, assign) bool isWaitingForTimeout;
@end
@implementation EXBackgroundLocationPermissionRequester
- (instancetype)init
{
if (self = [super init]) {
_wasAsked = false;
_isWaitingForTimeout = false;
}
return self;
}
+ (NSString *)permissionType
{
return @"locationBackground";
}
+ (void)load
{
alwaysAuthorizationSelector = NSSelectorFromString([@"request" stringByAppendingString:@"AlwaysAuthorization"]);
}
- (void)requestLocationPermissions
{
if ([EXBaseLocationRequester isConfiguredForAlwaysAuthorization] && [self.locationManager respondsToSelector:alwaysAuthorizationSelector]) {
_wasAsked = true;
CLAuthorizationStatus status = [self.locationManager authorizationStatus];
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
// We already have a foreground permission granted:
// When asking for background location, we might or might not have asked for foreground permission
// before we get here. An issue here is if the user has a temporary permission ("Allow once") - which
// results in the status being "kCLAuthorizationStatusAuthorizedWhenInUse" - without us knowing.
// We need to handle this special case which is not possible to detect through the API.
// What we do is that we'll wait 1.5 seconds on an UIApplicationWillResignActiveNotification
// notification (which will be emitted almost directly if the permission dialog is displayed). If the permission
// dialog is not displayed we'll timeout and can resolve the waiting promise with an updated denied status.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleAppBecomingInactive)
name:UIApplicationWillResignActiveNotification
object:nil];
// Setup timeout - if no permission dialog was displayed we can just stop listening and deny
// the request
[self setupAppInactivateTimeout];
}
// Request permissions
((void (*)(id, SEL))objc_msgSend)(self.locationManager, alwaysAuthorizationSelector);
} else {
self.reject(@"ERR_LOCATION_INFO_PLIST", @"One of the `NSLocation*UsageDescription` keys must be present in Info.plist to be able to use geolocation.", nil);
self.resolve = nil;
self.reject = nil;
}
}
- (void)handleAppBecomingActive
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
if (self.resolve) {
self.resolve([self getPermissions]);
self.resolve = nil;
self.reject = nil;
}
}
- (void)handleAppBecomingInactive
{
// Let's wait until the app becomes inactive - this happens when OS displays the
// permission dialog - then we can cancel the timeout handler.
_isWaitingForTimeout = false;
[[NSNotificationCenter defaultCenter] removeObserver:self];
// When the app is inactive it means that a permission dialog is showing and we should ask to be
// notified when the dialog is closed:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleAppBecomingActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
- (void)setupAppInactivateTimeout
{
_isWaitingForTimeout = true;
// Obtain a reference to the current queue
dispatch_queue_t currentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
// Calculate the time for the delay
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC));
EX_WEAKIFY(self);
// Schedule the block to be executed after the delay
dispatch_after(delayTime, currentQueue, ^{
EX_ENSURE_STRONGIFY(self)
// Check if we are still waiting - ie. we haven't seen a permission dialog
if (self.isWaitingForTimeout && self.resolve) {
self.isWaitingForTimeout = false;
self.resolve([self getPermissions]);
self.resolve = nil;
self.reject = nil;
}
});
}
- (NSDictionary *)parsePermissions:(CLAuthorizationStatus)systemStatus
{
EXPermissionStatus status;
switch (systemStatus) {
case kCLAuthorizationStatusAuthorizedAlways: {
status = EXPermissionStatusGranted;
break;
}
case kCLAuthorizationStatusDenied:
case kCLAuthorizationStatusRestricted: {
status = EXPermissionStatusDenied;
break;
}
case kCLAuthorizationStatusAuthorizedWhenInUse: {
if (_wasAsked) {
status = EXPermissionStatusDenied;
} else {
status = EXPermissionStatusUndetermined;
}
break;
}
case kCLAuthorizationStatusNotDetermined:
default: {
status = EXPermissionStatusUndetermined;
break;
}
}
return @{ @"status": @(status), @"scope": @(systemStatus == kCLAuthorizationStatusAuthorizedWhenInUse ? "whenInUse" : systemStatus == kCLAuthorizationStatusAuthorizedAlways ? "always" : "none") };
}
@end
@@ -0,0 +1,20 @@
// Copyright 2016-present 650 Industries. All rights reserved.
#import <CoreLocation/CLLocationManager.h>
#import <ExpoModulesCore/EXPermissionsInterface.h>
#import <CoreLocation/CLLocationManagerDelegate.h>
@interface EXBaseLocationRequester : NSObject<EXPermissionsRequester, CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) EXPromiseResolveBlock resolve;
@property (nonatomic, strong) EXPromiseRejectBlock reject;
+ (BOOL)isConfiguredForWhenInUseAuthorization;
+ (BOOL)isConfiguredForAlwaysAuthorization;
- (void)requestLocationPermissions;
- (NSDictionary *)parsePermissions:(CLAuthorizationStatus)systemStatus;
@end
@@ -0,0 +1,162 @@
// Copyright 2016-present 650 Industries. All rights reserved.
#import <ExpoLocation/EXBaseLocationRequester.h>
#import <ExpoModulesCore/EXUtilities.h>
#import <objc/message.h>
@interface EXBaseLocationRequester () <CLLocationManagerDelegate>
@property (nonatomic, assign) bool locationManagerWasCalled;
@property (nonatomic, assign) CLAuthorizationStatus beginStatus;
@end
@implementation EXBaseLocationRequester
# pragma mark - Abstract methods
- (void)requestLocationPermissions
{
@throw([NSException exceptionWithName:@"NotImplemented" reason:@"requestLocationPermissions should be implemented" userInfo:nil]);
}
+ (NSString *)permissionType {
@throw([NSException exceptionWithName:@"NotImplemented" reason:@"permissionType should be implemented" userInfo:nil]);
}
- (NSDictionary *)parsePermissions:(CLAuthorizationStatus)systemStatus
{
@throw([NSException exceptionWithName:@"NotImplemented" reason:@"parsePermissions should be implemented" userInfo:nil]);
}
# pragma mark - UMPermissionsRequester
- (NSDictionary *)getPermissions {
CLAuthorizationStatus systemStatus;
if (![EXBaseLocationRequester isConfiguredForAlwaysAuthorization] && ![EXBaseLocationRequester isConfiguredForWhenInUseAuthorization]) {
EXFatal(EXErrorWithMessage(@"This app is missing usage descriptions, so location services will fail. Add one of the `NSLocation*UsageDescription` keys to your bundle's Info.plist. See https://bit.ly/3iLqy6S (https://docs.expo.dev/distribution/app-stores/#system-permissions-dialogs-on-ios) for more information."));
systemStatus = kCLAuthorizationStatusDenied;
} else {
systemStatus = [CLLocationManager authorizationStatus];
}
return [self parsePermissions:systemStatus];
}
- (void)requestPermissionsWithResolver:(EXPromiseResolveBlock)resolve rejecter:(EXPromiseRejectBlock)reject {
NSDictionary *existingPermissions = [self getPermissions];
if (existingPermissions && [existingPermissions[@"status"] intValue] != EXPermissionStatusUndetermined) {
// since permissions are already determined, the iOS request methods will be no-ops.
// just resolve with whatever existing permissions.
resolve(existingPermissions);
} else {
_resolve = resolve;
_reject = reject;
EX_WEAKIFY(self)
[EXUtilities performSynchronouslyOnMainThread:^{
EX_ENSURE_STRONGIFY(self)
self.locationManagerWasCalled = false;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
}];
// 1. Why do we call CLLocationManager methods by those dynamically created selectors?
//
// Most probably application code submitted to Apple Store is statically analyzed
// paying special attention to camelcase(request_always_location) being called on CLLocationManager.
// This lets Apple warn developers when it notices that location authorization may be requested
// while there is no NSLocationUsageDescription in Info.plist. Since we want to neither
// make Expo developers receive this kind of messages nor add our own default usage description,
// we try to fool the static analyzer and construct the selector in runtime.
// This way behavior of this requester is governed by provided NSLocationUsageDescriptions.
// 2. Location permission request types
//
// Foreground
// - "Allow once"
// - "Allow while using App"
// - "Don't allow"
//
// Background
// - "Keep only while using"
// - "Change to always allow"
//
// Requesting background permissions directly without first asking for foreground permissions is the
// same as asking for foreground permissions and then asking for background permissions.
//
// "Allow once" is a temporary permission (limited to the current app session). It is not possible to get
// info from the API about wether or not the current permission is temporary. You cannot request background
// permissions with a temporary token - a background request will then return denied.
//
// Requesting background permissions directly and "Allow while using the App" gives you a provisional
// background permission that can later be elevated to a full "Always allow" permission.
// You will be asked at a later point if you want to convert to "Always allow". The system waits until
// you have started using the newly aquired permission before showing the permission dialog.
//
// Test the following scenarios in BareExpo -> APIs -> Location
// ------------------------------------------------------------
// (before tests, make sure to clear any location permissions and restart the app)
//
// rfp = requestForegroundPermissionsAsync, fp: Actual foreground permission given
// rbp = requestBackgroundPermissionsAsync, bg: Actual background permission given
//
// - rfp -> "Allow once", then rbp -> no dialog = (fp: granted (temporary), bg: denied after 1.5 seconds)
// - rfp -> "Allow while using App", then rbp -> "Keep only while using" = (fp: granted, bg: denied)
// - rfp -> "Allow while using App", then rbp -> "Change to always allow" = (fp: granted, bg: granted)
// - rfp -> "Don't allow", then rbp -> no dialog = (fp: denied, bg: denied)
// - rbp -> "Allow once", no more dalogs = (fp: granted (temporary), bg: denied)
// - rbp -> "Allow while using App", no more dialogs = (fp: granted, bg: granted (provisional))
// - rbp -> "Don't allow" = (fp: denied, bg: denied)
// Save start statue and call requestLocationPermissions
_beginStatus = [self.locationManager authorizationStatus];
[self requestLocationPermissions];
}
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
if (_reject) {
_reject(@"E_LOCATION_ERROR_UNKNOWN", error.localizedDescription, error);
_resolve = nil;
_reject = nil;
}
}
- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager
{
CLAuthorizationStatus nextState = [manager authorizationStatus];
if (_beginStatus == nextState && !_locationManagerWasCalled) {
// CLLocationManager calls this delegate method once on start with kCLAuthorizationNotDetermined even before the user responds
// to the "Don't Allow" / "Allow" dialog box. This isn't the event we care about so we skip it. See:
// http://stackoverflow.com/questions/30106341/swift-locationmanager-didchangeauthorizationstatus-always-called/30107511#30107511
_locationManagerWasCalled = true;
return;
}
if (_resolve) {
_resolve([self getPermissions]);
_resolve = nil;
_reject = nil;
}
}
#pragma mark - Helpers
+ (BOOL)isConfiguredForWhenInUseAuthorization
{
return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] != nil;
}
+ (BOOL)isConfiguredForAlwaysAuthorization
{
return [self isConfiguredForWhenInUseAuthorization] && [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysAndWhenInUseUsageDescription"];
}
@end
@@ -0,0 +1,8 @@
// Copyright 2016-present 650 Industries. All rights reserved.
#import <ExpoLocation/EXBaseLocationRequester.h>
@interface EXForegroundPermissionRequester : EXBaseLocationRequester
@end
@@ -0,0 +1,60 @@
// Copyright 2016-present 650 Industries. All rights reserved.
#import <ExpoLocation/EXForegroundPermissionRequester.h>
#import <ExpoModulesCore/EXUtilities.h>
#import <objc/message.h>
#import <CoreLocation/CLLocationManagerDelegate.h>
static SEL whenInUseAuthorizationSelector;
@implementation EXForegroundPermissionRequester
+ (NSString *)permissionType
{
return @"locationForeground";
}
+ (void)load
{
whenInUseAuthorizationSelector = NSSelectorFromString([@"request" stringByAppendingString:@"WhenInUseAuthorization"]);
}
- (void)requestLocationPermissions
{
if ([EXBaseLocationRequester isConfiguredForWhenInUseAuthorization] && [self.locationManager respondsToSelector:whenInUseAuthorizationSelector]) {
((void (*)(id, SEL))objc_msgSend)(self.locationManager, whenInUseAuthorizationSelector);
} else {
self.reject(@"ERR_LOCATION_INFO_PLIST", @"The `NSLocationWhenInUseUsageDescription` key must be present in Info.plist to be able to use geolocation.", nil);
self.resolve = nil;
self.reject = nil;
}
}
- (NSDictionary *)parsePermissions:(CLAuthorizationStatus)systemStatus
{
EXPermissionStatus status;
switch (systemStatus) {
case kCLAuthorizationStatusAuthorizedWhenInUse:
case kCLAuthorizationStatusAuthorizedAlways: {
status = EXPermissionStatusGranted;
break;
}
case kCLAuthorizationStatusDenied:
case kCLAuthorizationStatusRestricted: {
status = EXPermissionStatusDenied;
break;
}
case kCLAuthorizationStatusNotDetermined:
default: {
status = EXPermissionStatusUndetermined;
break;
}
}
return @{ @"status": @(status), @"scope": @(systemStatus == kCLAuthorizationStatusAuthorizedWhenInUse ? "whenInUse" : systemStatus == kCLAuthorizationStatusAuthorizedAlways ? "always" : "none") };
}
@end
@@ -0,0 +1,7 @@
// Copyright 2016-present 650 Industries. All rights reserved.
#import <ExpoLocation/EXBaseLocationRequester.h>
@interface EXLocationPermissionRequester : EXBaseLocationRequester
@end
@@ -0,0 +1,72 @@
// Copyright 2016-present 650 Industries. All rights reserved.
#import <ExpoLocation/EXLocationPermissionRequester.h>
#import <objc/message.h>
#import <CoreLocation/CLLocationManagerDelegate.h>
static SEL alwaysAuthorizationSelector;
static SEL whenInUseAuthorizationSelector;
@implementation EXLocationPermissionRequester
+ (NSString *)permissionType
{
return @"location";
}
+ (void)load
{
alwaysAuthorizationSelector = NSSelectorFromString([@"request" stringByAppendingString:@"AlwaysAuthorization"]);
whenInUseAuthorizationSelector = NSSelectorFromString([@"request" stringByAppendingString:@"WhenInUseAuthorization"]);
}
- (void)requestLocationPermissions
{
if ([EXBaseLocationRequester isConfiguredForAlwaysAuthorization] && [self.locationManager respondsToSelector:alwaysAuthorizationSelector]) {
((void (*)(id, SEL))objc_msgSend)(self.locationManager, alwaysAuthorizationSelector);
} else if ([EXBaseLocationRequester isConfiguredForWhenInUseAuthorization] && [self.locationManager respondsToSelector:whenInUseAuthorizationSelector]) {
((void (*)(id, SEL))objc_msgSend)(self.locationManager, whenInUseAuthorizationSelector);
} else {
self.reject(@"E_LOCATION_INFO_PLIST", @"One of the `NSLocation*UsageDescription` keys must be present in Info.plist to be able to use geolocation.", nil);
self.resolve = nil;
self.reject = nil;
}
}
- (NSDictionary *)parsePermissions:(CLAuthorizationStatus)systemStatus
{
EXPermissionStatus status;
NSString *scope = @"none";
switch (systemStatus) {
case kCLAuthorizationStatusAuthorizedWhenInUse: {
status = EXPermissionStatusGranted;
scope = @"whenInUse";
break;
}
case kCLAuthorizationStatusAuthorizedAlways: {
status = EXPermissionStatusGranted;
scope = @"always";
break;
}
case kCLAuthorizationStatusDenied:
case kCLAuthorizationStatusRestricted: {
status = EXPermissionStatusDenied;
break;
}
case kCLAuthorizationStatusNotDetermined:
default: {
status = EXPermissionStatusUndetermined;
break;
}
}
return @{
@"status": @(status),
@"scope": scope
};
}
@end