2017-05-28 55 views
0

點的電話號碼我已經創建了一段代碼,將用於與引腳相關的電話號碼搜索我的應用程序在地圖上掉落:搜索並呼籲感興趣

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個電話號碼,並要求撥打這10個電話號碼中的每一個。我希望代碼返回與該興趣點關聯的單個電話號碼,並要求我給它打電話。

回答

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語句將打破循環,並且不會顯示其他電話號碼的提示。

+0

是的!有效!非常感謝。 –

+0

不要忘記接受答案。樂於幫助。 :) –