如何在 Widgets/Today Extension 单击上打开特定视图控制器 [英] How to open Specific View controller on Widgets/ Today Extension click

查看:24
本文介绍了如何在 Widgets/Today Extension 单击上打开特定视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在小部件单击上打开特定的视图控制器,但无法打开它,我可以使用 url 模式打开应用程序,但我想打开特定的视图控制器我该怎么做,这里是代码使用 url 架构打开应用程序:

I am trying to open specific view controller on widgets click , but can not able to open it , i am able to open app using url schema but i want to open specific view controller how can i do this, here is code for open app using url schema :

@IBAction func open_app(_ sender: Any){ extensionContext?.open(URL(string: "open://")! ,完成处理程序:无)} 按钮单击我正在使用 url 模式成功地打开应用程序.但现在我想在点击时打开特定的视图控制器,我该怎么做?

@IBAction func open_app(_ sender: Any) { extensionContext?.open(URL(string: "open://")! , completionHandler: nil) } on button click i am opeing app sucessfully using url schema. but now i want to open specific view controller on that click how can i do this?

推荐答案

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

According to your requirement, I have created a sample to get this working correctly.

1. 首先在 TodayViewController 界面中,创建 3 个不同的 UIButtons 并赋予它们的 tag 值来唯一标识它们.

1. First of all in TodayViewController interface, create 3 different UIButtons and give their tag values to uniquely identify them.

这里我将标签设为:1, 2, 3 to First, Second and Third UIButton.

Here I have given tags as: 1, 2, 3 to First, Second and Third UIButton.

2.接下来,您需要编写代码以从 Today Extension 打开您的包含应用.在 TodayViewController 中创建一个 @IBAction 并将其连接到所有三个 UIButtons.

2. Next you need to write the code to open your Containing App from Today Extension. In TodayViewController create an @IBAction for and connect it to all three UIButtons.

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

在上面的代码中,标签将被添加到url方案中,以标识需要在UIButton上打开哪个UIViewController 按.所以网址看起来像:open://1

In the above code, tag will be added to the url scheme to identify which UIViewController needs to be opened on UIButton press. So the url will look something like: open://1

3.Containing App'sURL Types中需要为URL Scheme做一个入口,即

3. In the Containing App's URL Types need to make an entry for URL Scheme, i.e

从上面的屏幕截图中可以明显看出,无需为要从扩展程序打开的每个 url 进行输入.URLs 具有相同的 url scheme 只有一个条目.

As evident from the above screenshot, there is no need to make entry for each url that you want to open from your extensions. URLs having same url scheme have only a single entry.

4.包含应用从扩展中打开时,你可以在AppDelegate的 application(_ : url:sourceApplication: 注释:) 方法.在这里,您可以处理要打开的控制器,即

4. When the containing app is opened from extension, you can get the handle in AppDelegate’s application(_ : url: sourceApplication: annotation: ) method. Here, you can handle which controller to open i.e.

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 的scheme,即openurl.host 标识了其中的宿主组件URL 当前设置为 UIButton 的标记值,您可以使用它来唯一标识按下了哪个 UIButton 以及相应的下一步.

url.scheme identifies the scheme of URL i.e. open and url.host identifies the host component in the URL which is currently set to the UIButton's tag value which you can use to uniquely identify which UIButton is pressed and what to de next accordingly.

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

For more on Today Extensions, you can refer to: https://hackernoon.com/app-extensions-and-today-extensions-widget-in-ios-10-e2d9fd9957a8

如果您在这方面仍然遇到任何问题,请告诉我.

Let me know if you still face any issues regarding this.

这篇关于如何在 Widgets/Today Extension 单击上打开特定视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆