Ios 在设置控制器中启用/禁用通知

Ios 在设置控制器中启用/禁用通知,ios,objective-c,cocoa-touch,push-notification,Ios,Objective C,Cocoa Touch,Push Notification,我正在构建的IOS应用程序使用推送通知。苹果要求你在用户第一次运行应用程序时询问他们是否想要: if([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificatio

我正在构建的IOS应用程序使用推送通知。苹果要求你在用户第一次运行应用程序时询问他们是否想要:

if([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
但是,用户可以在使用应用程序期间改变主意,并使用UI开关在设置控制器中打开或关闭通知

如何捕获当前通知值并从通知中注册/注销用户? 这是我在互联网上发现的,但似乎不起作用:

[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

如果用户更改了首选项,则不必手动从通知中注销用户,但您可以随时使用此代码检查状态

if ([[UIApplication sharedApplication]  respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
    if ([[UIApplication sharedApplication]  isRegisteredForRemoteNotifications]){
        NSLog(@"Notifications Enabled iOS 8");
    } else {
        NSLog(@"Notifications Not Enabled iOS 8");
    }
} else {
    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types & UIRemoteNotificationTypeAlert) {
        NSLog(@"Notifications Enabled iOS 7 or older");
    } else {
        NSLog(@"Notifications Not Enabled iOS 7 or older");
    }
}
看这条线: