簡體   English   中英

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

當我在第一個應用程序中按下按鈕時,第二個應用程序中的此功能應打印如下網址:“ 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)")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM