gpt4 book ai didi

ios - 制作调用搜索位置的电话号码的调用按钮

转载 作者:可可西里 更新时间:2023-11-01 02:16:13 25 4
gpt4 key购买 nike

我正在搜索位置的注释标注上创建一个 UI 按钮。我想要按钮调用位置/公司的电话号码。

这是我的代码:

func doCall()
{
if let selectedPin = selectedPin
{
let mapItem = MKMapItem(placemark: selectedPin)
if mapItem.phoneNumber != nil
{
let url: NSURL! = NSURL(string: "telprompt://" + (mapItem.phoneNumber!))
UIApplication.sharedApplication().openURL(url!)
}
else
{
print("Cant Call")
print(mapItem.phoneNumber)

}

}
}

总是返回“Cant Call”

我可以访问通过此代码搜索到的位置的电话号码:

extension LocationSearchTable : UISearchResultsUpdating {

func updateSearchResultsForSearchController(searchController: UISearchController) {
guard let mapView = mapView,
let searchBarText = searchController.searchBar.text else { return }
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchBarText
// Change line above to find nearby places
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler { response, _ in
if let items = response?.mapItems
{
for item in items
{
if let coordinates = item.placemark.location?.coordinate
{
let annotation = MKPointAnnotation()
annotation.coordinate = coordinates
annotation.title = item.name
annotation.subtitle = item.phoneNumber
self.mapView!.addAnnotation(annotation)
}
}
}
guard let response = response else {
return
}


self.matchingItems = response.mapItems

self.tableView.reloadData()
}
}

如何将上面的代码直接嵌入到我的 doCall 方法中以使 UI 按钮继续调用?

如有任何帮助,我们将不胜感激...谢谢!

最佳答案

您看到的问题是 open(url:options:) (3.x) 或 openURL (2.2 ) 无法处理包含空格、( )、- 以及您将在 mapItem 中找到的其他字符的电话号码。

Swift 3.x 的更新答案:

func doCall() {
if let selectedPin = selectedPin {
let mapItem = MKMapItem(placemark: selectedPin)
if let phoneNumber = mapItem.phoneNumber?.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: ""),
let url = URL(string: "telprompt://" + phoneNumber) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
print("Unable to call because number is nil")
}
}
}

Swift 2.2 的原始答案:

func doCall() {
if let selectedPin = selectedPin {
let mapItem = MKMapItem(placemark: selectedPin)
if let phoneNumber = (mapItem?.phoneNumber?.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet))?.joinWithSeparator("") {
UIApplication.sharedApplication().openURL(NSURL(string: "telprompt://" + phoneNumber)!)
} else {
print("Unable to call because number is nil")
}
}
}

关于ios - 制作调用搜索位置的电话号码的调用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38315655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com