简体   繁体   中英

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. 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.

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...

<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

<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"

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?

Try to use the following method in the SceneDelegate:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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