如何使用交换机启用和禁用通知?

时间:2016-10-09 16:57:59

标签: ios objective-c notifications

我试图制作一个滑块,以便在它真实时为您提供弹出窗口以启用通知,如果它为假,则会禁用该应用的通知。这是我目前的代码。

-(IBAction)NotifSwitch:(id)sender {
if (_NotifSwitch.on) {

    UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert categories:nil];

    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
}
else {
    [[UIApplication sharedApplication] unregisterForRemoteNotifications];
}
    }

我使用AppDelegate中真实部分的代码来显示首次启动应用时的通知弹出窗口。虽然由于某种原因它不能在AppDelegate之外工作。

1 个答案:

答案 0 :(得分:0)

当然,当你的应用程序首次启动时,它不能在AppDelegate之外工作,代码只会执行一次。 你可以声明两个想要切换状态的控制器的方法。

-(void)switchOnRemoteNotification;
-(void)switchOffRemoteNotification;

当开关打开时,调用switchOn ...方法,相反,调用switchOff ...方法。

相关问题