Rahul Vyas
Rahul Vyas

Reputation: 28740

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.

Upvotes: 2

Views: 120

Answers (2)

Ronak Chaniyara
Ronak Chaniyara

Reputation: 5436

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
}

Upvotes: 2

pkc
pkc

Reputation: 8516

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"];
}

Upvotes: 3

Related Questions