Skip to content

Instantly share code, notes, and snippets.

@jmoody
Last active March 18, 2016 19:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmoody/523123326f8e20dfdf0c to your computer and use it in GitHub Desktop.
Save jmoody/523123326f8e20dfdf0c to your computer and use it in GitHub Desktop.
Two ways to handle the "Some App wants to use your current location" alert

The problem is that this kind of UIAlert appears before Instruments can take control of the app.

In most cases, applications that ask for location services, access to contacts, access to photos, etc, are violating the Mobile HIG guidelines.

from the HIG

Ask permission at app startup only if your app can’t perform its primary function without the user’s data.

People will not be bothered by this if it’s obvious that the main function of your app depends on knowing their personal information.

Avoid making programmatic calls that trigger the alert before the user actually selects the feature that needs the data.

This way, you avoid causing people to wonder why your app wants their personal information when they’re doing something that doesn’t appear to need it.

For location data, check the Location Services preference to avoid triggering the alert unnecessarily.

If your app's primary function cannot be achieved without access to Location, Contacts, Photos, etc. then consider add a small delay before triggering these alerts.

notes

If this is not acceptable in your production app, then consider using a compiler macro that confines the delay for the *-cal target.

In some sense I consider this a feature of Calabash - it is exposing a potential problem (a HIG violation) in apps.

If all else fails, see the work around in the UIApplicationDelegate.m portion of this gist.

// Option 1. Delay the request for the user's location.
// delay for 3 seconds
CGFloat delay = 3.0
CLLocationManager *manager = ...
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[manager startUpdatingLocation];
});
// Option 2. Automatically authorize Location Services for the Calabash target.
// In the UIApplicationDelegate.m
// for the -cal target only
#ifdef CALABASH
// objc category on CLLocationManager to expose a private method
@interface CLLocationManager (Calabash)
+ (void)setAuthorizationStatus:(BOOL)arg1 forBundleIdentifier:(id)arg2;
@end
#endif
// in the application:didFinishLaunchingWithOptions: method
#ifdef CALABASH
NSString *bundleId = [[NSBundle mainBundle] bundleIdentifier];
NSLog(@"WARN: authorizing app for location services for %@", bundleId);
[CLLocationManager setAuthorizationStatus:YES forBundleIdentifier:bundleId];
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment