[ios] wrap FIRDynamicLinks methods in the RNFirebaseLinks class
This commit is contained in:
parent
df6b2ac4cc
commit
ba2a04be98
|
@ -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,27 +40,47 @@ 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
|
||||||
openURL:(NSURL *)url
|
openURL:(NSURL *)url
|
||||||
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);
|
||||||
|
|
Loading…
Reference in New Issue