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

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.

Related

Links in Apple Mail (App in iOS) are not working

Our users have reported that the the links in our mails are not clickable in Apple's Mail App as they are just recognized as pure texts(underlined blue colored text). But links become clickable when the same mail is forwarded (in quoted text). This problem only occurs in Apple Mail App and not in any other third party mail apps like gmail etc. Any reason or fix for this? Are we missing something that Mail App needs specifically to be mentioned while creating links? Why Links become operational when the same email is forwarded? This happens on all iOS devices (tested on Ipads and Iphones)
Any additional information from our side can be provided on the same, if needed for better clarification.
Thank You
Are you sure you are adding link attribute to your mailTo: link text?
You may want to give it a try while setting mail content attributed text.
let attrText = NSMutableAttributedString.init(string: "Email me")
let mailLink = "mailTo:test#example.com"
let url = NSURL.init(string: mailLink) as NSURL?
attrText.addAttribute(NSAttributedStringKey.link, value: url ?? "", range: NSMakeRange(0, attrText.length))
let documentAttributes = [NSAttributedString.DocumentAttributeKey.documentType : NSAttributedString.DocumentType.html]
do {
let htmlData = try! attrText.data(from: NSMakeRange(0, attrText.length), documentAttributes:documentAttributes)
if let htmlString = String(data:htmlData, encoding:String.Encoding.utf8) {
let messageVC = MFMailComposeViewController()
messageVC.setMessageBody(htmlString, isHTML: true)
}
}

Swift: Replace Strings to NSTextAttachment in UITextView after loading styled RTF

In my iOS/swift project I am loading a RTF document to a UITextView with the code below. The RTF itself contains styled text like "... blah blah [ABC.png] blah blah [DEF.png] blah..." wich is loaded to the UITextView just fine.
Now, I want to replace all occurrences of [someImage.png] with an actual image as NSTextAttachment. How can I do that?
I am aware of the possibility to embed images in RTF documents, but I can not do that in this project.
if let rtfPath = Bundle.main.url(forResource: "testABC", withExtension: "rtf")
{
do
{
//load RTF to UITextView
let attributedStringWithRtf = try NSAttributedString(url: rtfPath, options: [.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil)
txtView.attributedText = attributedStringWithRtf
//find all "[ABC.png]" and replace with image
let regPattern = "\\[.*?\\]"
//now...?
}
}
Here is something that you could do.
Note: I'm not a Swift Developper, more an Objective-C one, so there may be some ugly Swift code (the try!, etc.). But it's more for the logic on using NSRegularExpression (which I used in Objective-C since it's shared in CocoaTouch)
So the main line directives:
Find where are the images placeholders.
Create a NSAttributeString/NSTextAttachment from it.
Replace the placeholder with the previous attributed string.
let regPattern = "\\[((.*?).png)\\]"
let regex = try! NSRegularExpression.init(pattern: regPattern, options: [])
let matches = regex.matches(in: attributedStringWithRtf.string, options: [], range: NSMakeRange(0, attributedStringWithRtf.length))
for aMatch in matches.reversed()
{
let allRangeToReplace = attributedStringWithRtf.attributedSubstring(from: aMatch.range(at: 0)).string
let imageNameWithExtension = attributedStringWithRtf.attributedSubstring(from: aMatch.range(at: 1)).string
let imageNameWithoutExtension = attributedStringWithRtf.attributedSubstring(from: aMatch.range(at: 2)).string
print("allRangeToReplace: \(allRangeToReplace)")
print("imageNameWithExtension: \(imageNameWithExtension)")
print("imageNameWithoutExtension: \(imageNameWithoutExtension)")
//Create your NSAttributedString with NSTextAttachment here
let myImageAttribute = ...
attributedStringWithRtf.replaceCharacters(in: imageNameRange, with: myImageAttributeString)
}
So what's the idea?
I used a modify pattern. I hard-wrote "png", but you could change it. I added some () to get easily the interesting parts. I thought that you may wanted to retrieve the name of the image, with or without the .png, that's why I got all theses print(). Maybe because you saved it in your app, etc. If you need to add the extension as a group, you may want to add it into parenthesis in your regPattern and check what aMatch.range(at: ??) to call. for using Bundle.main.url(forResource: imageName, withExtension: imageExtension)
I used the matches.reversed() because if you modify the length of the "match" with a replacement of different length, the previous ranges will be off. So starting from the end could do the trick.
Some code to transform a UIImage into NSAttributedString through NSTextAttachment: How to add images as text attachment in Swift using nsattributedstring

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

Getting components of NSURL as phone number

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
}

Using NSDataDetector to just like Apple's Notes app

I'm trying to find several different data types including Dates, Addresses, Phone numbers, and Links. I'm already able to find them but I want to be able to format them by underlining and changing their color. This is my code so far.
func detectData() {
let text = self.textView.text
let types: NSTextCheckingType = .Date | .Address | .PhoneNumber | .Link
var error: NSError?
let detector = NSDataDetector(types: types.rawValue, error: &error)
var dataMatches: NSArray = [detector!.matchesInString(text, options: nil, range: NSMakeRange(0, (text as NSString).length))]
for match in dataMatches {
I was thinking I should first get each result out of the loop then
1) turn them into strings 2)format them.
First question. How will I put my formatted string back into my UITextView at the same place?
Second question. I'm thinking about creating a switch like so
switch match {
case match == NSTextCheckingType.date
but now that I have a specific type of NSTextCheckingType, what do I have to do to make them have the functionality I want? (e.g. call a phone number, open up maps for an address, create a event for a date)
To do what Notes does you just need to set the dataDetectorTypes property on your text view. That's all! No NSDataDetector involved.

Resources