Custom URL Scheme maximum URL length

Viewed 19955

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)?

5 Answers

(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;
}
Related