从应用程序扩展显示UIAlertController



我正在为iOS开发一个消息过滤器扩展,我有点卡住了。我希望能够在收到新消息并将其归类为垃圾邮件时显示UIAlertController
到目前为止,我尝试过的代码(在扩展的句柄方法中(:

DispatchQueue.main.async {
let alertController = UIAlertController(title: " TITLE ", message: " MESSAGE ", preferredStyle: UIAlertController.Style.alert)
alertController.addAction(UIAlertAction(title: "Close", style: UIAlertAction.Style.cancel, handler: nil))
let alertWindow = UIWindow(frame: UIScreen.init().bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindow.Level.alert + 1;
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alertController, animated: true, completion: nil)
}

所以我的问题是:考虑到即使应用程序本身关闭,过滤器也可以工作,是否可以在消息过滤器扩展中显示UIAlertController?(如果是这样的话,我在上面的实现中缺少了什么?(

您需要一个UIWindow来展示您的UIAlertController,但您只是创建了一个不附加到任何东西的UIViewController。因此,您绝对不能在扩展中显示UIAlertController

相关内容

  • 没有找到相关文章

最新更新