2

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.

Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256

2 Answers2

3

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"];
}
Community
  • 1
  • 1
pkc456
  • 8,350
  • 38
  • 53
  • 109
  • for your information the docs says /* * locationServicesEnabled * * Discussion: * Determines whether the user has location services enabled. * If NO, and you proceed to call other CoreLocation API, user will be prompted with the warning * dialog. You may want to check this property and use location services only when explicitly requested by the user. */ – Rahul Vyas Aug 29 '16 at 09:32
  • ideally it should return false. Don't you think ? – Rahul Vyas Aug 29 '16 at 09:35
  • Yes. Agree with your points. It must return false. Have you tried in device? – pkc456 Aug 29 '16 at 09:36
  • added your code and it worked but still no luck with Push. Thanks – Rahul Vyas Aug 29 '16 at 09:38
  • For notification, try to check `enabledRemoteNotificationTypes` as written in my updated answer. – pkc456 Aug 29 '16 at 09:43
  • got the answer from this thread http://stackoverflow.com/questions/28441007/checking-push-notification-registration-isregisteredforremotenotifications-not – Rahul Vyas Aug 29 '16 at 09:50
  • Yes, this [answer](http://stackoverflow.com/a/28441181/988169) is very useful. I have added the reference of same in my answer too. – pkc456 Aug 29 '16 at 09:52
2

Check if Push notifications Enabled or Location Services Enabled:

Push Notifications:

From iOS above 8.0 it registers the device and provides a Token even if the user opts out from pushes.

but, pushes are not presented to the user when the push is sent, so it will return true.

- (BOOL)isPushNotificationsEnabled {
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {
        UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
        return (types & UIUserNotificationTypeAlert);
    }
    else {
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        return (types & UIRemoteNotificationTypeAlert);
    }
}

enabledRemoteNotificationTypes is deprecated since iOS8.

To check remote notifications status in iOS8 you can use:

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

Check the answers here: detect “Allow Notifications” is on/off for iOS8

Read docs here:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/currentUserNotificationSettings

Also read the thread here: https://forums.developer.apple.com/thread/16958

Location Services:

Needs to check for both locationServicesEnabled and authorizationStatus like,

if([CLLocationManager locationServicesEnabled] && 
   [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
   //Enabled
}
Community
  • 1
  • 1
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51