HomeiOS Developmentios - Utilizing ShareLink in SwiftUI to create merchandise textual content with...

ios – Utilizing ShareLink in SwiftUI to create merchandise textual content with URL hyperlink formatted for each the Message app and e mail


My want is to make use of ShareLink to permit the consumer to ship formatted e mail or textual content message together with a URL hyperlink. My SwiftUI code seems like:

    ShareLink(
        merchandise: itemText,
        topic: Textual content("Message from app consumer"),
        preview: SharePreview("Hyperlink",picture: "AppIcon")) {
        Label(message, systemImage:  "sq..and.arrow.up")
    }

the place I am producing itemText from itemText(hyperlink: URL) (I’ve trimmed the string I am utilizing to simply the fundamentals):

    func shareItemMessage(hyperlink: URL) -> String {
        return
            "Please be a part of me on our appnn"
            + "Faucet on ((hyperlink)) to just accept my invitation (hyperlink: (hyperlink)).nn"
    }

    func itemText(hyperlink: URL) -> (some Transferable)? {
        // The `out there` situation is required as a result of solely in iOS 16.1 does AttributedString conform to Transferable.
        if #out there(iOS 16.1, *) {
            // The AttributedString choices beneath allow inclusion of whitespace within the textual content formatting.
            // See additionally https://boards.macrumors.com/threads/markdown-linebreaks-are-ignored.2367417/
            return attempt? AttributedString(markdown: shareItemMessage(hyperlink: hyperlink),
                choices: AttributedString.MarkdownParsingOptions(
                    allowsExtendedAttributes: false,
                    interpretedSyntax: .inlineOnlyPreservingWhitespace,
                    failurePolicy: .returnPartiallyParsedIfPossible,
                    languageCode: nil
                ))
        } else {
            return shareItemMessage(hyperlink: hyperlink)
        }
    }

My downside is that the markdown will get stripped out when the consumer chooses the message app. Instance:
enter image description here

And once they use an e mail, I get two hyperlinks. Instance:
enter image description here

Word that I’ve generated the picture examples above working on an iPhone with > iOS 16.1, so it is utilizing the AttributedString formatting for each the message and e mail.

Has anybody provide you with a greater method to do that? i.e., to make use of ShareLink to ship a textual content e mail or message with a hyperlink?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments