Wywołanie numeru telefonu w swift
Próbuję zadzwonić na numer nie za pomocą konkretnych numerów, ale numer, który jest wywoływany w zmiennej lub przynajmniej powiedzieć mu, aby wyciągnąć numer w telefonie. Ten numer, który jest wywoływany w zmiennej jest numer, który pobrałem za pomocą parsera lub chwytając z SQL witryny. Zrobiłem przycisk próbując zadzwonić na numer telefonu przechowywany w zmiennej z funkcją, ale bezskutecznie. Wszystko pomoże dzięki!
func callSellerPressed (sender: UIButton!){
//(This is calls a specific number)UIApplication.sharedApplication().openURL(NSURL(string: "tel://######")!)
// This is the code I'm using but its not working
UIApplication.sharedApplication().openURL(NSURL(scheme: NSString(), host: "tel://", path: busPhone)!)
}
17 answers
Po prostu spróbuj:
if let url = NSURL(string: "tel://\(busPhone)") where UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.sharedApplication().openURL(url)
}
Zakładając, że numer telefonu jest w busPhone
.
NSURL
'S init(string:)
zwraca opcjonalne, więc używając if let
upewniamy się, że url
jest NSURL
(a nie NSURL?
jako zwracane przez init
).
Dla Swift 3:
if let url = URL(string: "tel://\(busPhone)"), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
Musimy sprawdzić, czy jesteśmy na iOS 10 czy później, ponieważ:]}
OpenURL został wycofany w systemie iOS 10.0.]}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-08 23:53:31
Samodzielne rozwiązanie w iOS 10, Swift 3 :
private func callNumber(phoneNumber:String) {
if let phoneCallURL = URL(string: "tel://\(phoneNumber)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)) {
application.open(phoneCallURL, options: [:], completionHandler: nil)
}
}
}
Powinieneś być w stanie użyć callNumber("7178881234")
, aby zadzwonić.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-17 20:10:06
/ Align = "center" bgcolor = "# e0ffe0 " / cesarz chin / / align = center / Ponadto umieściłem miły mały system ostrzegania na wypadek, gdyby numer telefonu nie był prawidłowy. Moim problemem było to, że dzwoniłem dobrze, ale numer miał spacje i niechciane znaki, takie jak ("123 456-7890"). UIApplication działa lub akceptuje tylko wtedy, gdy twój numer jest ("1234567890"). Więc zasadniczo usuwasz spację i nieprawidłowe znaki, tworząc nową zmienną, aby wyciągnąć tylko liczby. Następnie dzwoni na te numery za pomocą aplikacji UIApplication.
func callSellerPressed (sender: UIButton!){
var newPhone = ""
for (var i = 0; i < countElements(busPhone); i++){
var current:Int = i
switch (busPhone[i]){
case "0","1","2","3","4","5","6","7","8","9" : newPhone = newPhone + String(busPhone[i])
default : println("Removed invalid character.")
}
}
if (busPhone.utf16Count > 1){
UIApplication.sharedApplication().openURL(NSURL(string: "tel://" + newPhone)!)
}
else{
let alert = UIAlertView()
alert.title = "Sorry!"
alert.message = "Phone number is not available for this business"
alert.addButtonWithTitle("Ok")
alert.show()
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-17 21:03:58
Swift 3.0 i ios 10 lub starszy
func phone(phoneNum: String) {
if let url = URL(string: "tel://\(phoneNum)") {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url as URL)
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-23 09:28:43
Powyższe odpowiedzi są częściowo poprawne, ale z "tel://" jest tylko jeden problem. Po zakończeniu połączenia powróci do ekranu głównego, a nie do naszej aplikacji. Więc lepiej użyć " telprompt://", powróci do aplikacji.
var url:NSURL = NSURL(string: "telprompt://1234567891")!
UIApplication.sharedApplication().openURL(url)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-24 10:22:10
Używam tej metody w mojej aplikacji i działa dobrze. Mam nadzieję, że to też ci pomoże.
func makeCall(phone: String) {
let formatedNumber = phone.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
let phoneUrl = "tel://\(formatedNumber)"
let url:NSURL = NSURL(string: phoneUrl)!
UIApplication.sharedApplication().openURL(url)
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-27 05:07:19
Swift 3, IOS 10
func call(phoneNumber:String) {
let cleanPhoneNumber = phoneNumber.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
let urlString:String = "tel://\(cleanPhoneNumber)"
if let phoneCallURL = URL(string: urlString) {
if (UIApplication.shared.canOpenURL(phoneCallURL)) {
UIApplication.shared.open(phoneCallURL, options: [:], completionHandler: nil)
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-08 23:23:17
W Swift 3,
if let url = URL(string:"tel://\(phoneNumber)"), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-07 04:52:47
To jest aktualizacja do odpowiedzi @ Tom za pomocą Swift 2.0 Uwaga - to jest cała klasa CallComposer, której używam.
class CallComposer: NSObject {
var editedPhoneNumber = ""
func call(phoneNumber: String) -> Bool {
if phoneNumber != "" {
for i in number.characters {
switch (i){
case "0","1","2","3","4","5","6","7","8","9" : editedPhoneNumber = editedPhoneNumber + String(i)
default : print("Removed invalid character.")
}
}
let phone = "tel://" + editedPhoneNumber
let url = NSURL(string: phone)
if let url = url {
UIApplication.sharedApplication().openURL(url)
} else {
print("There was an error")
}
} else {
return false
}
return true
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-01 02:54:15
Używam rozwiązania swift 3 z weryfikacją numeru
var validPhoneNumber = ""
phoneNumber.characters.forEach {(character) in
switch character {
case "0"..."9":
validPhoneNumber.characters.append(character)
default:
break
}
}
if UIApplication.shared.canOpenURL(URL(string: "tel://\(validNumber)")!){
UIApplication.shared.openURL(URL(string: "tel://\(validNumber)")!)
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-24 07:42:56
OpenURL() został wycofany w systemie iOS 10. Oto nowa składnia:
if let url = URL(string: "tel://\(busPhone)") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-01 20:01:52
Dla swift 3.0
if let url = URL(string: "tel://\(number)"), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
else {
print("Your device doesn't support this feature.")
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-13 11:17:07
Swift 3.0 rozwiązanie:
let formatedNumber = phone.components(separatedBy: NSCharacterSet.decimalDigits.inverted).joined(separator: "")
print("calling \(formatedNumber)")
let phoneUrl = "tel://\(formatedNumber)"
let url:URL = URL(string: phoneUrl)!
UIApplication.shared.openURL(url)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-04 10:09:47
Oto alternatywny sposób na zmniejszenie numeru telefonu do ważnych komponentów za pomocą Scanner
...
let number = "+123 456-7890"
let scanner = Scanner(string: number)
let validCharacters = CharacterSet.decimalDigits
let startCharacters = validCharacters.union(CharacterSet(charactersIn: "+#"))
var digits: NSString?
var validNumber = ""
while !scanner.isAtEnd {
if scanner.scanLocation == 0 {
scanner.scanCharacters(from: startCharacters, into: &digits)
} else {
scanner.scanCharacters(from: validCharacters, into: &digits)
}
scanner.scanUpToCharacters(from: validCharacters, into: nil)
if let digits = digits as? String {
validNumber.append(digits)
}
}
print(validNumber)
// +1234567890
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-24 15:54:12
Swift 3.0 & iOS 10+
UIApplication.shared.openURL(url)
została zmieniona na
UIApplication.shared.open(_ url: URL, options:[:], completionHandler completion: nil)
options i completion handler są opcjonalne, rendering:
UIApplication.shared.open(url)
Https://developer.apple.com/reference/uikit/uiapplication/1648685-open
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-28 21:40:11
Dla podejścia Swift 3.1 i wstecznie kompatybilnego, zrób to:
@IBAction func phoneNumberButtonTouched(_ sender: Any) {
if let number = place?.phoneNumber {
makeCall(phoneNumber: number)
}
}
func makeCall(phoneNumber: String) {
let formattedNumber = phoneNumber.components(separatedBy:
NSCharacterSet.decimalDigits.inverted).joined(separator: "")
let phoneUrl = "tel://\(formattedNumber)"
let url:NSURL = NSURL(string: phoneUrl)!
if #available(iOS 10, *) {
UIApplication.shared.open(url as URL, options: [:], completionHandler:
nil)
} else {
UIApplication.shared.openURL(url as URL)
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-19 05:15:03
Jeśli Twój numer telefonu zawiera spacje, usuń je najpierw! Następnie możesz użyć zaakceptowanego rozwiązania.
let numbersOnly = busPhone.replacingOccurrences(of: " ", with: "")
if let url = URL(string: "tel://\(numbersOnly)"), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-14 13:56:16