简体   繁体   English

来自锁定屏幕和通知中心的iOS Today小部件通用链接

[英]iOS Today Widget Universal Links From Lock Screen and Notification Center

We have a today widget to deep link into the app. 我们有一个今日小部件,可用于深入链接到该应用程序。 The deep links work just fine when the user accesses the widget from the home screen. 当用户从主屏幕访问窗口小部件时,深层链接就可以正常工作。 However, when a user accesses the widget when the device is locked, or when the user slides down from the top of the screen, the links open in Safari. 但是,当用户在设备锁定时访问窗口小部件时,或者当用户从屏幕顶部向下滑动时,链接会在Safari中打开。

I was wondering if anyone else has come across this issue, and if so, how they solved it. 我想知道是否还有其他人遇到过这个问题,如果是,他们是如何解决的。

Here was the solution we came upon (Swift 4.1). 这是我们遇到的解决方案(Swift 4.1)。 We needed to support a custom URL scheme to tell iOS that we can open links from the today widget. 我们需要支持一个自定义URL方案,以告诉iOS我们可以打开今日小部件中的链接。 This uses a different UIApplication delegate function. 这使用了另一个UIApplication委托函数。 Along with implementing func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool , we also need to implement func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool 除了实现func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool ,我们还需要实现func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool

First, in Info.plist , we have our supported schemes under CFBUndleURLTypes . 首先,在Info.plist ,我们在CFBUndleURLTypes下拥有我们支持的方案。

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>todayWidgetScheme</string>
        </array>
    </dict>
</array>

Then, also in Info.plist , we also listed the scheme under LSApplicationQueriesSchemes . 然后,也在Info.plist ,我们还在LSApplicationQueriesSchemes下列出了该方案。

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>todayWidgetScheme</string>
</array>

Next, when opening the link from the today widget, set the url scheme to the iOS recognized todayWidgetScheme. 接下来,从“今日”小部件中打开链接时,将url方案设置为iOS公认的“今天的小部件”方案。

func openAppFromTodayWidget() {
    if let url = URL(string: "https://url.com") {
        var components = URLComponents(url: url, resolvingAgainstBaseURL: true)
        components?.scheme = "todayWidgetScheme"

        if let todayWidgetUrl = components?.url {
            extensionContext?.open(todayWidgetUrl)
        }
    }
}

Finally, in AppDelegate.swift , when iOS asks the application to handle the universal link, set the original url scheme 最后,在AppDelegate.swift ,当iOS要求应用程序处理通用链接时,请设置原始url方案

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    if url.scheme == "todayWidgetScheme" {
        var components = URLComponents(url: url, resolvingAgainstBaseURL: true)
        components?.scheme = "https"

        if let todayWidgetUrl = components?.url {
            // do your thing
            return true
        }
    }

    return false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM