Reputation: 175
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?
Upvotes: 2
Views: 1314
Reputation: 694
Try to use the following method in the SceneDelegate:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
print("\(URLContexts.first!.url)")
}
Upvotes: 2