deprecation warning for iOS/tvOS: `stringByAddingPercentEscapesUsingEncoding:`
- WARN | [tvOS] xcodebuild: TSMarkdownParser/TSMarkdownParser/TSMarkdownParser.m:253:77: warning: 'stringByAddingPercentEscapesUsingEncoding:' is deprecated: first deprecated in tvOS 9.0 - Use -stringByAddingPercentEncodingWithAllowedCharacters: instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid. [-Wdeprecated-declarations]
But because we are trying to do some error handling here, we would need to manually decompose the string to be aware of the components of the URL to percent-escape.
If someone wants to help, the goal is to recode stringByAddingPercentEscapesUsingEncoding: to work on a string which isn't recognized by URLWithString:
Replace
NSURL* url = [NSURL URLWithString:link] ?: [NSURL URLWithString: [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
With
NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet];
NSURL *url = [NSURL URLWithString:link] ?: [NSURL URLWithString: [link stringByAddingPercentEncodingWithAllowedCharacters:set]];
@uaremad, thank you, but it's a bit more complex: URL is multi parts (host / path ? query # anchor), so it needs various escapes. And default Apple character sets aren't even RFC compliant, so we have to consider extra workarounds like done by https://github.com/Alamofire/Alamofire/blob/ab07523ee93527e79e99037f1a2d596b30689016/Source/ParameterEncoding.swift#L196