diff --git a/platform/darwin/src/http_file_source.mm b/platform/darwin/src/http_file_source.mm index 007a0b902d4..d8fbf884363 100644 --- a/platform/darwin/src/http_file_source.mm +++ b/platform/darwin/src/http_file_source.mm @@ -226,8 +226,9 @@ BOOL isValidMapboxEndpoint(NSURL *url) { MLN_APPLE_EXPORT NSURL *resourceURL(const Resource& resource) { - NSURL *url = [NSURL URLWithString:@(resource.url.c_str())]; - + NSString *encodedUrlString = [@(resource.url.c_str()) stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet]; + NSURL *url = [NSURL URLWithString:encodedUrlString]; + #if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR if (isValidMapboxEndpoint(url)) { NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO]; diff --git a/platform/darwin/test/MLNResourceTests.mm b/platform/darwin/test/MLNResourceTests.mm index 401670eecc3..6ad67d63236 100644 --- a/platform/darwin/test/MLNResourceTests.mm +++ b/platform/darwin/test/MLNResourceTests.mm @@ -86,4 +86,15 @@ - (void)testOfflineQueryParameterIsAddedForOfflineResourceForChina { [self internalTestOfflineQueryParameterIsAddedForOfflineResource:testURL]; } +- (void)testResourceURL { + using namespace mbgl; + + // Test URL with characters requiring encoding + Resource resource(Resource::Kind::Unknown, "https://example.com/test?param1=a|b¶m2=c|d"); + NSURL *url = resourceURL(resource); + + XCTAssertNotNil(url); + XCTAssertEqualObjects(url.absoluteString, @"https://example.com/test?param1=a%7Cb¶m2=c%7Cd"); +} + @end