尝试使用 localnotification 呈现其视图不在窗口层次结构中的 UIAlertController [重复]

2024-01-22

我试图在用户单击本地通知后呈现 AlertView。 AlertView 有取消或确定选项。

extension ViewController:UNUserNotificationCenterDelegate{


    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        print("Tapped in notification")
        if(defaults.object(forKey: "alertOn") != nil){


            // Create the alert controller
            let alertController = UIAlertController(title: "Some text", message: "Some text again", preferredStyle: .alert)


            // Create the actions
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                UIAlertAction in

            }

            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
                UIAlertAction in
            }

            // Add the actions
            alertController.addAction(okAction)
            alertController.addAction(cancelAction)

            // Present the controller
                self.present(alertController, animated: true, completion: nil)

        }
    }


func triggerNotification(){

        print("notification will be triggered in five seconds..Hold on tight")
        let content = UNMutableNotificationContent()
        content.title = "SomeText"
        content.body = "Some more text"
        content.sound = UNNotificationSound.default()

        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5.0, repeats: false)
        let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)

        UNUserNotificationCenter.current().delegate = self as! UNUserNotificationCenterDelegate
        UNUserNotificationCenter.current().add(request){(error) in

            if (error != nil){

                print(error?.localizedDescription)
            }
        }
    }

它应该向我显示警报视图,其中包含确定或取消请求的选项。相反,它向我显示消息UIAlertController 其视图不在窗口层次结构中

当我将警报视图放入 viewdidappear 时,它工作正常,但是当我将其放入 userNotificationCenter 时,我的 AlertView 不会附加到主视图。

代码简述

extension ViewController:UNUserNotificationCenterDelegate{
          func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

         Present UIAlertView

        }
}

ViewController{

Call to notification when app is in background 
triggerNotification()

triggerNotification(){
 Definition 
}
}

try this

func currentTopViewController() -> UIViewController {
    var topVC: UIViewController? = UIApplication.shared.delegate?.window?.rootViewController
    while topVC?.presentedViewController {
        topVC = topVC?.presentedViewController
    }
    return topVC!
}

并将 VC 表示为

let currentTopVC: UIViewController? = self.currentTopViewController()

currentTopVC.present(alertController, animated: true, completion: nil)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试使用 localnotification 呈现其视图不在窗口层次结构中的 UIAlertController [重复] 的相关文章

  • Swift 中的 import 语句是否有相关成本?

    阅读字符串宣言 我看到一个段落 https github com apple swift blob master docs StringManifesto md batteries included关于避免Foundation不需要的时候导
  • UIView晃动动画

    我试图在按下按钮时使 UIView 摇动 我正在调整我找到的代码http www cimgf com 2008 02 27 core animation tutorial window shake effect http www cimgf
  • iOS - UITableViewCell 使文本加粗

    我有一个字符串 NSString userInfo James Johnson james 我想做的就是大胆James Johnson并保留 james正常字体 所以我尝试过的是使用NSAttributedString但为了完成这个过程 我
  • 无法在 ios 应用程序中通过 googlecast 正确投射视频

    我正在开发一个基于 AVPlayer 的自定义视频播放器项目 尝试整合谷歌演员 我已经根据谷歌图进行了集成 https codelabs developers google com codelabs cast videos ios http
  • 从未调用过交互式委托方法

    我想在 ViewController 1 和 NavigationViewController 2 之间进行交互式转换 NavigationController 通过按钮调用 因此呈现时没有交互转换 它可以通过按钮或 UIPanGestur
  • BigQuery 未显示链接的 Firebase Analytics 事件日志的任何数据集

    我将我的帐户链接到 Big Query 但 Firebase Analytics 事件不会自动加载到 BigQuery 中 显示 未找到数据集 警告 我的工作进度附在下面 请查收 I have getting firebase Analyt
  • 将类型传递给通用 Swift 扩展,或者理想情况下推断它

    说你有 class Fancy UIView 你想找到所有兄弟姐妹Fancy意见 没问题 https stackoverflow com q 37232743 294884 for v UIView in superview subview
  • Swift:长按手势识别器 - 检测轻击和长按

    我想连接一个动作 如果手势是点击 它会以特定的方式为对象设置动画 但如果按下持续时间超过 0 5 秒 它会执行其他操作 现在 我刚刚连接了动画 我不知道如何区分长按和点击 如何访问新闻持续时间以实现上述目的 IBAction func ta
  • 我的 UICollectionView 无法使用 Swift 平滑滚动

    我有一个CollectionView它使单元出队取决于message类型 例如 文本 图像 我遇到的问题是当我向上 向下滚动时滚动确实很不稳定 因此用户体验不是很好 这仅在第一次加载单元格时发生 之后滚动就会平滑 我有什么想法可以解决这个问
  • AVAudioPlayer 无法从网站播放 m4a 或 mp3 文件类型

    我试图在我的应用程序中找到一个仅纯 m4a 声音的 URL 我有音频的 URL 理论上可以下载它 然后 使用下载的文件URL到声音 我尝试使用AVAudioPlayer播放它 但它不播放任何声音 这是我的代码 在 URL 检索函数中 我调用
  • 动态调度协议扩展不适用于多个目标

    这是我的主要目标中的代码 所以不是测试目标 protocol ProtocolA func dontCrash extension ProtocolA func dontCrash fatalError func tryCrash dont
  • 使用 UITabBarController 时覆盖整个屏幕的视图?

    我想在 UITabBarController 设置中在整个屏幕上覆盖 HUD 样式的透明图形 执行此操作的按钮位于第一个选项卡的屏幕 FirstViewController 中 并且覆盖层也应该覆盖选项卡 这可能吗 您可以将新视图直接附加到
  • 通过 Button Swift 中的标签发送行和部分

    我里面有这个cellForRowAtIndexPath cell plusBut tag indexPath row cell plusBut addTarget self action plusHit forControlEvents U
  • 从现有坐标地图套件中查找最近的位置

    我正在为拥有多家商店的客户开发 iPhone 应用程序 目标 C 我有数组中所有商店 20 的坐标 纬度 长 目前我正在考虑循环遍历商店坐标数组并获取从用户当前位置到商店位置的距离 然后将它们添加到数组中并按最小距离进行排序 这是正确的方法
  • UICollectionView setLayout:animated: 不保留 zIndex

    我注意到打电话时setLayout animated in a UICollectionView要在两个布局之间切换 当前可见的单元格不遵循zIndex它的布局属性已设置在layoutAttributesForItemAtIndexPath
  • 在 SwiftUI 中使用可观察对象切换视图

    我正在练习尝试使用 SwiftUI 中的可观察对象切换视图 但我的代码无法正常工作 我知道我可以用 State 来做到这一点 但我想用可观察的对象来实现这一点 当我单击内容视图中的图像时 图像不会改变 有人能帮我吗 内容视图 swift i
  • iOS:Facebook 登录访问令牌错误:由于模拟器错误,回退到从 NSUserDefaults 加载访问令牌

    根据说明进行配置后 我不断收到此错误 并且无法在我的应用程序上成功使用 Facebook 登录 我在 XCode 8 1 上运行它并使用 iOS 10 1 模拟器 我按照 Facebook iOS SDK 指南中的步骤操作 并将 Faceb
  • 诊断和仪器均缺少“僵尸”选项

    运行 Xcode 4 0 2 Zombie 选项丢失 其他 SO 帖子建议找到它的两个地方 Product gt Run looks like this Product gt Profile looks like this 奇怪的是 我之前
  • Mac 上的 Delphi - 可能吗? [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我负责一个 Delphi Win32 项目管理应用程序 我刚刚完成了向 Delphi 2009 的迁移
  • 使用日期 Swift 3 对字典数组进行排序

    我有一个名为 myArray 的数组 其中添加了字典 我希望该字典按时间排序 这是字典中的键 那个时间是在 String 中 时间的日期格式为 yyyy MM dd HH mm ss 我尝试使用下面的代码解决方案 但给出了 从 字符串转换

随机推荐