简体   繁体   中英

Working with iOS Location Manager

I am developing an app that has many view. Into my app sometimes the user arrives to a view where he can ask for his position clicking over a button. I am trying to follow the Apple guide lines to only ask for the user position if the user allows to do it. What should I do, use the next first code into the app delegate and declare a location manager attribute into any view that the user invokes, passing the location manager attribute to the new view and from the old view and asking with the second next code anytime that the user clicks the button to locate himself?; or just use the second code, declaring a location manager attribute only into the views that allow to get the user location with a button, to check if the location services are enable?

First snippet.

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

    // Override point for customization after application launch.

    // Add the navigation controller's view to the window and display.
    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    // Create a location manager instance to determine if location services are enabled. This manager instance will be
    // immediately released afterwards.
    CLLocationManager *manager = [[CLLocationManager alloc] init];
    if (manager.locationServicesEnabled == NO) {
        UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [servicesDisabledAlert show];
        [servicesDisabledAlert release];
    }
    [manager release];

    return YES;
}

Second snippet.

- (IBAction)locateUser:(id)sender {

    if([CLLocationManager locationServicesEnabled]) {
        self.locationManager = [[[CLLocationManager alloc] init] autorelease]; 
        self.locationManager.delegate = self;
    } else {
        [[[[UIAlertView alloc] initWithTitle:@"Location services." 
                                     message:@"Location services are disabled." 
                                    delegate:nil 
                           cancelButtonTitle:@"OK" 
                           otherButtonTitles:nil] autorelease] show];       
    }
}

Thanks for reading.

CoreLocation will handle all the alerts for you. If locations services are disabled and that you ask for the location, CoreLocation will show an alert telling so to the user with a button to go directly to Settings.app .

替代文字

If you want to know what append you can check for the delegate call

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

The error here contains a code that will be kCLErrorDenied if the user doesn't let the app use location services.

Also, you should use CoreLocation when the user need it. It's not necessary to check for location services at launch and the overhead of multiple CLLocationManager is almost inexistent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM