简体   繁体   中英

How do I encode a large Data object for a URL in Swift?

I want to open my iOS application using a URL but my url appears to be too long? Is there a limit on the length of the URL? I've searched the web all day on this and it looks like there is no definitive answer. My url contains a base64EncodedString of an object and is about 1,500 bytes.

It looks like this:

MyApp://favorite?Class=eyJzZXR0aW5ncyI6eyJzY2FsZSI6WzAsNCwxMiwxOSwyMF0sImZyZXEiOjIyMCwidHVuaW5nIjoiZXlKelkyRnNZU0k2SWlGY2JpRWdjSFJ2YkdWdGVTNXpZMnhjYmlGY2JsQjBiMnhsYlhrZ01USWdibTkwWlNCelkyRnNaVnh1SURFeVhHNGhYRzRnTVRkY0x6RTJYRzRnT1Z3dk9GeHVJREU1WEM4eE5seHVJRFZjTHpSY2JpQTBYQzh6WEc0Z01URmNMemhjYmlBelhDOHlYRzRnTVROY0x6aGNiaUExWEM4elhHNGdOMXd2TkZ4dUlERTFYQzg0WEc0Z01sd3ZNVnh1SW4wPSIsInNvdW5kIjoiZXlKaGRIUnlhV0oxZEdWeklqcGJleUpoZEhSaFkyc2lPakl1TXprd01EQXdNREF3TURBd01EQXlMQ0prWldOaGVTSTZNQzR6TkRrNU9UazVPVGs1T1RrNU9UazVMQ0pwYzAxMWRHVmtJanBtWVd4elpTd2ljbVZzWldGelpTSTZNQzR6TlRJMU1EQXdNREF3TURBd01EQXhMQ0p3WVhKMGFXRnNJam93TGpVeExDSmhiWEFpT2pBdU1qTXpOelV3TURBd01EQXdNREF3TVN3aWMzVnpkR0ZwYmlJNk1IMHNleUpoZEhSaFkyc2lPakF1TWpVc0ltUmxZMkY1SWpvd0xqRXpOU3dpYVhOTmRYUmxaQ0k2Wm1Gc2MyVXNJbkpsYkdWaGMyVWlPakVzSW5CaGNuUnBZV3dpT2pFc0ltRnRjQ0k2TUM0M05Td2ljM1Z6ZEdGcGJpSTZNSDBzZXlKaGRIUmhZMnNpT2pBdU9URTFNREF3TURBd01EQXdNREF4TXl3aVpHVmpZWGtpT2pBdU5URXlOVEF3TURBd01EQXdNREF3TXl3aWFYTk5kWFJsWkNJNlptRnNjMlVzSW5KbGJHVmhjMlVpT2pBdU1qWXlOU3dpY0dGeWRHbGhiQ0k2TWk0eU5EazVPVGs1T1RrNU9UazVPVGdzSW1GdGNDSTZNQzR3T0RFNU9UazVPVGs1T1RrNU9UazVPU3dpYzNWemRHRnBiaUk2TUgwc2V5SmhkSFJoWTJzaU9qQXNJbVJsWTJGNUlqb3dMakkwTlN3aWFYTk5kWFJsWkNJNlptRnNjMlVzSW5KbGJHVmhjMlVpT2pBdU16Y3lOVEF3TURBd01EQXdNREF3TVN3aWNHRnlkR2xoYkNJNk5TNDNOREl3TURBd01EQXdNREF3TURFc0ltRnRjQ0k2TUM0d05UY3NJbk4xYzNSaGFXNGlPakI5WFN3aWJtRnRaU0k2SWtOc1pXRnlJRUpsYkd3aUxDSmtaWE5qSWpvaVNuVnpkQ0JoYm05MGFHVnlJR0psYkd3Z2MyOTFibVF1SW4wPSIsInRvbmUiOiJBIiwibXV0ZWQiOmZhbHNlfSwibmFtZSI6IlB0b2xlbXkgaW4gQSIsImRlc2MiOiJDbGVhciBCZWxsIn0=

I want to text or email this URL and then have the recipient tap on the link to open my app on their device.

The trouble is the link is wrapped in the mail and messages application and only the "MyApp://favorite?" part is marked in blue as a link. The remainder of the text is not part of the link.

Is there any way to send data like this via a URL?

This doesn't sound like it has anything to do with the URL itself or Swift. The problem you're describing is with "the mail and messages application." It sounds like you're sending the message in plaintext and relying on the data detector to find it. That's asking too much of the data detector, which is a heuristic guessing process. If you want to make it as a link, you should send it as an HTML mail, and mark it with <a href> .

Some notes on the format, though:

This data is double-base64-encoded, which is very wasteful. If you only encoded one time, this would be:

MyApp://favorite?Class=eyJzZXR0aW5ncyI6eyJzY2FsZSI6WzAsNCwxMiwxOSwyMF0sImZyZXEiOjIyMCwidHVuaW5nIjp7InNjYWxhIjoiIVxuISBwdG9sZW15LnNjbFxuIVxuUHRvbGVteSAxMiBub3RlIHNjYWxlXG4gMTJcbiFcbiAxNy8xNlxuIDkvOFxuIDE5LzE2XG4gNS80XG4gNC8zXG4gMTEvOFxuIDMvMlxuIDEzLzhcbiA1LzNcbiA3LzRcbiAxNS84XG4gMi8xXG4ifSwic291bmQiOnsiYXR0cmlidXRlcyI6W3siYXR0YWNrIjoyLjM5MDAwMDAwMDAwMDAwMiwiZGVjYXkiOjAuMzQ5OTk5OTk5OTk5OTk5OSwiaXNNdXRlZCI6ZmFsc2UsInJlbGVhc2UiOjAuMzUyNTAwMDAwMDAwMDAwMSwicGFydGlhbCI6MC41MSwiYW1wIjowLjIzMzc1MDAwMDAwMDAwMDEsInN1c3RhaW4iOjB9LHsiYXR0YWNrIjowLjI1LCJkZWNheSI6MC4xMzUsImlzTXV0ZWQiOmZhbHNlLCJyZWxlYXNlIjoxLCJwYXJ0aWFsIjoxLCJhbXAiOjAuNzUsInN1c3RhaW4iOjB9LHsiYXR0YWNrIjowLjkxNTAwMDAwMDAwMDAwMTMsImRlY2F5IjowLjUxMjUwMDAwMDAwMDAwMDMsImlzTXV0ZWQiOmZhbHNlLCJyZWxlYXNlIjowLjI2MjUsInBhcnRpYWwiOjIuMjQ5OTk5OTk5OTk5OTk4LCJhbXAiOjAuMDgxOTk5OTk5OTk5OTk5OTksInN1c3RhaW4iOjB9LHsiYXR0YWNrIjowLCJkZWNheSI6MC4yNDUsImlzTXV0ZWQiOmZhbHNlLCJyZWxlYXNlIjowLjM3MjUwMDAwMDAwMDAwMDEsInBhcnRpYWwiOjUuNzQyMDAwMDAwMDAwMDAxLCJhbXAiOjAuMDU3LCJzdXN0YWluIjowfV0sIm5hbWUiOiJDbGVhciBCZWxsIiwiZGVzYyI6Ikp1c3QgYW5vdGhlciBiZWxsIHNvdW5kLiJ9LCJ0b25lIjoiQSIsIm11dGVkIjpmYWxzZX0sIm5hbWUiOiJQdG9sZW15IGluIEEiLCJkZXNjIjoiQ2xlYXIgQmVsbCJ9

which saves over 350 bytes over your current approach.

If you run this through gzip before Base64-encoding it, it'd be a third of this size (~550 bytes). And using a binary format, you could probably get this down to ~200 bytes (after base64-encoding) without even getting clever.

That said, URLs are perfectly capable of handling data of this size. I suspect your problem is just asking heuristic systems to detect the URL rather than marking exactly where it is with HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM