Skip to content

Commit

Permalink
Merge pull request #20 from inoshperera/master
Browse files Browse the repository at this point in the history
 Location off in device handling
  • Loading branch information
Kamidu Sachith Punchihewa authored Mar 20, 2018
2 parents b500308 + 2a3fade commit 3f8da67
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
2 changes: 2 additions & 0 deletions iOSMDMAgent/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) AVAudioPlayer *theAudio;

extern NSInteger const LOCATION_OFF_CODE;

- (void)showLoginViewController;
- (void)authorizeLocationService;

Expand Down
47 changes: 30 additions & 17 deletions iOSMDMAgent/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
#import <MediaPlayer/MediaPlayer.h>

#define systemSoundID 1154
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

@interface AppDelegate ()
@end

@implementation AppDelegate

NSInteger const LOCATION_OFF_CODE = 1000;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

NSLog(@"App is starting...");
Expand All @@ -31,14 +34,21 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
// Set a movement threshold for new events.
self.locationManager.distanceFilter = 10; // meters

// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
if (![CLLocationManager locationServicesEnabled]) {
NSString *message = @"Turn on location services and let the app find device's location when necessery."
"Go to Settings->Privacy->Location and enable.";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Turn On Location Services"
message:message delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
alertView.tag = LOCATION_OFF_CODE;
[alertView show];
} else {
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self authorizeLocationService];
}
}

NSLog(@"Authorizing location service");
[self authorizeLocationService];

NSString *enrollURL = [URLUtils getEnrollmentURLFromPlist];
NSString *serverURL = [URLUtils getServerURLFromPlist];
if(enrollURL && ![@"" isEqualToString:enrollURL] && serverURL && ![@"" isEqualToString:serverURL]) {
Expand Down Expand Up @@ -255,21 +265,24 @@ - (void) registerForPushToken {
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
NSLog(@"User has changed location authorization. Requesting authorization");
[self authorizeLocationService];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Location Service Authorization"
message:@"Turn on location services and let the app find device's location"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Turn on location services", nil];
[alertView show];
NSLog(@"didChangeAuthorizationStatus");
if ([CLLocationManager locationServicesEnabled]) {
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self authorizeLocationService];
}
}
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
if(alertView.tag && LOCATION_OFF_CODE == alertView.tag) {
NSLog(@"Opening location settings");
NSString* url = SYSTEM_VERSION_LESS_THAN(@"10.0") ?
@"prefs:root=LOCATION_SERVICES" : @"App-Prefs:root=Privacy&path=LOCATION";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];
} else {
if(buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
}

Expand Down

0 comments on commit 3f8da67

Please sign in to comment.