Change push notifications programmatically in Swift

There isn't any way you can change push notifications permission status from program. Also, prompt asking user to allow push notifications can not be shown again and again. You can refer this https://developer.apple.com/library/ios/technotes/tn2265/_index.html.

The first time a push-enabled app registers for push notifications, iOS asks the user if they wish to receive notifications for that app. Once the user has responded to this alert it is not presented again unless the device is restored or the app has been uninstalled for at least a day.

So using UISwitch to toggle permission status doesn't make any sense unless you use switch status to turn on/off remote notifications from your server.


Updated with swift 4 :

func switchChanged(sender: UISwitch!) {
    print("Switch value is \(sender.isOn)")

    if(sender.isOn){
        print("on")
        UIApplication.shared.registerForRemoteNotifications()
    }
    else{
        print("Off")
        UIApplication.shared.unregisterForRemoteNotifications()
    }
}

if you are using FireBase to send push notifications to your devices , you can use topic subscription to enable push notification in subscribed devices and unsubscribe users from the topic when you don't want the user to receive push notification in those devices that have been unsubscribed.

to subscribe a user to a topic simply import Firebase, then use this method:

Messaging.messaging().subscribe(toTopic: "topicName")

and to unsubscribe a user use:

Messaging.messaging().unsubscribe(fromTopic: "topicName")