gpt4 book ai didi

ios - 如何在顶部显示消息?

转载 作者:行者123 更新时间:2023-11-28 08:30:58 25 4
gpt4 key购买 nike

我是 WatchKit 开发的新手。无论当前正在使用什么应用程序或 watch 是否处于事件状态,我都想显示一条消息,例如内置定时器应用程序如何显示标签“定时器完成”。然后,用户应该能够单击“确定”按钮并关闭消息。

我已尝试同时使用警报和模态视图,但以编程方式显示它们仍然需要我的应用处于事件状态。使用通知系统不是一个可行的解决方案,因为这将依赖于 iPhone。

我已经坚持这个问题好几个小时了,任何见解都会有所帮助,谢谢。

最佳答案

您需要先在您的 iOS 应用程序中请求许可:

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound])
{ (granted, error) in
if !granted
{
print("User did not give permissions to send notitications...")
}
}

然后在你的 watchOS 应用程序中你可以为将来的某个时间创建一个通知,确保你给它一个 UUID(这可能是 watchOS3 中的一个错误):

    let content: UNMutableNotificationContent = UNMutableNotificationContent()

content.title = "Title"

content.subtitle = "Subtitle"

content.body = "message"

content.sound = UNNotificationSound.default()

content.categoryIdentifier = "aCategory"

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: duration, repeats: false)

let id: String = WatchNotify.getUUID()
let request = UNNotificationRequest.init(identifier: id,
content: content,
trigger: trigger)

UNUserNotificationCenter.current().add(request)
{
(error) in // ...
}

....

class func getUUID() -> String
{
let uuidObj = CFUUIDCreate(nil)
let uuidString = CFUUIDCreateString(nil, uuidObj)!
return uuidString as String
}

关于ios - 如何在顶部显示消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38895436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com