Ryan
Ryan

Reputation: 109

How to Send URL in Text Message with MFMessageComposeViewController?

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: enter image description here

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?

Upvotes: 2

Views: 2178

Answers (1)

Rahul Iyer
Rahul Iyer

Reputation: 21015

It is automatic, and depends on meta data on the webpage of url in question.

Messages in iOS and macOS will automatically generate inline previews for links people send. By default these render as gray bubbles showing the page title, domain, and small icon. By adding a small amount of Open Graph metadata on your website pages, you can make these iMessage link previews look great by displaying images and meaningful captions.

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:

Link preview for http://www.apple.com/iphone includes metadata for an image and title

The webpage itself includes metadata for an image and title.

<meta property="og:title" content="iPhone" />
<meta property="og:image" content="https://www.apple.com/v/iphone/home/t/images/home/og.png?201610171354" />

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:

  1. Request the administrator of the webpage to add the metadata
  2. Link to an intermediate webpage which you do control, in which you can add metadata, and then redirect to the end page.

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.

Upvotes: -2

Related Questions