I want to have text in my app that looks something like this:
Zoom in to the map, or use your location.
… so one sentence, with three words linking to a pop-up window requesting access to use the user’s location.
If I wanted two separate lines, I could do something like this:
VStack{ Text(“Zoom in on the map, or”) Button(“use your location”) { print(“do the thing”) } }
… but that puts the link part on a separate line.
Also it’s a lot more of a pain for translation.
If I wanted to link to a URL in Safari, I could just use Markdown.
Text("Zoom in to the map, or [use your location](https://whatever.com)")
But I don’t want to link to a webpage. I want to ‘link’ to a Swift function to ask the user for permission to use their location.
But I can get around that by using an OpenURLAction, thus:
Text(“Zoom in to the map, or use your location.”) .environment(.openURL, OpenURLAction(handler: { url in switch url.absoluteString { case “use_location”: { print(“use the location”) return .handled }() default: { print(“Should never happen”) return .discarded }() } }))
It’s not 100% perfect, as the link doesn’t work in the Preview. But it works just fine in the emulator (and, presumably, on the device).
(thanks to this post for the useful info.)
UPDATE - maybe change the words to:
Zoom in to the map, or go to your location.
‘Go to’ means we can use the same words when we have location permission but just zoomed out too far.
… and if we use a OpenURLAction, then the link text does NOT behave like a button. It doesn’t press and respond to touch. So … maybe I go back to Plan A and just have two lines, one regular Text
and one Button
in a VStack
.