【问题标题】:Deep-linking on xcode 11 beta5 and swiftuixcode 11 beta5 和 swiftui 上的深度链接
【发布时间】:2019-08-02 20:38:33
【问题描述】:

我想尝试一下 SwiftUI 和深度链接,以及在两个应用程序之间传递数据。但它似乎不起作用。

我构建了两个小应用程序。第一个带有文本字段和按钮的应用程序,第二个带有标签和按钮的应用程序。当我在第一个应用程序中按下按钮时,它会通过给定的 url 方案调用第二个应用程序,反之亦然。

第一个应用看起来像这样......

struct ContentView: View {

    @State var message: String = ""

    var body: some View {

        Group {

            TextField("Enter message...", text: $message).textFieldStyle(.roundedBorder).padding()

            Button(action: {
                // go to second app

                let application = UIApplication.shared
                let secondAppPath = "second://\(self.message)"
                let appUrl = URL(string: secondAppPath)!

                application.open(appUrl, options: [ : ], completionHandler: nil)

                }) { Text("Go to second app")}
                .padding()
                .border(Color.black, width: 2)
        }
    }
}

还有 info.plist...

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>second</string>
</array>
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>first</string>
        </array>
    </dict>
</array>

第二个应用信息.plist

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>first</string>
</array>
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>second</string>
        </array>
    </dict>
</array>

当我在第一个应用程序中按下按钮时,第二个应用程序中的此功能应打印如下网址:“second://somethingfromthetextfield”

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    print(url)

    return true
}

但它没有这样做。第二个应用程序启动但没有打印任何内容。现在我想知道,我做错了什么吗?它是在 Xcode beta 和 SwiftUI 上运行还是运行方式不同?

【问题讨论】:

标签: deep-linking swiftui xcode11


【解决方案1】:

尝试在 SceneDelegate 中使用以下方法:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    print("\(URLContexts.first!.url)")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-15
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    相关资源
    最近更新 更多