Currently I'm developing an email app and want all links with mailto scheme to be opened via my app, not default Apple's Mail app.
For instance, I have a link like this one
<a href="mailto:email@example.com\>mailto_test</a>
in UIWebView or UITextView (doesn't matter which one, they have similar behavior).
When I longpress this link iOS will show UIAlertController with 3 options:
The first one option, "New Message", will open default Mail app. So my question is how to override this behavior? How can i force this option to launch my own email app?
For those who think it is impossible - take a look at iOS Gmail app. Gmail devs have implemented what I'm asking about, but I don't understand how.
3 Answers
Answers 1
In a text view, link behavior is completely up to you. Give the text view a delegate and implement textView(_:shouldInteractWith:in:interaction:)
. A long press is the .presentActions
interaction. Return false
and substitute your own response. You can put up your own .actionSheet
alert that looks just like the default one, but does what you want it to.
Answers 2
The solution is to change the default click action of the mail link to the way you want.
Make a UITextView
example:
Suppose we have UITextView
textView, its attributeString
is
<a href="mailto:email@example.com\>mailto_test</a>
and its documentType
is html. The code is below:
let textView = UITextView(frame: view.bounds) textView.frame.origin.y += 100 let attrStr = try! NSAttributedString( data: "<a href=\"mailto:email@example.com\">mailto_test</a>".data(using: .utf8, allowLossyConversion: true)!, options:[.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) textView.attributedText = attrStr view.addSubview(textView)
This the default effect that after pressed the mailto_test
text in the screen the native mail app functions waked up. So we add a delegate
to the textView
to modify the click action.
textView.isEditable = false textView.dataDetectorTypes = .link textView.delegate = self
Here is the delegate function to control the specific click action:
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { print(URL) return false }
Whenever you pressed the mail link, the functions invoked without calling the native mail app. And the URL is just the mail url.
Answers 3
You are looking for something like this, right? Where you can open your app from anywhere you click email URL.
I can't find the exact answer right now, but you want to add your app as a URL Scheme (Apple documentation)
Edit: more apple documentation
0 comments:
Post a Comment