Way to find out which button the user selected on the UIAlertView displayed by iOS ,asking permission to allow the app to receive push notification

StackOverflow https://stackoverflow.com/questions/21673919

Pregunta

Is there a way to interact with the alerts show by iOS.For eg: If my app has registered itself for APNS,on first launch, iOS shows an UIAlertView(I am assuming,it is one),giving the user two choices.Is there a way to find out which button the user selected? I have two alerts that are shown ,during my app launch,one for APNS and the other for Location Services.Is there a way to identify which alert is for what?

¿Fue útil?

Solución

In case you can't access those alerts directly I suggest you to look at this problem from another point of view.

For CoreLocation for example you can look at its [CLLocationManager authorizationStatus].

   kCLAuthorizationStatusNotDetermined = 0, // User wasn't proposed to use location services
   kCLAuthorizationStatusRestricted, // Parental control or something like that
   kCLAuthorizationStatusDenied,    // User didn't allow this application to use services
   kCLAuthorizationStatusAuthorized // User allowed to use his location.

As for APNS there are [[UIApplication sharedApplication] enabledRemoteNotificationTypes]

Source: https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIRemoteNotificationType

https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/doc/c_ref/CLAuthorizationStatus

Otros consejos

No. There is no way to get callbacks on the AlertViews created by the OS. Like CoolMonster indicated in his comment, you can find out what the user chose for that particular AlertView and do something based off that.

I don't know about push notifications but here's one (ugly) way of detecting the user's choice when asking for permissions to get user's location:

// After asking for permission, the alert is shown to the user. Since he can't do anything
// at this point but select one of the two options we simply wait...
while(([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined))
{
     sleep(1);
}
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)
{
    NSLog(@"User allowed access to gps");
}
else
{
    NSLog(@"User denied access to gps");
}

You can get notification types like this:

UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

And may create some hack ways to get user's interaction to Notifications question

Though there may not be a clear-cut way to know what button the user clicked on, you can certainly find out if the user authorized or not the use of push notifications by implementing these UIApplication delegate methods:

application:didRegisterForRemoteNotificationsWithDeviceToken: (User accepted)

application:didFailToRegisterForRemoteNotificationsWithError: (User denied or is not able to accept)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top