ios - Swift如何将超链接添加到按钮
问题描述
因此,当我单击按钮打开网站链接但由于某种原因无法正常工作时,我正在尝试。我看过 2020 年的视频,对于那个工作的人来说,但对我来说,它给了我错误,这里是错误的图像。
这是代码
import UIKit
class ContactViewController: UIViewController {
@IBAction func FB(_ sender: Any) {
}
@IBAction func YT(_ sender: Any) {
UIApplication.shared.open(URL(string: "www.google.com")!)
}
@IBAction func URL(_ sender: Any) {
}
@IBAction func CALL(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
我希望每个按钮都只链接一个名为具有电话号码的呼叫并直接进入电话拨号器。
编辑
解决方案
而不是强制转换使用此代码
@IBAction func YT(_ sender: Any) {
if let url = URL(string: "www.google.com") {
UIApplication.shared.open(url)
} else {
print("url is not correct")
}
}
@IBAction func CALL(_ sender: Any) {
if let url = URL(string: "telprompt://\(phoneNumber)") {
let application = UIApplication.shared
guard application.canOpenURL(url) else {
return
}
application.open(url, options: [:], completionHandler: nil)
}
}
对于电子邮件导入 MessageUI
import MessageUI
@IBAction func Email(_ sender: Any) {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["you@yoursite.com"])
mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)
present(mail, animated: true)
} else {
// show failure alert
}
}
推荐阅读
- php - Symfony 4 - 编辑实体
- python - 根据其他列和 series.index 之间的匹配创建 df 列
- c# - 如何在 ASP.NET 中添加名称中带有破折号的默认控制器?
- android - 将接收到的坐标存储到 SQLite 数据库
- jquery - 如何通过(.each循环)在jquery中获取未排序的html列表的值?
- opentk - 为什么基于 OpenTK(C#) 的纹理渲染什么也没显示
- jsf - Lazy Datatable 满载后如何调用 bean 方法?
- reactjs - Redux reducer - 它会改变状态吗?
- css - 对齐两个并排 div 内的内容
- r - if (mydata[row1 - 1, col2] == "Has") { 出现错误:需要 TRUE/FALSE 但不适用的缺失值?