我正在为iOS应用编写Today Widget。小部件有一些操作按钮。我想在有人点击它时收到点击事件。但是,它不应该启动应用。
我已经尝试this,但无济于事。
我目前的实现是定义一个URL Scheme,并在那些按钮上调用openURL,如下所示:
按钮1链接到myApp:// button1
按钮2链接到myApp:// button2
按钮3链接到myApp:// button3
我在AppDelegate的
中收到这些事件这是TodayWodgetController中的代码
@IBAction func widgetClicked(sender: UIButton){
if sender == button1 {
let u = NSURL(string: "myApp://button1")
self.extensionContext?.open(u! as URL, completionHandler: nil)
}
...
}
这是我在主机应用程序的AppDelegate中使用的代码
func application(_ app: UIApplication,
open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if url.absoluteString.range(of: "button1") != nil{
print ("Button 1 Pressed")
}
....
return true
}
但是,就像我说的那样,它也会启动主机App。我希望它只是发送点击事件而不启动应用程序。
任何帮助都将不胜感激。
答案 0 :(得分:0)
我认为从应用扩展程序中无法做到这一点。
Apple :
所述open(_ URL: URL, completionHandler: ((Bool) -> Void)? = nil)
的定义
要求系统代表当前正在运行的应用程序打开一个URL 扩展
每个扩展点确定是否支持此方法,或 在哪种条件下支持这种方法。在iOS 8中,只有 今天的扩展点(用于创建小部件)支持这一点 方法
重要说明:Apple允许小部件使用open(_:completionHandler :)方法打开小部件自己的小部件 应用强>