Rahul Vyas Rahul Vyas - 3 months ago 20
iOS Question

Check if push enabled or location enabled ios sdk

I'm using following code to detect these 2 (Push Notifications and Location Services)

[postvars setObject:([CLLocationManager locationServicesEnabled])?@"true":@"false" forKey:@"locationServicesEnabled"];

BOOL pushEnabled = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
[postvars setObject:(pushEnabled)?@"true":@"false" forKey:@"pushServicesEnabled"];


But the issue is I'm always getting true for both even If I tap Don't allow when prompted in the app. Also in settings app I checked that Location is set to Never and notifications subheading shows off. What's wrong with this code ? can anyone guide me in this.

Answer

Just checking [CLLocationManager locationServicesEnabled] is not enough.

if([CLLocationManager locationServicesEnabled] && 
   [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
    [postvars setObject:@"true" forKey:@"locationServicesEnabled"];

}

For Notifications Check this awesome SO answer

BOOL pushEnabled = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
[postvars setObject:(pushEnabled)?@"true":@"false" forKey:@"pushServicesEnabled"];

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) 
   {
    [postvars setObject:@"false" forKey:@"pushServicesEnabled"];
}
Comments