Mohammed Hussain
Mohammed Hussain

Reputation: 176

Want to enable location services through xcode

I'm new in ios and I want to know how can I enable location services through programming when I'm using this below....

enter image description here

As I'm using this label to show Whenever location service is disabled And an enable button(Green) to enable location services.But how to this programmatically.How will I get to know Location services are disabled and how the button will enable this.Please help me out.

Upvotes: 0

Views: 3712

Answers (1)

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

you can use CLLocationManager's locationServicesEnabled property to check the system-wide/overall availability. Use your CLLocationManagerDelegate's locationManager: didFailWithError: method and check for a kCLErrorDenied error to see if the user denied location services.

if([CLLocationManager locationServicesEnabled]){

           NSLog(@"Location Services Enabled");

           if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
                 [[[UIAlertView alloc] initWithTitle:@"App Permission Denied" 
                                                        message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil]show];

            }
        }

Choice-2

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

    NSLog(@"%@",error);
}

if location service disable for your app then its give you error

Error Domain=kCLErrorDomain Code=1 "The operation couldn’t be completed. (kCLErrorDomain error 1.)"

Update

if user denied the permission, we cant enable in programmatically, user can manually enable the location services, for more reference

Upvotes: 2

Related Questions