【问题标题】:Unchecking a switch depending on user decision根据用户决定取消选中开关
【发布时间】:2015-05-18 18:10:40
【问题描述】:

我正在构建一个应用程序,该应用程序会在用户启用开关时询问用户是否允许发布通知。我正在使用此代码:

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

期望的行为如下:

  • 用户滑动开关
  • 警报请求许可
  • 代码等待用户决定
  • 要么执行我在接受的地方编写的任何代码,要么禁用开关。

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

谢谢!

【问题讨论】:

  • 你看过你得到的委托回调了吗?
  • 阅读registerUserNotificationSettings 的文档中的讨论。它会告诉你如何处理。
  • @rmaddy 正在阅读。我看到文档告诉您:“如果您的应用程序显示警报、播放声音或标记其图标,您必须在启动周期内调用此方法以请求以这些方式提醒用户的权限。”。但是当用户滑动一个开关时,我会调用它,因为他们知道他们在“注册”什么,从而为用户提供了更多的上下文。这是否允许?
  • 阅读讨论的第二段。
  • @rmaddy 我读过它,但我不太确定。讨论的第一部分说“您必须在启动周期内调用此方法以请求以这些方式提醒用户的权限”,而再往下则说“建议您在安排任何本地之前调用此方法通知或注册推送通知服务。”

标签: ios objective-c uialertview uilocalnotification uiswitch


【解决方案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
   }

【讨论】:

    【解决方案2】:
        //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];
        }
    

    【讨论】:

      猜你喜欢
      • 2015-07-03
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-29
      • 1970-01-01
      • 2015-05-05
      相关资源
      最近更新 更多