Custom URL Scheme maximum URL length
Asked Answered
C

5

35

As per title, what is the maximum length a URL can be, when using a custom URL scheme with an app?

e.g. If I'm launching another app via URL, and passing a blob of data using something like

   myappscheme://some/path?data=0123456789ABCDEF

how long can that string get before the URL gets cut off (or the system refuses to launch the other app at all)?

Conceit answered 18/7, 2012 at 16:59 Comment(4)
de facto limit of 2000 charactersMuntjac
@MSK (1) that is a different question asking about the length of the scheme part; I am asking about the entire URL (2) when you say "de facto", what's your source?Conceit
check out anser to this question #417642Muntjac
@MSK I am talking about the limit on iOS, for custom URL schemes. Not in a general sense, with Web browsers. That is answering a different question.Conceit
G
31

Unfortunately, the answer does not seem to be available. The max len is not defined specifically in any iOS documentation that I could find.

There are limits, as noted above by MSK's link, for the URL base url length, not the absolute string length of the url. So thats probably a safe bet for a max, around 2000 characters.

However, in testing there seemed to be no upper limit. I was able to use 365,535 character URLs on iOS with no problems. But there is no requirement for apple to continue to support it, so it's a "use at your own risk". I would however, say that based on the fact that many, many apps are using this to transfer information, including large amounts of data between apps, that Apple will probably continue to support it, since there seems to be no reason to reduce the length.

Gratulant answered 3/1, 2013 at 14:38 Comment(2)
I am still able to send URLs with over 10,000,000 characters (iOS 13).Innocent
@Innocent Jesus, i really wonder what that is you are doing o_OResidence
F
12

(The following is a repost from another question's answer, but it directly answers the question here as well.)

On Apple platforms (iOS/iPadOS/macOS/tvOS/watchOS), the limit is a 2 GB long URL scheme, as seen by this comment in the source code of Swift:

// Make sure the URL string isn't too long.
// We're limiting it to 2GB for backwards compatibility with 32-bit executables using NS/CFURL
if ( (urlStringLength > 0) && (urlStringLength <= INT_MAX) )
{
...

On iOS, I've tested and confirmed that even a 300+ MB long URL is accepted. You can try such a long URL like this in Objective-C:

NSString *path = [@"a:" stringByPaddingToLength:314572800 withString:@"a" startingAtIndex:0];
NSString *js = [NSString stringWithFormat:@"window.location.href = \"%@\";", path];
[self.webView stringByEvaluatingJavaScriptFromString:js];

And catch if it succeed with:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"length: %@", @(request.URL.absoluteString.length));
    return YES;
}
Fransis answered 15/11, 2019 at 2:25 Comment(1)
@DustinKerstein No. Feel free to try and post the answer.Artful
H
5

The answers above are misleading. The docs don't really mention it but paths and file names do have length limits.

You can see here there is a limit to the length a file path or name can take up. You can check these in syslimits.h.

#define NAME_MAX          255   /* max bytes in a file name */
#define PATH_MAX         1024   /* max bytes in pathname */

You can log them and see for yourself like so:

NSLog(@"PATH MAX VALUE: %i", PATH_MAX)

Paths are limited at 1024 bytes or 1024 characters in UTF-8. Filenames in paths are limited to 255 bytes or 255 characters in UTF-8.

I ran into this problem trying to deal with files that have encoded Japanese characters (which lengthen the URL/names significantly). My only solution is to crop the name as of right now.

You can see here on this apple support page that they talk about how some languages (like korean or japanese) have characters that take up more bytes and cause problems (they don't really provide a good way to deal with the problem though):

https://support.apple.com/en-is/HT202808

Highlight answered 25/8, 2015 at 23:29 Comment(2)
This is useful information about length limits for file names and file paths. But do we know that this affects URLs? Many URLs do not refer to local files. And even URLs which do refer to local files might be processed by APIs which are subject to different limits than the APIs for direct role manipulation through file names and paths.Mycenae
@Mycenae it is my understanding that URL stands for 'Uniform Resource Locator.' The resources in question here being files, since you use a url within an OS environment and it locates files it stands to reason that file path limits would apply to URL lengths as well. I could be wrong on this though.Highlight
B
5

I have created a simple project to measure the limit, at the moment it seems that even a whopping megabyte is fine. It would not be wise to depend on that number for the future, obviously.

Bayne answered 7/12, 2017 at 10:9 Comment(1)
More than a year before your answer, I had already demonstrated that a whopping 300 megabytes was also fine, and later I found the upper limit to be 2 GB. ;)Artful
G
-2

Reference http://en.wikipedia.org/wiki/URI_scheme :

The scheme name consists of a sequence of characters beginning with a letter and followed by any combination of letters, digits, plus ("+"), period ("."), or hyphen ("-"). Although schemes are case-insensitive, the canonical form is lowercase and documents that specify schemes must do so with lowercase letters. It is followed by a colon (":").

There is nothing about length.

Goldsberry answered 14/1, 2013 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.