xcode 11 beta5 和 swiftui 的深层链接
Deep-linking on xcode 11 beta5 and swiftui
我想尝试一下 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>
第二个应用程序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>
当我在第一个应用程序中按下按钮时,第二个应用程序中的这个函数应该打印 url,如:"second://somethingfromthetextfield"
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
print(url)
return true
}
但它没有这样做。第二个应用程序启动但未打印任何内容。现在我想知道,我做错了什么吗?它在 Xcode beta 和 SwiftUI 上仍然可以工作还是工作方式不同?
尝试在SceneDelegate中使用如下方法:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
print("\(URLContexts.first!.url)")
}
我想尝试一下 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>
第二个应用程序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>
当我在第一个应用程序中按下按钮时,第二个应用程序中的这个函数应该打印 url,如:"second://somethingfromthetextfield"
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
print(url)
return true
}
但它没有这样做。第二个应用程序启动但未打印任何内容。现在我想知道,我做错了什么吗?它在 Xcode beta 和 SwiftUI 上仍然可以工作还是工作方式不同?
尝试在SceneDelegate中使用如下方法:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
print("\(URLContexts.first!.url)")
}