Programing

Swift를 사용하여 iOS 10에서 전화를 거는 방법은 무엇입니까?

lottogame 2020. 11. 16. 07:45
반응형

Swift를 사용하여 iOS 10에서 전화를 거는 방법은 무엇입니까?


이 질문에 이미 답변이 있습니다.

버튼을 클릭하면 내 앱이 특정 번호로 전화를 걸 수 있기를 바랍니다. 나는 그것을 google 시도했지만 지금까지 iOS 10에 대한 것이없는 것 같습니다 (openURL이 사라진 곳). 누군가 그렇게하는 방법에 대한 예를 들어 줄 수 있습니까? 예를 들면 다음과 같습니다.

@IBAction func callPoliceButton(_ sender: UIButton) {
    // Call the local Police department
}

다음과 같이 호출 할 수 있습니다.

 if let url = URL(string: "tel://\(number)") {
                UIApplication.shared.openURL(url)
            }

들어 스위프트 3+ , 당신처럼 사용할 수 있습니다

guard let number = URL(string: "tel://" + number) else { return }
UIApplication.shared.open(number)

OR

UIApplication.shared.open(number, options: [:], completionHandler: nil)

당신이 전화 번호 문자열을 문질러했습니다 확인하는 것은의 인스턴스 제거하는 (, ), -, 또는 space.


직무

전화 번호 확인으로 전화 걸기

세부

테스트 대상 :

  • Xcode 9.2, Swift 4
  • Xcode 10.2 (10E125), Xcode 11.0 (11A420a), Swift 5

해결책

extension String {

    enum RegularExpressions: String {
        case phone = "^\\s*(?:\\+?(\\d{1,3}))?([-. (]*(\\d{3})[-. )]*)?((\\d{3})[-. ]*(\\d{2,4})(?:[-.x ]*(\\d+))?)\\s*$"
    }

    func isValid(regex: RegularExpressions) -> Bool { return isValid(regex: regex.rawValue) }
    func isValid(regex: String) -> Bool { return range(of: regex, options: .regularExpression) != nil }

    func onlyDigits() -> String {
        let filtredUnicodeScalars = unicodeScalars.filter { CharacterSet.decimalDigits.contains($0) }
        return String(String.UnicodeScalarView(filtredUnicodeScalars))
    }

    func makeAColl() {
        guard   isValid(regex: .phone),
                let url = URL(string: "tel://\(self.onlyDigits())"),
                UIApplication.shared.canOpenURL(url) else { return }
        if #available(iOS 10, *) {
            UIApplication.shared.open(url)
        } else {
            UIApplication.shared.openURL(url)
        }
    }
}

용법

"+1-(800)-123-4567".makeAColl()

"phone:+1(617)111-22-33!".makeAColl() // Will extract "+1(617)111-22-33" and make a call

let text = "blabla, +1(222)333-44-55, dasdwedsczx acscas 123-89-01"
let phones = text.extractAll(type: .phoneNumber).compactMap { $0.phoneNumber }
print(phones)

전체 샘플

func test() {
    isPhone("blabla")
    isPhone("+1(222)333-44-55")
    isPhone("+42 555.123.4567")
    isPhone("+1-(800)-123-4567")
    isPhone("+7 555 1234567")
    isPhone("+7(926)1234567")
    isPhone("(926) 1234567")
    isPhone("+79261234567")
    isPhone("926 1234567")
    isPhone("9261234567")
    isPhone("1234567")
    isPhone("123-4567")
    isPhone("123-89-01")
    isPhone("495 1234567")
    isPhone("469 123 45 67")
    isPhone("8 (926) 1234567")
    isPhone("89261234567")
    isPhone("926.123.4567")
    isPhone("415-555-1234")
    isPhone("650-555-2345")
    isPhone("(416)555-3456")
    isPhone("202 555 4567")
    isPhone("4035555678")
    isPhone(" 1 416 555 9292")
    isPhone("+44 1838 300284")
    isPhone("+44 1838 300284, 1 416 555 9292")
}

private func isPhone(_ string: String) {
    let result = string.to(type: .phoneNumber) != nil
    print("\(result ? "✅" : "❌") \(string) | \(string.onlyDigits()) | \(result ? "[a phone number]" : "[not a phone number]")")
}

결과

❌ blabla |  | [not a phone number]
✅ +1(222)333-44-55 | 12223334455 | [a phone number]
✅ +42 555.123.4567 | 425551234567 | [a phone number]
✅ +1-(800)-123-4567 | 18001234567 | [a phone number]
✅ +7 555 1234567 | 75551234567 | [a phone number]
✅ +7(926)1234567 | 79261234567 | [a phone number]
✅ (926) 1234567 | 9261234567 | [a phone number]
✅ +79261234567 | 79261234567 | [a phone number]
✅ 926 1234567 | 9261234567 | [a phone number]
✅ 9261234567 | 9261234567 | [a phone number]
✅ 1234567 | 1234567 | [a phone number]
✅ 123-4567 | 1234567 | [a phone number]
✅ 123-89-01 | 1238901 | [a phone number]
✅ 495 1234567 | 4951234567 | [a phone number]
✅ 469 123 45 67 | 4691234567 | [a phone number]
✅ 8 (926) 1234567 | 89261234567 | [a phone number]
✅ 89261234567 | 89261234567 | [a phone number]
✅ 926.123.4567 | 9261234567 | [a phone number]
✅ 415-555-1234 | 4155551234 | [a phone number]
✅ 650-555-2345 | 6505552345 | [a phone number]
✅ (416)555-3456 | 4165553456 | [a phone number]
✅ 202 555 4567 | 2025554567 | [a phone number]
✅ 4035555678 | 4035555678 | [a phone number]
✅  1 416 555 9292 | 14165559292 | [a phone number]
✅ +44 1838 300284 | 441838300284 | [a phone number]
ERROR: Detected several phone numbers
❌ +44 1838 300284, 1 416 555 9292 | 44183830028414165559292 | [not a phone number]
["+1(222)333-44-55", "123-89-01"]

Swift 3 용으로 업데이트되었습니다.

used below simple lines of code, if you want to make a phone call:

// function defination:

func makeAPhoneCall()  {
    let url: NSURL = URL(string: "TEL://1234567890")! as NSURL
    UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}

// function call: [Used anywhere in your code]

self.makeAPhoneCall()

Note: Please run the app on a real device because it won't work on the simulator.


In Swift 4.2

func dialNumber(number : String) {

 if let url = URL(string: "tel://\(number)"),
   UIApplication.shared.canOpenURL(url) {
      if #available(iOS 10, *) {
        UIApplication.shared.open(url, options: [:], completionHandler:nil)
       } else {
           UIApplication.shared.openURL(url)
       }
   } else {
            // add error message here 
   }
}

Call this like below

dialNumber(number: "+921111111222")

Hope this help.


if let phoneCallURL:URL = URL(string: "tel:\(strPhoneNumber)") {
        let application:UIApplication = UIApplication.shared
        if (application.canOpenURL(phoneCallURL)) {
            let alertController = UIAlertController(title: "MyApp", message: "Are you sure you want to call \n\(self.strPhoneNumber)?", preferredStyle: .alert)
            let yesPressed = UIAlertAction(title: "Yes", style: .default, handler: { (action) in
                application.openURL(phoneCallURL)
            })
            let noPressed = UIAlertAction(title: "No", style: .default, handler: { (action) in

            })
            alertController.addAction(yesPressed)
            alertController.addAction(noPressed)
            present(alertController, animated: true, completion: nil)
        }
    }

By mistake my answer was misplaced, please checkout this one: You can use this:

guard let url = URL(string: "tel://\(yourNumber)") else {
return //be safe
}

if #available(iOS 10.0, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}

We need to check whether we're on iOS 10 or later As 'openURL' was deprecated in iOS 10.0

참고URL : https://stackoverflow.com/questions/40078370/how-to-make-phone-call-in-ios-10-using-swift

반응형