简体   繁体   English

在xcode 11 beta5和swiftui上进行深层链接

[英]Deep-linking on xcode 11 beta5 and swiftui

I wanted to play a bit with SwiftUI and deep-linking, with passing data between two apps. 我想使用SwiftUI和深层链接,在两个应用之间传递数据。 But it seems not to work. 但这似乎行不通。

I build two small apps. 我构建了两个小应用程序。 First app with a textfield and a button, second app with a label and a button. 第一个具有文本字段和按钮的应用程序,第二个具有标签和按钮的应用程序。 When I press the button in the first app, it calls the second app via the given url scheme and vice versa. 当我在第一个应用程序中按下按钮时,它将通过给定的url方案调用第二个应用程序,反之亦然。

First App looks like that... 第一个应用看起来像...

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)
        }
    }
}

and the info.plist... 和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>

Second app info.plist 第二个应用程序info.plist

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

And when I press the button in the first app, this function in the second app should print the url like: "second://somethingfromthetextfield" 当我在第一个应用程序中按下按钮时,第二个应用程序中的此功能应打印如下网址:“ second:// somethingfromthetextfield”

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

    return true
}

But its not doing it. 但是它没有这样做。 The second app starts but didn't print anything. 第二个应用程序启动,但未打印任何内容。 Now i wondering, am I doing something wrong? 现在我想知道,我做错了什么吗? Is it working anyways on Xcode beta and SwiftUI or does it work differently? 它是否可以在Xcode beta和SwiftUI上正常工作,还是工作方式不同?

Try to use the following method in the SceneDelegate: 尝试在SceneDelegate中使用以下方法:

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

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

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