When creating a URL Object Part of my querystring is being removed when sharing on Messenger

I have a URL string that when shared it is sometimes (50% of the time) removing the querystring when shared on Message App via the activity view controller. Here is the code :

func share() {
    // activity items to be shared
    var activityItems : [Any] = []
   // This will return our site url https://www.myurl.com
    if let url_string = Config.getString("link_back_url") { 
        // https://www.myurl.com/123457
        var link_back = "\(url_string)/\((self.listing?.listno)!)"
        let agent = Agent.getDefault()
        if agent.isAgent {
            // https://www.myurl.com/123457?actor=1&share=ios
            link_back = link_back + "?&actor=\(agent.agentID)&share=ios"
            link_back = link_back.trimmingCharacters(in: .whitespacesAndNewlines)
        }
       
        // This is always (100% of the time) showing the correct url
        print(link_back)
       
        if let link_back_url = URL(string: link_back) {
          // The URL will not include the "actor" or "share" when I share on messages 50% of the time
          activityItems.append(link_back_url)
          // If I just append the string and not the url then it will always work but I don't get 
          // the og:title, og:image to show up like it would when I share the url object
 
        } else {
            activityItems.append(link_back)
       
    }
   
    let vc = UIActivityViewController(activityItems: activityItems, applicationActivities: [])
    vc.popoverPresentationController?.sourceView = self.tabBar
    vc.popoverPresentationController?.sourceRect = CGRect(x: 280, y: 0, width: 1, height: 40)
    navigationController?.present(vc, animated: true, completion: nil);
}

Is it Message app or Messenger (that's not the same).


Did you try using


func addingPercentEncoding(withAllowedCharacters allowedCharacters: CharacterSet) -> String?


to return a new string made from the receiver by replacing all characters not in the specified set with percent-encoded characters.


Discussion

Entire URL strings cannot be percent-encoded, because each URL component specifies a different set of allowed characters. For example, the query component of a URL allows the “@” character, but that character must be percent-encoded in the password component.

UTF-8 encoding is used to determine the correct percent-encoded characters. Any characters in allowedCharacters outside of the 7-bit ASCII range are ignored.

Thank you for the reply. I did try this


link_back = link_back.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)

but it didn't work

Read carefully the doc: the characterset must be selected for the payload (that's whre you face problem, isn't it ?).


So use

urlQueryAllowed

instead of

urlHostAllowed


and only on "?&actor=\(agent.agentID)&share=ios"

I updated the code to the following:

link_back = link_back + "?actor=\(agent.agentID)&share=ios".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!


Still doesn't show up correctly. I think at this point I am just going to share the url as a string not a url object. It doesn't look as nice but it at least works.


Thank you for trying to help.

This sort of things is a lot easier if you construct your URL from

URLComponents
, rather than manually building a string and then constructing the URL from that string.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
I have the same problem!
The query part of url gets deleted when it's shared with message app. This bug doesn't happen with other apps like reminders, other third party apps, copy, and Email app. So it seems like a bug in message app. It occurs on iOS 14.4 and 14.5 but not in 11.4.
I'd like to report a bug about it. But now feedback assistant is not working...

Same issue as well, only happens in the message app

When creating a URL Object Part of my querystring is being removed when sharing on Messenger
 
 
Q