如何在 swift 5、SwiftUI 中的 Text() 中使用 mailto 链接

lan*_*319 1 mailto swift swift5 swiftui

Stackoverflow 上有几十个关于 Swift 5 中 mailto 链接的答案。

共识看起来像这样

let url = NSURL(string: "mailto:jon.doe@mail.com")
UIApplication.sharedApplication().openURL(url)
Run Code Online (Sandbox Code Playgroud)

但我该如何实际使用这段代码呢?最好是在警报中,但至少是一般的文本元素

import SwiftUI

struct MainMenu: View {
    @State private var showAlert = false
    
    // What do I put here
    var emailLink:UIApplication {
        let url = URL(string: "mailto:jon.doe@mail.com")!
        return UIApplication.shared.openURL(url)
    }
    
    // To make it work in here
    var body: some View {
        HStack(alignment: .center, spacing: .zero, content: {
            Text(
                "Here is an email link \(emailLink)"
            )
            
            Button(action: {
                showAlert = true
            }) {
                Text("MENU")
            }
            .alert(isPresented: $showAlert, content: {
                Alert(
                    title: Text("Title Here"),
                    message: Text(
                        "Need Help, \(emailLink)"
                    )
                )
            })
        })
    }
}

Run Code Online (Sandbox Code Playgroud)

lan*_*319 5

没有警报,根据乔治的暗示性评论,这是有效的:

var body: some View {
    Text(
        "Here is an email link "
    )
            
    Link("jon.doe@mail.com", destination: URL(string: "mailto:jon.doe@mail.com")!)
Run Code Online (Sandbox Code Playgroud)