我已经创建了一段代码,用于搜索与我的应用中地图上丢弃的相关联的电话号码:
func doCall(_ mapView : MKMapView! , didUpdateUserLocation userLocation : MKUserLocation, placemark:MKPlacemark )
{
let request = MKLocalSearchRequest()
let searchBar = resultSearchController!.searchBar
request.naturalLanguageQuery = searchBar.text
let span = MKCoordinateSpan(latitudeDelta: 0.00000000001, longitudeDelta: 0.0000000001)
let search = MKLocalSearch(request: request)
search.start {MKLocalSearchResponse, error in
for item in (MKLocalSearchResponse?.mapItems)! as [MKMapItem]
{
let optionalString = item.phoneNumber
if let unwrapped = optionalString
{
let center = MKLocalSearchResponse!.mapItems.first!.placemark.coordinate
request.region = MKCoordinateRegion(center: center, span: span)
print(unwrapped)
let cleanUrl = unwrapped.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
let url: URL = URL(string: "tel://" + (cleanUrl))!
UIApplication.shared.openURL(url)
}
}
}
}
然而,这段代码返回10个电话号码,并要求拨打其中的每一个。我希望代码返回与该兴趣点相关联的单个电话号码,并让我给它打电话。
答案 0 :(得分:1)
考虑添加一个break语句。
if cleanUrl != nil {
let cleanUrl = unwrapped.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
let url: URL = URL(string: "tel://" + (cleanUrl))!
UIApplication.shared.openURL(url)
break
}
所以,一旦电话没有。被发现,警告称不。将显示并且break语句将中断循环,并且不会显示其余电话号码的警报。