HowTo —— Use Link or openURL to open URL scheme in SwiftUI2.0

Published on

Get weekly handpicked updates on Swift and SwiftUI!

SwiftUI 2.0 provides native support for opening URL schemes, which makes it very convenient to call other apps in code.

Similar to NavigationLink, open the URL scheme directly to the corresponding app



In SwiftUI 2.0, Apple provided some methods to invoke system operations by injecting them through Environment, such as exportFiles, importFiles, openURL, etc.

@Environment(\.openURL) var openURL

Code Example

struct URLTest: View {
    @Environment(\.openURL) var openURL
    let safariUrl = URL(string:"")!
    let mailUrl = URL(string:"!")!
    let phoneURl = URL(string:"tel:12345678")!
    var body: some View {
            Link("Use Safari to open web page",destination:safariUrl)
            Button("Send email"){
                openURL.callAsFunction(mailUrl){ result in
            Link(destination: phoneURl){
                Label("Make a phone call",systemImage:"")

The simulator only supports very few URL schemes, it’s best to test on a real device

Some URL schemes provided by Apple

Explore weekly Swift highlights with developers worldwide

Buy me a if you found this article helpful