[ios] wrap FIRDynamicLinks methods in the RNFirebaseLinks class

This commit is contained in:
Omer Levy 2017-09-30 08:54:11 -04:00
parent df6b2ac4cc
commit ba2a04be98
1 changed files with 73 additions and 25 deletions

View File

@ -9,7 +9,7 @@ static void sendDynamicLink(NSURL *url, id sender) {
[[NSNotificationCenter defaultCenter] postNotificationName:LINKS_DYNAMIC_LINK_RECEIVED [[NSNotificationCenter defaultCenter] postNotificationName:LINKS_DYNAMIC_LINK_RECEIVED
object:sender object:sender
userInfo:@{@"url": url.absoluteString}]; userInfo:@{@"url": url.absoluteString}];
NSLog(@"sendDynamicLinkSuccess: %@", url.absoluteString); NSLog(@"sendDynamicLink Success: %@", url.absoluteString);
} }
@implementation RNFirebaseLinks @implementation RNFirebaseLinks
@ -20,12 +20,12 @@ RCT_EXPORT_MODULE();
self = [super init]; self = [super init];
if (self != nil) { if (self != nil) {
NSLog(@"Setting up RNFirebaseLinks instance"); NSLog(@"Setting up RNFirebaseLinks instance");
[self initializeLinks]; [self initialiseLinks];
} }
return self; return self;
} }
- (void)initializeLinks { - (void)initialiseLinks {
// Set up internal listener to send notification over bridge // Set up internal listener to send notification over bridge
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(sendDynamicLinkEvent:) selector:@selector(sendDynamicLinkEvent:)
@ -40,8 +40,13 @@ RCT_EXPORT_MODULE();
+ (BOOL)application:(UIApplication *)app + (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
sendDynamicLink(url, self); FIRDynamicLink *dynamicLink =
[[FIRDynamicLinks dynamicLinks] dynamicLinkFromCustomSchemeURL:url];
if (dynamicLink) {
sendDynamicLink(dynamicLink.url, self);
return YES; return YES;
}
return NO;
} }
+ (BOOL)application:(UIApplication *)application + (BOOL)application:(UIApplication *)application
@ -49,18 +54,33 @@ RCT_EXPORT_MODULE();
sourceApplication:(NSString *)sourceApplication sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation annotation:(id)annotation
{ {
sendDynamicLink(url, self); FIRDynamicLink *dynamicLink =
[[FIRDynamicLinks dynamicLinks] dynamicLinkFromCustomSchemeURL:url];
if (dynamicLink) {
sendDynamicLink(dynamicLink.url, self);
return YES; return YES;
}
return NO;
} }
+ (BOOL)application:(UIApplication *)application + (BOOL)application:(UIApplication *)application
continueUserActivity:(NSUserActivity *)userActivity continueUserActivity:(NSUserActivity *)userActivity
restorationHandler:(void (^)(NSArray *))restorationHandler restorationHandler:(void (^)(NSArray *))restorationHandler
{ {
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) { BOOL handled = [[FIRDynamicLinks dynamicLinks]
sendDynamicLink(userActivity.webpageURL, self); handleUniversalLink:userActivity.webpageURL
completion:^(FIRDynamicLink * _Nullable dynamicLink, NSError * _Nullable error) {
if (error != nil){
NSLog(@"Failed to handle universal link: %@", [error localizedDescription]);
} }
return YES; else {
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
NSURL* url = dynamicLink ? dynamicLink.url : userActivity.webpageURL;
sendDynamicLink(url, self);
}
}
}];
return handled;
} }
- (NSArray<NSString *> *)supportedEvents { - (NSArray<NSString *> *)supportedEvents {
@ -71,27 +91,55 @@ continueUserActivity:(NSUserActivity *)userActivity
[self sendEventWithName:LINKS_DYNAMIC_LINK_RECEIVED body:notification.userInfo[@"url"]]; [self sendEventWithName:LINKS_DYNAMIC_LINK_RECEIVED body:notification.userInfo[@"url"]];
} }
-(void)handleInitialLinkFromCustomSchemeURL:(NSURL*)url resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
FIRDynamicLink *dynamicLink =
[[FIRDynamicLinks dynamicLinks] dynamicLinkFromCustomSchemeURL:url];
NSString* urlString = dynamicLink ? dynamicLink.url.absoluteString : (id)kCFNull;
NSLog(@"initial link is: %@", urlString);
resolve(urlString);
}
-(void)handleInitialLinkFromUniversalLinkURL:(NSDictionary *)userActivityDictionary resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
NSUserActivity* userActivity = (NSUserActivity*) userActivityDictionary[@"UIApplicationLaunchOptionsUserActivityKey"];
if ([userActivityDictionary[UIApplicationLaunchOptionsUserActivityTypeKey] isEqual:NSUserActivityTypeBrowsingWeb])
{
[[FIRDynamicLinks dynamicLinks]
handleUniversalLink:userActivity.webpageURL
completion:^(FIRDynamicLink * _Nullable dynamicLink, NSError * _Nullable error) {
if (error != nil){
NSLog(@"Failed to handle universal link: %@", [error localizedDescription]);
reject(@"links/failure", @"Failed to handle universal link", error);
}
else {
NSString* urlString = dynamicLink ? dynamicLink.url.absoluteString : userActivity.webpageURL.absoluteString;
NSLog(@"initial link is: %@", urlString);
resolve(urlString);
}
}];
}
else {
NSLog(@"no initial link");
resolve((id)kCFNull);
}
}
RCT_EXPORT_METHOD(getInitialLink:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { RCT_EXPORT_METHOD(getInitialLink:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
NSURL *initialLink = nil;
if (self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey]) { if (self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey]) {
initialLink = self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey]; NSURL* url = (NSURL*)self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey];
[self handleInitialLinkFromCustomSchemeURL:url resolver:resolve rejecter:reject];
} else { } else {
NSDictionary *userActivityDictionary = NSDictionary *userActivityDictionary =
self.bridge.launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey]; self.bridge.launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey];
if ([userActivityDictionary[UIApplicationLaunchOptionsUserActivityTypeKey] isEqual:NSUserActivityTypeBrowsingWeb]) { [self handleInitialLinkFromUniversalLinkURL:userActivityDictionary resolver:resolve rejecter:reject];
initialLink = ((NSUserActivity*) userActivityDictionary[@"UIApplicationLaunchOptionsUserActivityKey"]).webpageURL;
} }
}
NSString* initialLinkString = initialLink ? initialLink.absoluteString : (id)kCFNull;
NSLog(@"getInitialLink: link is: %@", initialLinkString);
resolve(initialLinkString);
} }
RCT_EXPORT_METHOD(createDynamicLink: (NSDictionary *) metadata resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { RCT_EXPORT_METHOD(createDynamicLink: (NSDictionary *) metadata resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
FIRDynamicLinkComponents *components = [self getDynamicLinkComponentsFromMetadata:metadata]; FIRDynamicLinkComponents *components = [self getDynamicLinkComponentsFromMetadata:metadata];
if (components == nil) { if (components == nil) {
reject(@"links/failure", @"error", nil); reject(@"links/failure", @"Failed to create Dynamic Link", nil);
} else { } else {
NSURL *longLink = components.url; NSURL *longLink = components.url;
NSLog(@"created long dynamic link: %@", longLink.absoluteString); NSLog(@"created long dynamic link: %@", longLink.absoluteString);
@ -106,8 +154,8 @@ RCT_EXPORT_METHOD(createShortDynamicLink: (NSDictionary *) metadata resolver:(RC
NSArray *_Nullable warnings, NSArray *_Nullable warnings,
NSError *_Nullable error) { NSError *_Nullable error) {
if (error) { if (error) {
NSLog(@"create short dynamic link failure %@", error.description); NSLog(@"create short dynamic link failure %@", [error localizedDescription]);
reject(@"links/failure", error.description, nil); reject(@"links/failure", @"Failed to create Short Dynamic Link", error);
} }
NSURL *shortLink = shortURL; NSURL *shortLink = shortURL;
NSLog(@"created short dynamic link: %@", shortLink.absoluteString); NSLog(@"created short dynamic link: %@", shortLink.absoluteString);