Skip to content

Commit 35b6f86

Browse files
ericlewisfacebook-github-bot
authored andcommitted
helpful error on canOpenURL for missing scheme (#23535)
Summary: iOS 9 introduced a whitelist for schemes that apps are allowed to open / check against, the current behavior of React Native is to simple return `NO` when a scheme is missing from that whitelist. It would be more helpful to throw an error with a suggested fix for the problem: ``` Unable to open URL: asos://checkout, add asos to LSApplicationQueriesSchemes in Info.plist. ``` [iOS] [Changed] - canOpenURL throws when custom scheme isn't in LSApplicationQueriesSchemes. Pull Request resolved: #23535 Differential Revision: D14143005 Pulled By: cpojer fbshipit-source-id: 4ead5f073690e627b4a4bbe3fa5a6cb5af46b589
1 parent eb414b7 commit 35b6f86

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

Libraries/LinkingIOS/RCTLinkingManager.m

+15-5
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,23 @@ - (void)handleOpenURLNotification:(NSNotification *)notification
106106
return;
107107
}
108108

109-
// TODO: on iOS9 this will fail if URL isn't included in the plist
110-
// we should probably check for that and reject in that case instead of
111-
// simply resolving with NO
112-
113109
// This can be expensive, so we deliberately don't call on main thread
114110
BOOL canOpen = [RCTSharedApplication() canOpenURL:URL];
115-
resolve(@(canOpen));
111+
112+
NSString *scheme = [URL scheme];
113+
114+
// On iOS 9 and above canOpenURL returns NO without a helpful error.
115+
// Check if a custom scheme is being used, and if it exists in LSApplicationQueriesSchemes
116+
if (![[scheme lowercaseString] hasPrefix:@"http"] && ![[scheme lowercaseString] hasPrefix:@"https"]) {
117+
NSArray *querySchemes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"LSApplicationQueriesSchemes"];
118+
if (querySchemes != nil && ([querySchemes containsObject:scheme] || [querySchemes containsObject:[scheme lowercaseString]])) {
119+
resolve(@(canOpen));
120+
} else {
121+
reject(RCTErrorUnspecified, [NSString stringWithFormat:@"Unable to open URL: %@. Add %@ to LSApplicationQueriesSchemes in your Info.plist.", URL, scheme], nil);
122+
}
123+
} else {
124+
resolve(@(canOpen));
125+
}
116126
}
117127

118128
RCT_EXPORT_METHOD(getInitialURL:(RCTPromiseResolveBlock)resolve

0 commit comments

Comments
 (0)