我有一个 Swift 中的 Xcode 项目,其目标如下:
- iOS 应用程序
- WatchKit 扩展/WatchKit 应用
- “公共(public)”项目,由“主”项目和扩展使用
在公共(public)项目中我有以下代码:
public class func scheduleNotification(seconds: Int) {
var notification = UILocalNotification()
notification.fireDate = NSDate().dateByAddingTimeInterval(NSTimeInterval(seconds))
notification.alertBody = "item"
notification.alertAction = "open"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["UUID": "XX", ]
notification.category = "CATEGORY"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
我可以调用此方法并从 iOS 应用程序触发通知:
@IBAction func XXX(sender: AnyObject) {
NotificationHelper.scheduleNotification(100)
}
但是从 WatchKit 扩展执行的相同代码不会触发任何通知。该方法被调用,但 iPhone 或 Apple Watch 上没有任何反应,也没有触发任何通知。
有人可以帮助我吗?
如何安排来自 WatchKit 扩展逻辑的通知?
最佳答案
不允许应用扩展访问sharedApplication
。我猜测在您的情况下 sharedApplication
返回 nil
,这可以解释为什么未安排通知。
我建议使用类似 openParentApplication:reply:
的方式在后台打开您的主机 iOS 应用程序并从那里安排通知。
关于ios - 从 WatchKit 触发 UILocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30102806/