swift - How to turn on and off notifications in iOS? -


i trying implement settings screen in ios 11 app, need uiswitch used control user notifications. when set on off, want give permissions notifications, when set on, want request permissions (standard dialog asking user permission send notifications).

to ask permission, found following code:

unusernotificationcenter.current().requestauthorization(options: [.alert, .sound]) { (granted, error) in     // enable or disable features based on authorization. } 

however, if turn off notifications app in system settings, code not pop dialog request, , returns false in granted.

i not able find on how give permissions.

any hint on how solve problem? possible, or apple assume task should left system settings?

in ios turn on/off permission push notification appears 1 time. in order achieve need tweak can check first whether notification enabled or not.

func pushenabledatoslevel() -> bool {  guard let currentsettings = uiapplication.shared.currentusernotificationsettings?.types else { return false }  return currentsettings.rawvalue != 0 } 

after can create customize popup turnon/off button , navigate system setting page user can enable option accordingly

if let appsettings = nsurl(string: uiapplicationopensettingsurlstring) {     uiapplication.shared.openurl(appsettings url) } 

Comments

Popular posts from this blog

service - Android MediaPlayer calls onCompletion before it already finished -

javascript - Training Neural Network to play flappy bird with genetic algorithm - Why can't it learn? -

javascript - Create a stacked percentage column -