How can you send a url via MFMessageComposeViewController?
When you share a url from Safari, it shows a rich link preview. However, I've only been able to pass a url as plain text.
This is what I want to achieve:

This is my code that only displays a link as plain text:
import MessageUI
class testVC: UIViewController, MFMessageComposeViewControllerDelegate {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
sendText(toPhoneNumber: "1001001000", url: URL(string: "https://stackoverflow.com/questions/ask")!)
}
func sendText(toPhoneNumber: String?, url: URL) {
if MFMessageComposeViewController.canSendText() {
let messageVC = MFMessageComposeViewController()
messageVC.body = "\(url)"
if let number = toPhoneNumber {
messageVC.recipients = [number]
}
messageVC.messageComposeDelegate = self
self.present(messageVC, animated: true, completion: nil)
} else {print("Cannot send message. Error.")}
}
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
controller.dismiss(animated: true, completion: nil)
}
}
How can you send a url via text?
It is automatic, and depends on meta data on the webpage of url in question.
https://developer.apple.com/library/archive/technotes/tn2444/_index.html
For example, try the following URL in your code for testing purposes:
http://www.apple.com/iphone
You should get a preview like this:
The webpage itself includes metadata for an image and title.
The meta data above adds the title and image in the preview. Without it, you would not get a preview.
You can test this by updating the metadata of the page for which you want a preview generated.
If the webpage you are generating a preview for is not under your control, your alternatives are:
In summation: Your iOS code is not responsible for generating the preview - it is metadata on the webpage of the actual URL.
If this answer solves your problem please mark it as correct.