Files
react-native-url-handler/iOS/NTURLHandler/NTURLHandler.m
T
James Ide de7ea51fd4 Lowercase the supported URL schemes, and use scheme list to determine if URL is internal
If a URL has no scheme, it is considered an internal URL. If it has a scheme, we need to lop off the trailing ":" from the protocol and then compare it against the entries in the supported scheme list.

Note that Cocoa does not automatically lowercase the supported schemes (we do this in native though) and Node's url library does.
2015-04-30 15:05:17 -07:00

103 lines
3.4 KiB
Objective-C

// Copyright 2015-present 650 Industries. All rights reserved.
#import "NTURLHandler.h"
@implementation NTURLHandler
RCT_EXPORT_MODULE()
static NSString * const NTURLHandlerOpenURLNotification = @"NTURLHandlerOpenURL";
@synthesize bridge = _bridge;
- (instancetype)init
{
if (self = [super init]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_dispatchOpenURLEvent:)
name:NTURLHandlerOpenURLNotification
object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
/**
* Opens the given URL, which must be an internal URL.
*
* Do not call this from application code to navigate to a given URL. Call -[UIApplication openURL:] instead. This
* method is to be called from the UIApplicationDelegate when it needs to open a URL.
*/
+ (BOOL)openInternalURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{@"url": url}];
if (sourceApplication) {
userInfo[@"sourceApplication"] = sourceApplication;
}
if (annotation) {
userInfo[@"annotation"] = annotation;
}
[[NSNotificationCenter defaultCenter] postNotificationName:NTURLHandlerOpenURLNotification object:self userInfo:userInfo];
return YES;
}
- (void)_dispatchOpenURLEvent:(NSNotification *)notification
{
[self.bridge.eventDispatcher sendDeviceEventWithName:@"NTURLHandler.openURL" body:notification.userInfo];
}
#pragma mark - JavaScript
- (NSDictionary *)constantsToExport
{
NSDictionary *launchOptions = self.bridge.launchOptions;
NSMutableDictionary *constants = [NSMutableDictionary dictionaryWithDictionary:@{
@"schemes": [self _supportedURLSchemes],
@"initialURL": [launchOptions[UIApplicationLaunchOptionsURLKey] absoluteString] ?: [NSNull null],
@"settingsURL": UIApplicationOpenSettingsURLString ?: [NSNull null],
}];
if (launchOptions[UIApplicationLaunchOptionsSourceApplicationKey]) {
constants[@"initialReferrer"] = @{
@"sourceApplication": launchOptions[UIApplicationLaunchOptionsSourceApplicationKey],
@"annotation": launchOptions[UIApplicationLaunchOptionsAnnotationKey],
};
}
return constants;
}
- (NSSet *)_supportedURLSchemes
{
NSMutableSet *schemes = [NSMutableSet set];
NSDictionary *info = [NSBundle mainBundle].infoDictionary;
for (NSDictionary *urlType in info[@"CFBundleURLTypes"]) {
for (NSString *scheme in urlType[@"CFBundleURLSchemes"]) {
[schemes addObject:scheme.lowercaseString];
}
}
return schemes;
}
RCT_REMAP_METHOD(openURL,
openURLString:(NSString *)urlString
successCallback:(RCTResponseSenderBlock)successCallback
errorCallback:(RCTResponseSenderBlock)errorCallback)
{
NSURL *url = [NSURL URLWithString:urlString];
if (!url) {
NSString *message = [NSString stringWithFormat:@"Could not create a URL from \"%@\"", urlString];
errorCallback(@[message]);
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
BOOL opened = [[UIApplication sharedApplication] openURL:url];
successCallback(@[@(opened)]);
});
}
@end