Skip to content

Commit

Permalink
helpful error on canOpenURL for missing scheme (#23535)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ericlewis authored and facebook-github-bot committed Feb 20, 2019
1 parent eb414b7 commit 35b6f86
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions Libraries/LinkingIOS/RCTLinkingManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,23 @@ - (void)handleOpenURLNotification:(NSNotification *)notification
return;
}

// TODO: on iOS9 this will fail if URL isn't included in the plist
// we should probably check for that and reject in that case instead of
// simply resolving with NO

// This can be expensive, so we deliberately don't call on main thread
BOOL canOpen = [RCTSharedApplication() canOpenURL:URL];
resolve(@(canOpen));

NSString *scheme = [URL scheme];

// On iOS 9 and above canOpenURL returns NO without a helpful error.
// Check if a custom scheme is being used, and if it exists in LSApplicationQueriesSchemes
if (![[scheme lowercaseString] hasPrefix:@"http"] && ![[scheme lowercaseString] hasPrefix:@"https"]) {
NSArray *querySchemes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"LSApplicationQueriesSchemes"];
if (querySchemes != nil && ([querySchemes containsObject:scheme] || [querySchemes containsObject:[scheme lowercaseString]])) {
resolve(@(canOpen));
} else {
reject(RCTErrorUnspecified, [NSString stringWithFormat:@"Unable to open URL: %@. Add %@ to LSApplicationQueriesSchemes in your Info.plist.", URL, scheme], nil);
}
} else {
resolve(@(canOpen));
}
}

RCT_EXPORT_METHOD(getInitialURL:(RCTPromiseResolveBlock)resolve
Expand Down

0 comments on commit 35b6f86

Please sign in to comment.