繁体   English   中英

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

[英]Trigger local notification permission dialog from button press in Swift

从按钮按下触发通知权限对话框的正确方法是什么?

所有教程都将权限对话框触发器放置在 appDelegate 中,因此一旦加载应用程序,就会出现权限对话框。

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

@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