gpt4 book ai didi

ios - 根据用户决定取消选中开关

转载 作者:行者123 更新时间:2023-11-29 02:01:33 25 4
gpt4 key购买 nike

我正在构建一个应用程序,当用户启用开关时,它会请求用户允许发布通知。我正在使用这段代码:

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

所需的行为如下:

  • 用户滑动切换
  • 警报请求许可
  • 代码等待用户决定
  • 无论我在已接受的地方编写什么代码,都执行//或禁用开关。

当前的行为是让代码一次性跑完,不等用户决定。我如何更改它以获得所需的行为?

谢谢!

最佳答案

注意:这是解决方法

一旦您调用此方法:- [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
}

关于ios - 根据用户决定取消选中开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30310127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com