From 61ca11965046f75e7500e5152c5f2b60df2a2cd5 Mon Sep 17 00:00:00 2001 From: Pavlos Vinieratos Date: Fri, 25 Jan 2019 04:37:39 -0800 Subject: [PATCH] Replace deprecated `stringByReplacingPercentEscapesUsingEncoding:` with `stringByAddingPercentEncodingWithAllowedCharacters:` (#19792) Summary: Replace some NSString deprecated methods. motivation for these prs is less warnings reported on xcode everytime we compile a rn app. N/A [INTERNAL] [DEPRECATIONS] [NSString] - Replace NSString deprecation methods. Pull Request resolved: https://github.com/facebook/react-native/pull/19792 Differential Revision: D8515136 Pulled By: cpojer fbshipit-source-id: 4379ef4e229ef201685b87e54ac859ba3d30a833 --- React/Base/RCTConvert.m | 9 ++++++++- React/DevSupport/RCTInspectorDevServerHelper.mm | 8 ++++---- React/Views/RCTWebView.m | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/React/Base/RCTConvert.m b/React/Base/RCTConvert.m index 7bae9fbc5..fcb8ec167 100644 --- a/React/Base/RCTConvert.m +++ b/React/Base/RCTConvert.m @@ -89,7 +89,14 @@ RCT_CUSTOM_CONVERTER(NSData *, NSData, [json dataUsingEncoding:NSUTF8StringEncod // Check if it has a scheme if ([path rangeOfString:@":"].location != NSNotFound) { - path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + NSMutableCharacterSet *urlAllowedCharacterSet = [NSMutableCharacterSet new]; + [urlAllowedCharacterSet formUnionWithCharacterSet:[NSCharacterSet URLUserAllowedCharacterSet]]; + [urlAllowedCharacterSet formUnionWithCharacterSet:[NSCharacterSet URLPasswordAllowedCharacterSet]]; + [urlAllowedCharacterSet formUnionWithCharacterSet:[NSCharacterSet URLHostAllowedCharacterSet]]; + [urlAllowedCharacterSet formUnionWithCharacterSet:[NSCharacterSet URLPathAllowedCharacterSet]]; + [urlAllowedCharacterSet formUnionWithCharacterSet:[NSCharacterSet URLQueryAllowedCharacterSet]]; + [urlAllowedCharacterSet formUnionWithCharacterSet:[NSCharacterSet URLFragmentAllowedCharacterSet]]; + path = [path stringByAddingPercentEncodingWithAllowedCharacters:urlAllowedCharacterSet]; URL = [NSURL URLWithString:path]; if (URL) { return URL; diff --git a/React/DevSupport/RCTInspectorDevServerHelper.mm b/React/DevSupport/RCTInspectorDevServerHelper.mm index 2901b6833..e157ef5da 100644 --- a/React/DevSupport/RCTInspectorDevServerHelper.mm +++ b/React/DevSupport/RCTInspectorDevServerHelper.mm @@ -33,8 +33,8 @@ static NSString *getServerHost(NSURL *bundleURL, NSNumber *port) static NSURL *getInspectorDeviceUrl(NSURL *bundleURL) { NSNumber *inspectorProxyPort = @8082; - NSString *escapedDeviceName = [[[UIDevice currentDevice] name] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; - NSString *escapedAppName = [[[NSBundle mainBundle] bundleIdentifier] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + NSString *escapedDeviceName = [[[UIDevice currentDevice] name] stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet]; + NSString *escapedAppName = [[[NSBundle mainBundle] bundleIdentifier] stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet]; return [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/inspector/device?name=%@&app=%@", getServerHost(bundleURL, inspectorProxyPort), escapedDeviceName, @@ -44,8 +44,8 @@ static NSURL *getInspectorDeviceUrl(NSURL *bundleURL) static NSURL *getAttachDeviceUrl(NSURL *bundleURL, NSString *title) { NSNumber *metroBundlerPort = @8081; - NSString *escapedDeviceName = [[[UIDevice currentDevice] name] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; - NSString *escapedAppName = [[[NSBundle mainBundle] bundleIdentifier] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + NSString *escapedDeviceName = [[[UIDevice currentDevice] name] stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLHostAllowedCharacterSet]; + NSString *escapedAppName = [[[NSBundle mainBundle] bundleIdentifier] stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLHostAllowedCharacterSet]; return [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/attach-debugger-nuclide?title=%@&device=%@&app=%@", getServerHost(bundleURL, metroBundlerPort), title, diff --git a/React/Views/RCTWebView.m b/React/Views/RCTWebView.m index a634efc3d..8f61e3804 100644 --- a/React/Views/RCTWebView.m +++ b/React/Views/RCTWebView.m @@ -247,7 +247,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder) if (isJSNavigation && [request.URL.host isEqualToString:kPostMessageHost]) { NSString *data = request.URL.query; data = [data stringByReplacingOccurrencesOfString:@"+" withString:@" "]; - data = [data stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + data = [data stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet]; NSMutableDictionary *event = [self baseEvent]; [event addEntriesFromDictionary: @{