Hey guys, I'm attempting to associate my app with ...
# compose-desktop
j
Hey guys, I'm attempting to associate my app with deep links. Is there a well-documented and working way to achieve this? I've tried adding it to the registry to associate links like myApp://link which I could call on my website, however, I'm unable to make it work.
o
I made it work on macOS using plist, no clue for Windows and Linux (no machine available to test, quick search on Windows gave me no solution)
Copy code
macOS {
      infoPlist {
          extraKeysRawXml = """
  <key>CFBundleURLTypes</key>
  <array>
    <dict>
      <key>CFBundleURLSchemes</key>
      <array>
        <string>myApp</string>
      </array>
    </dict>
  </array>
"""
      }
  }
To consume it on app side
Copy code
try {                                
        Desktop.getDesktop().setOpenURIHandler { event ->                                                                
            val url = Url(event.uri)
...
            }                                                                                                            
        }                                                                                                                
    } catch (e: UnsupportedOperationException) {                                                                         
        println("setOpenURIHandler is unsupported")
    }
there is also a way to deal with deep link using Compose navigation, but never tried myself
Notice that in
2.9.0-alpha02
, there will be
NavController.handleDeepLink(request: NavDeepLinkRequest)
j
MacOS works simply by having an associated domain, the same way Android and iOS work. I spent a night at it and found a working solution by adding new values to the Windows Registry. So for anyone who's also looking for a solution to open deep links on Windows, this should do it. This is however not the first it was mentioned in this Slack channel, so I'll just note what was noted there: you also need to remove the registries when the app is being uninstalled. For this, a better solution would be to use Conveyor which supports Jetpack Compose.
Copy code
val commands = listOf(
    """reg add "HKCU\Software\Classes\yourApp" /ve /d "Description here" /f""",
    """reg add "HKCU\Software\Classes\yourApp" /v "URL Protocol" /f""",
    """reg add "HKCU\Software\Classes\yourApp\shell" /f""",
    """reg add "HKCU\Software\Classes\yourApp\shell\open" /f""",
    """reg add "HKCU\Software\Classes\yourApp\shell\open\command" /ve /d "\"$exePath\" \"%1\"" /f"""
)

for (cmd in commands) {
    val process = Runtime.getRuntime().exec(cmd)
    process.waitFor()
    if (process.exitValue() != 0) {
        println("Command failed: $cmd")
        process.errorStream.bufferedReader()
            .use { it.lines().forEach { line -> println(line) } }
    } else {
        println("Command succeeded: $cmd")
    }

fun main(args: Array<String>) = application {
    //handle args here
}
Calling then yourApp://path from the website successfully executes the yourApp.exe. I couldn't find a better solution.
I did test it on my machine multiple times and it works. It doesn't however work for debugging, only a released msi as there is no .exe.
m
Right, with Conveyor you'd just add
app.url-schemes = myapp
to the config.