我想在SwiftUI文本(或任何视图)中显示一个电话号码,然后使其可单击以便打开“电话”。
有没有办法用SwiftUI做到这一点,还是我应该尝试将UITextView包装在SwiftUI中,并用NSAttributed字符串等做旧方式?
我已经阅读了SwiftUI中有关Text的文档,但找不到有关如何执行此操作的任何信息。当前正在Xcode 11 beta 5中尝试这样做。
我已经在SwiftUI.h的SwiftUI API中搜索了“文本”
我还用“使电话号码/网址可轻敲”,“可链接的链接/网址swiftUI”等查询搜索stackoverflow [swiftui]和google。
Text("123-456-7890")
.onTapGesture {
// do something here
}
(文字为日语电话号码)
答案 0 :(得分:8)
亲吻答案:
Button("url") {UIApplication.shared.open(URL(string: "https://google.com")!)}
答案 1 :(得分:5)
使用iOS 14 / Xcode 12.0 beta 5
在SwiftUI中为电话和电子邮件链接使用新的链接功能。
// Link that will open Safari on iOS devices
Link("Apple", destination: URL(string: "https://www.apple.com")!)
// Clickable telphone number
Link("(800)555-1212", destination: URL(string: "tel:8005551212")!)
// Clickable Email Address
Link("apple@me.com", destination: URL(string: "mailto:apple@me.com")!)
答案 2 :(得分:4)
感谢Ashish的回答,我找到了解决此问题所需的必要代码:
在按钮的内部动作中-您需要调用此方法:
UIApplication.shared.open(url)
实际拨打电话/在SwiftUI视图中打开链接。
当然,我一开始不理解如何设置电话号码的格式,这些答案是我发现的:
How to use openURL for making a phone call in Swift?
别忘了在字符串的开头添加“ tel://” /将其设置为URL。
起作用的完整代码(目前很脏……)
Button(action: {
// validation of phone number not included
let dash = CharacterSet(charactersIn: "-")
let cleanString =
hotel.phoneNumber!.trimmingCharacters(in: dash)
let tel = "tel://"
var formattedString = tel + cleanString
let url: NSURL = URL(string: formattedString)! as NSURL
UIApplication.shared.open(url as URL)
}) {
Text(verbatim: hotel.phoneNumber!)
}
答案 3 :(得分:4)
从iOS 14开始,Apple默认为我们提供链接视图。所以,您可以使用它,
Link("Anyone can learn Swift", destination: URL(string: "https://ohmyswift.com")!)
对于以前的iOS版本,例如iOS 13.0,您仍然必须使用
Button("Anyone can learn Swift") {
UIApplication.shared.open(URL(string: "https://ohmyswift.com")!)
}
答案 4 :(得分:2)
通过操作将其设置为// Checking if the request contains body, usually will be null wiht HTTP GET and DELETE
if (request.Content != null)
{
byte[] content = await request.Content.ReadAsByteArrayAsync();
MD5 md5 = MD5.Create();
// Hashing the request body, any change in request body will result in different hash, we'll ensure message integrity
byte[] requestContentHash = md5.ComputeHash(content);
requestContentBase64String = Convert.ToBase64String(requestContentHash);
}
// Creating the raw signature string
string signatureRawData = String.Format("{0}{1}{2}{3}{4}{5}", APPId, requestHttpMethod, requestUri, requestTimeStamp, nonce, requestContentBase64String);
var secretKeyByteArray = Convert.FromBase64String(APIKey);
byte[] signature = Encoding.UTF8.GetBytes(signatureRawData);
using (HMACSHA256 hmac = new HMACSHA256(secretKeyByteArray))
{
byte[] signatureBytes = hmac.ComputeHash(signature);
string requestSignatureBase64String = Convert.ToBase64String(signatureBytes);
// Setting the values in the Authorization header using custom scheme (amx)
request.Headers.Authorization = new AuthenticationHeaderValue("amx", string.Format("{0}:{1}:{2}:{3}", APPId, requestSignatureBase64String, nonce, requestTimeStamp));
}
response = await base.SendAsync(request, cancellationToken);
,而不是将其设置为Button()
。
答案 5 :(得分:2)
尝试一下,
let strNumber = "123-456-7890"
Button(action: {
let tel = "tel://"
let formattedString = tel + strNumber
let url = URL(string: formattedString)!
UIApplication.shared.open(url)
}) {
Text("123-456-7890")
}
答案 6 :(得分:1)
Button {
var cleanNum = selectedItem.phoneNum
let charsToRemove: Set<Character> = [" ", "(", ")", "-"] // "+" can stay
cleanNum.removeAll(where: { charsToRemove.contains($0) })
guard let phoneURL = URL(string: "tel://\(cleanNum)") else { return }
UIApplication.shared.open(phoneURL, options: [:], completionHandler: nil)
} label: {
// ...
}
答案 7 :(得分:1)
利用支持链接的 SwiftUI 中的 Markdown!
struct ContentView: View {
var body: some View {
Text("Call [123-456-7890](tel:1234567890)")
}
}
结果:
答案 8 :(得分:0)
使用 iOS 14/Xcode 12.5
如果你想使用视图,我会添加这个。
undefined
Link(destination: URL(string: "tel:1234567890")!, label: {
SomeView()
})