[ios] Fix opening websites with percent encoding characters

Signed-off-by: Alexander Borsuk <me@alex.bio>
This commit is contained in:
Alexander Borsuk
2025-08-01 00:11:51 +02:00
committed by Yannik Bloscheck
parent e0c579634b
commit 29d60c9af0

View File

@@ -181,17 +181,18 @@
- (BOOL)openUrl:(NSString *)urlString externally:(BOOL)externally skipEncoding:(BOOL)skipEncoding
{
// TODO: This is a temporary workaround to open cyrillic/non-ASCII URLs.
// URLs in OSM are stored in UTF-8. NSURL constructor documentation says:
// > Must be a URL that conforms to RFC 2396. This method parses URLString according to RFCs 1738 and 1808.
// The right way to encode the URL string should be:
// 1. Split the (non-ASCII) string into components (host, path, query, fragment, etc.)
// 2. Encode each component separately (they have different allowed characters).
// 3. Merge them back into the string and create NSURL.
NSString * encoded;
if (skipEncoding) {
encoded = urlString;
} else {
} else if (![urlString canBeConvertedToEncoding:NSASCIIStringEncoding])
{
// TODO: This is a temporary workaround to open cyrillic/non-ASCII URLs.
// URLs in OSM are stored in UTF-8. NSURL constructor documentation says:
// > Must be a URL that conforms to RFC 2396. This method parses URLString according to RFCs 1738 and 1808.
// The right way to encode the URL string should be:
// 1. Split the (non-ASCII) string into components (host, path, query, fragment, etc.)
// 2. Encode each component separately (they have different allowed characters).
// 3. Merge them back into the string and create NSURL.
NSMutableCharacterSet * charset = [[NSMutableCharacterSet alloc] init];
[charset formUnionWithCharacterSet:NSCharacterSet.URLHostAllowedCharacterSet];
[charset formUnionWithCharacterSet:NSCharacterSet.URLPathAllowedCharacterSet];
@@ -200,7 +201,7 @@
[charset addCharactersInString:@"#;/?:@&=+$,"];
encoded = [urlString stringByAddingPercentEncodingWithAllowedCharacters:charset];
}
// Matrix has an url with two hashes which doesn't work for NSURL and NSURLComponent.
NSRange const matrixUrl = [encoded rangeOfString:@"#/#"];
if (matrixUrl.location != NSNotFound)