简体   繁体   English

从 Swift 中的按钮按下触发本地通知权限对话框

[英]Trigger local notification permission dialog from button press in 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.所有教程都将权限对话框触发器放置在 appDelegate 中,因此一旦加载应用程序,就会出现权限对话框。

I think you just answered your own question.我想你刚刚回答了你自己的问题。 Move the code that asks for notification permission to the function that gets called for your button press.将请求通知权限的代码移动到为您的按钮按下而调用的函数。 It might look something like this:它可能看起来像这样:

@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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM