取决于用户决定取消选中开关

时间:2015-05-18 18:10:41

标签: ios objective-c uialertview uilocalnotification uiswitch

我正在构建一个应用程序,要求用户在用户启用切换时发布通知的权限。我正在使用此代码:

- (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
    }
}
}

所需的行为如下:

  • 用户幻灯片切换
  • 警报要求许可
  • 代码等待用户决定
  • 要么在我接受的任何地方进行编码,要么禁用切换。

当前行为使代码立即运行,并且不等待用户决定。如何更改此项以获得所需的行为?

谢谢!

2 个答案:

答案 0 :(得分:1)

注意:这是变通方法

调用此方法后: - [UIApplication sharedApplication] registerUserNotificationSettings并且用户为该应用授予推送通知,然后AppDelegate中的didRegisterForRemoteNotificationsWithDeviceToken被解雇,

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

}

所以你需要做的是,你可以从上述方法调用NSNotification,相应地更新UI。

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

            // Accepted
   }

答案 1 :(得分:0)

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