搜索兴趣点并拨打 phone 号码

Searching for & calling a phone number for a point of interest

我创建了一段代码,用于搜索 phone 与放置在我的应用程序地图上的图钉关联的数字:

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)
            }


    }
    }
}

然而,这段代码 return 有 10 个 phone 号码,并要求调用这 10 个号码中的每一个号码。我希望代码 return 一个 phone 与该兴趣点关联的号码,请我拨打它。

考虑添加 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
}

所以,一次电话phone没有。找到后,会发出一个警告,称该号码为 no。将显示并且 break 语句将中断循环,其余 phone 个数字将不会显示警报。