Getting components of NSURL as phone number - ios

I have an NSURL being returned to me as something like
tel:555-555-5555
I can recognize it as a telephone by identifying the NSURL's scheme as "tel" using
let url = NSURL(string: "tel:555-555-5555")
if url.scheme == "tel" {
// it's a telephone number
}
But I can't find a command to get the actual phone number component. The phone numbers can come back much more messy than the example I gave, so I thought it would be much safer to ask if there was a way to get the phone number component directly instead of saving as string and clipping out the scheme.

Use resourceSpecifier:
let url = NSURL(string: "tel:555-555-5555")!
if url.scheme == "tel" {
let number = url.resourceSpecifier
}

Related

iOS swift extracting permalink from deeplink URL

I am implementing Firebase dynamic links in my iOS project. It is working fine and it is opening my iOS Home screen properly. But now I would like to extract values from url and open appropriate screen based on url.
for example:
https://www.dd.com/forums/hot-deals-online/topics/smart-tv-carniva
in this url I would like to get 'hot-deals-online' and 'smart-tv-carniva' permalink which I will pass to view controller to open that screen on the app
can someone suggest me best approach for this.
Shortest is to access path of URLComponents object:
let url = "https://www.dd.com/forums/hot-deals-online/topics/smart-tv-carniva"
if let comps = URLComponents(string: url) {
var elements = comps.path.split(separator: "/").map(String.init)
// ["forums", "hot-deals-online", "topics", "smart-tv-carniva"]
// To build an url back, example:
var url = URL(string: comps.host!)!
url.appendPathComponent(elements[0])
url.appendPathComponent(elements[1])
// Result: www.dd.com/forums/hot-deals-online
}
P.S. calling .map(String.init) makes in-place conversion from array of substrings to normal String array.

Why can't I convert this String to a URL?

I have x 2 questions about urls and webViews.
Question 1:
I have a string which I am getting from an API that is supposed to be a url. The string is https://godochurch.blob.core.windows.net/sermons/1031/30-1-2017-Vision Sunday-Devotion.mp3
When trying to convert to a url I'm getting nil.
Here is the code:
if let sermonUrl = sermonUrl {
if let url = URL(string: sermonUrl) {
let requestObj = URLRequest(url: url)
webView.loadRequest(requestObj)
}
}
I have worked out that the space between 'Vision' and 'Sunday' is the problem.
Should I be encoding the string in some way before trying to convert it to a URL? What's confusing is that if I paste the string into my browser it works just fine, but I notice the browser is percent encoding the space.
If I am supposed to be encoding the string, how do I do that?
Question 2:
I see that URL(string: "urlStringHere") is only available from iOS 10. My app needs to work for iOS 9. How can I convert the above code so it works on iOS 9 and 10.
Thanks in advance for your time.
1: escape that space with %20 or +:
if let url = URL(string: "https://godochurch.blob.core.windows.net/sermons/1031/30-1-2017-Vision%20Sunday-Devotion.mp3") {
// ...
}
2: URL was introduced with Swift 3, which is compatible with iOS 8 and later. See this question on how to make it work with older versions of iOS.
Edit: the easiest way to percent-escape a URL is to use URLComponents:
var components = URLComponents(string: "https://godochurch.blob.core.windows.net")!
components.path = "/sermons/1031/30-1-2017-Vision Sunday-Devotion.mp3"
if let url = components.url {
// ...
}
If you happen to get you URL strings from a webservice and it contains a space, that service is bad. Space is not allowed in an URL. Web browsers relax that rule because they know there are bad URLs out there. Swift won't take it.

Detect and open a Postal Address in Swift(iOS) using NSDataDetector

I currently have a UITextView with Address detection active, this detects any addresses in this field and makes them into a clickable link that opens up Apple Maps with the address as the focus. I really like this functionality but my users would rather have a button to click on instead of the inline link.
With this in mind I have researched into NSDataDetector and managed to get this functionality working for emailing and phone numbers but I am stuck on Addresses. The code I am using to detect the address is below:
let types: NSTextCheckingType = [.Address]
let detector = try! NSDataDetector(types: types.rawValue)
let matches = detector.matchesInString(input, options: [], range: NSMakeRange(0, input.characters.count))
for match in matches {
return NSURL(string: "\((input as NSString).substringWithRange(match.range))")
}
However the NSURL fails to be created, I believe the syntax must be wrong, it works for phone numbers as below:
let types: NSTextCheckingType = [.PhoneNumber]
let detector = try! NSDataDetector(types: types.rawValue)
let matches = detector.matchesInString(input, options: [], range: NSMakeRange(0, input.characters.count))
for match in matches {
return NSURL(string: "telprompt://\((input as NSString).substringWithRange(match.range))")
}
If the address is valid it returns the components of the address(street, city etc.) in the matches object. I was hoping the NSURL could be used with address just like phone numbers but I may be wrong so is there an alternate way I could use the results from the detector and display them on Apple Maps manually?
Or as a side note could I hide the UITextView and trigger the link touch programatically based on a button touch?
Any more information can be provided as needed,
thank you in advance for any help.

Choose specific url without writing a lot of if statement

I'm working on an horoscope application now I have a problem after user selected is zodiac how can I know what php url to show him for example:
there is 12 zodiac's so how can I give him his horoscope for this url for example
NSURL: "blablabla_horscope.com/libra/today_horoscope"
I mean I don't want to write in Xcode twelve options of url, and start making if statements for example "if this user and then use here is NSUserDefaults with his choosed zodiac"
since then I will have to start making a list of a lot of if statement and it will make my app work slow and it's a bad idea so I have thinking about making some kinda of php file, that will handle all that after the user registered in the background but where to start for that?
I'm currently working on Swift, core-data, php.
let urlString = "http://www.blablabla/?sign=libra&time=today"
let url = NSURL (string: urlString)
let dataURL = try? NSData(contentsOfURL: url!, options: [])
let result: String = String(data: dataURL!, encoding: NSUTF8StringEncoding)!
print("the result is %#", result)
Set your constant for the sign type prior to setting your urlString.
let sign = "libra"
or
let sign = "cancer"
etc...
and then you can:
let urlString = "http://www.blablabla/?sign=\(sign)&time=today"
Late

iOS Make a phone call and pass data

For a Proof of Concept, I'd like to be able to make a phone call to a specific phone number and pass data to the telephone number.
Is this possible?
for your first question I'd like to be able to make a phone call to a specific phone number
Ans:
YES, you can do it like
for ex
let phoneNumber = "1234567890"
if let phoneCallURL = NSURL(string: "telprompt://(phoneNumber)") {
let application = UIApplication.sharedApplication()
if application.canOpenURL(phoneCallURL) {
application.openURL(phoneCallURL)
}
else{
println("failed")
}
}
for your second question pass data to the telephone number,
Ans:
NO, it is not possible
but, you can send the data to backend on that call event, and trigger the sms to particular number

Resources