Erik
Erik

Reputation: 2530

Unchecking a switch depending on user decision

I'm building an app that asks the user for permission to post notifications when the user enables a switch. I'm using this code:

- (IBAction)mySwitchValueChanged:(id)sender {
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]]; // ask the user for permission
}

if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) { // Check it's iOS 8 and above
    UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
    if (grantedSettings.types != UIUserNotificationTypeNone)
    {
        // Accepted
    } else
    {
        [self.mySwitch setOn:NO]; // Declined
    }
}
}

The desired behaviour is as follows:

The current behaviour makes the code run through at once, and doesn't wait for the user to decide. How can I change this to get the desired behaviour?

Thanks!

Upvotes: 0

Views: 50

Answers (2)

Naresh Reddy M
Naresh Reddy M

Reputation: 1096

    //Call the below method in your else part and remove the line
    //[self.mySwitch setOn:NO];

    -(void)Showalert{
    UIAlertview*Temp=[[UIAlertview alloc]initWithTitle:@"Need Permission to Send Notifications" message:@"The App Wants The Permission to Work Properly!\nPermit The App in Notification settings"delegate:self cancelButtonTitle:@"Okay!" otherButtonTitles:nil];
     [Temp show];        
    }
    #pragma mark Alertview delegate method
    - (void)alertViewCancel:(UIAlertView *)alertView{
    //Remove the Below line From Else part of your code
      [self.mySwitch setOn:NO];
    }

Upvotes: 0

Vizllx
Vizllx

Reputation: 9246

Note: This is workaround idea

Once you call this method :- [UIApplication sharedApplication] registerUserNotificationSettings and user grants push notification for the app, then didRegisterForRemoteNotificationsWithDeviceToken in AppDelegate get fired,

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  //call a notification when user granted Push Notifiaction 
   [[NSNotificationCenter defaultCenter] 
    postNotificationName:@"PushNotificationSuccess" 
    object:self];

}

So what you have to do, you can call a NSNotification from the mentioned method, to update the UI accordingly.

- (void)updateUIOnNotification:(NSNotification *) notification
{

            // Accepted
   }

Upvotes: 1

Related Questions