ios - 使用 swift 调用电话不适用于某些号码

标签 ios iphone swift apple-maps

在过去的几个小时里,我一直在拼命尝试在调用此功能时用swift调用电话。

    func callPhone(phoneNumber: String){
        guard let url = URL(string:"telprompt:\(phoneNumber)") else {
             print("failed to load url, phone number: \(phoneNumber)")

             return
        }
        UIApplication.shared.open(url)
    }

由于某种原因,它不起作用,我到处都在网上进行了研究,但仍然找不到为什么这不起作用的问题。有些数字偶尔会起作用,比如如果我尝试“123456789”它会起作用。但如果我尝试不同的号码,它就不会工作; guard let 语句不起作用,url 将为空。 请帮我。非常感谢任何反馈。

编辑:所以我发现它不起作用,但我不确定如何解决它。当我从苹果 map 中获取电话号码时它不起作用,但是当我对电话号码进行硬编码时它起作用了。 这是我的完整代码:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

@IBOutlet weak var map: MKMapView!
let locationManager = CLLocationManager()
let regionMeters : Double = 10000

override func viewDidLoad() {
    super.viewDidLoad()

    map.delegate = self
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
    map.showsUserLocation = true
    if let location = locationManager.location?.coordinate {
        let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionMeters, longitudinalMeters: regionMeters)

        map.setRegion(region, animated: true)
    }
    searchLocations(search: "Restaurant")

}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let identifier = "Place"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

        if annotationView == nil {

            annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = true

            let btn = UIButton(type: .contactAdd)

            annotationView!.rightCalloutAccessoryView = btn
        } else {
            annotationView!.annotation = annotation
        }

        return annotationView
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    let subtitle = view.annotation?.subtitle as! String
    let splitted = subtitle.components(separatedBy: "+")
    let number = splitted.last as! String
    let trimmedNumber : String = number.components(separatedBy: [" ", "-", "(", ")"]).joined()
    CallOnPhone(phoneNumber: trimmedNumber)

}
func searchLocations(search : String){
    let searchRequest = MKLocalSearch.Request()
    searchRequest.naturalLanguageQuery = search
    searchRequest.region = map.region

    let activeSearch = MKLocalSearch(request: searchRequest)
    activeSearch.start { (response, err) in
        if response == nil {
            print("no request")
        } else {
            for item in (response?.mapItems)!{
                let annotation = MKPointAnnotation()
                annotation.title = item.name

                if let phone = item.phoneNumber {
                    annotation.subtitle = "Phone Number: \(phone as String)"
                }

                annotation.coordinate = item.placemark.coordinate
                self.map.addAnnotation(annotation)
            }



        }
    }
}

@objc func CallOnPhone(phoneNumber: String){
    let newStringPhone = phoneNumber.replacingOccurrences(of: " ", with: "", options: .literal, range: nil)
    print(newStringPhone)
    if newStringPhone != ""{
        if let url = URL(string: "tel://\(newStringPhone)"), UIApplication.shared.canOpenURL(url) {
            if #available(iOS 10, *) {
                UIApplication.shared.open(url)
            } else {
                UIApplication.shared.openURL(url)
            }
        }
    }

}

谢谢,

最佳答案

尝试修剪电话号码。

let myphone:String = phoneNumber.trimmingCharacters(in:.whitespacesAndNewlines)

完整代码:

if let url = URL(string: "tel://\(myphone)"),UIApplication.shared.canOpenURL(url) {
        if #available(iOS 10, *) {
            UIApplication.shared.open(url, options: [:], completionHandler:nil)
        } else {
            UIApplication.shared.openURL(url)
        }
    } else {
        Util.shared.showToast(message: "Can't make call from this device", view: self.view)
    }

关于ios - 使用 swift 调用电话不适用于某些号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53976548/

相关文章:

android - 最近更新后 react-native run-ios 失败

ios - 检查应用程序的今日小部件是否已安装

ios - 我可以过滤掉从 Apple Music 下载到设备的歌曲吗?

swift - RxSwift 4/RxCocoa 4 弃用变量——优缺点

objective-c - 使用 AVAudioEngine 播放 WAV 数据

objective-c - UITableView 滚动感觉很乱

iphone - 是否有用于 iPhone 开发的颜色选择器库/代码?

iphone - 如何在两点之间画线?

PC上的iPhone开发: Best Option?

swift - Xcode 7 如何获取文本字段输入?