Trigger local notification permission dialog from button press in Swift

时间:2015-07-28 17:02:48

标签: ios swift

What is the correct way to trigger a notification permissions dialog from a button press?

All of the tutorials place the permission dialog trigger in appDelegate so as soon as the application is loaded a permissions dialog appears.

1 个答案:

答案 0 :(得分:3)

我想你刚回答了自己的问题。将要求通知权限的代码移动到按下按钮调用的函数。它可能看起来像这样:

@IBAction func buttonPressed() {
    registerUserNotificationSettings()
}

func registerUserNotificationSettings() {
    let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
    let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}