gpt4 book ai didi

swift3 - 如何在“小部件/今日扩展”上打开特定 View Controller ,请单击

转载 作者:行者123 更新时间:2023-12-04 22:27:56 25 4
gpt4 key购买 nike

我正在尝试在小部件click上打开特定的 View Controller ,但无法打开它,我能够使用url模式打开应用程序,但是我想打开特定的 View Controller ,我该怎么做,这是使用open app的代码网址架构:

单击按钮上的@IBAction func open_app(_ sender: Any)
{ extensionContext?.open(URL(string: "open://")! ,
completionHandler: nil)
}
我正在使用url模式成功运行应用程序。但是现在我想在该单击上打开特定的 View Controller ,我该怎么做?

最佳答案

根据您的要求,我创建了一个示例来使其正常工作。

1. 首先,在TodayViewController界面中,创建3个不同的UIButtons,并为其提供 tag 值以唯一地标识它们。
enter image description here

在这里,我给了标记作为:1, 2, 3First, Second and Third UIButton

2. 接下来,您需要编写代码以从“今日扩展”中打开包含应用程序的。在TodayViewController中,为创建一个@IBAction并将其连接到所有三个UIButtons

@IBAction func openAppController(_ sender: UIButton)
{
if let url = URL(string: "open://\(sender.tag)")
{
self.extensionContext?.open(url, completionHandler: nil)
}
}

在上面的代码中,标签将被添加到 url方案中,以标识在按UIViewController时需要打开哪个UIButton。因此,该网址将类似于: open://1

3. 。在包含应用程序的 URL Types中,需要输入URL Scheme,即

enter image description here

从上面的屏幕截图可以明显看出,无需为要从扩展名中打开的每个URL进行输入。具有相同URLs url scheme仅具有单个条目

4. 当从扩展程序打开包含应用程序
时,您可以使用AppDelegate’s application(_ : url: sourceApplication: annotation: )方法获取该句柄。在这里,您可以处理要打开哪个 Controller ,即
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
{
if url.scheme == "open"
{
switch url.host
{
case "1":
//Open First View Controller

case "2":
//Open Second View Controller

case "3":
//Open Third View Controller

default:
break
}
}
return true
}
url.scheme标识URL的方案,即openurl.host标识URL中的主机组件,该主机组件当前设置为UIButton's tag value,您可以使用它来唯一标识按下的UIButton和相应的下一步操作。

有关的更多信息,今天扩展,您可以引用:https://hackernoon.com/app-extensions-and-today-extensions-widget-in-ios-10-e2d9fd9957a8

让我知道您是否仍然面临与此有关的任何问题。

关于swift3 - 如何在“小部件/今日扩展”上打开特定 View Controller ,请单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49150602/

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