我如何在swift 5中的text()中使用邮件链接,swiftui

发布于 2025-01-25 06:10:11 字数 1369 浏览 3 评论 0原文

Swift 5中有数十个邮政链接的堆栈volfflow答案。

共识看起来像这样,

let url = NSURL(string: "mailto:[email protected]")
UIApplication.sharedApplication().openURL(url)

但是我如何实际使用此代码?理想情况下在警报中,但至少是一般文本元素

import SwiftUI

struct MainMenu: View {
    @State private var showAlert = false
    
    // What do I put here
    var emailLink:UIApplication {
        let url = URL(string: "mailto:[email protected]")!
        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)"
                    )
                )
            })
        })
    }
}

There are dozens of Stackoverflow answers for mailto links in Swift 5.

The consensus look like this

let url = NSURL(string: "mailto:[email protected]")
UIApplication.sharedApplication().openURL(url)

But how do I actually use this code? Ideally in an alert, but at least a general Text element

import SwiftUI

struct MainMenu: View {
    @State private var showAlert = false
    
    // What do I put here
    var emailLink:UIApplication {
        let url = URL(string: "mailto:[email protected]")!
        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)"
                    )
                )
            })
        })
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏尔 2025-02-01 06:10:11

没有警报,基于

var body: some View {
    Text(
        "Here is an email link "
    )
            
    Link("[email protected]", destination: URL(string: "mailto:[email protected]")!)

Without an Alert, based on the suggestive comment of George this works:

var body: some View {
    Text(
        "Here is an email link "
    )
            
    Link("[email protected]", destination: URL(string: "mailto:[email protected]")!)
请远离我 2025-02-01 06:10:11

您可以使用环境 var openUrl为此,documentation 在这里

struct ContentView: View {
    @Environment(\.openURL) var openURL
    @State var alert: Bool = false
    var body: some View {
        HStack {
            Button(action: {
                alert.toggle()
            }, label: {
                Label("Email", systemImage: "envelope.fill")
            })
        }
        .alert("Contact", isPresented: $alert, actions: {
            Button(action: {
                mailto("[email protected]")
            }, label: {
                Label("Email", systemImage: "envelope.fill")
            })
            Button("Cancel", role: .cancel, action: {})
        })
    }
    
    func mailto(_ email: String) {
        let mailto = "mailto:\(email)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        print(mailto ?? "")
        if let url = URL(string: mailto!) {
            openURL(url)
        }
    }
}

You can use Environment var openURL for this, documentation here.

struct ContentView: View {
    @Environment(\.openURL) var openURL
    @State var alert: Bool = false
    var body: some View {
        HStack {
            Button(action: {
                alert.toggle()
            }, label: {
                Label("Email", systemImage: "envelope.fill")
            })
        }
        .alert("Contact", isPresented: $alert, actions: {
            Button(action: {
                mailto("[email protected]")
            }, label: {
                Label("Email", systemImage: "envelope.fill")
            })
            Button("Cancel", role: .cancel, action: {})
        })
    }
    
    func mailto(_ email: String) {
        let mailto = "mailto:\(email)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        print(mailto ?? "")
        if let url = URL(string: mailto!) {
            openURL(url)
        }
    }
}

alert

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文