[links] Tidy up links to match messaging and invitations implementation more closely
This commit is contained in:
parent
d1d53a442d
commit
878f928d8c
|
@ -15,9 +15,7 @@
|
|||
@property _Nullable RCTPromiseResolveBlock invitationsResolver;
|
||||
|
||||
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options;
|
||||
|
||||
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;
|
||||
|
||||
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler;
|
||||
|
||||
@end
|
||||
|
@ -28,4 +26,3 @@
|
|||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -92,8 +92,7 @@ RCT_EXPORT_METHOD(getInitialInvitation:(RCTPromiseResolveBlock)resolve rejecter:
|
|||
if (self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey]) {
|
||||
url = (NSURL*)self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey];
|
||||
} else if (self.bridge.launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey]) {
|
||||
NSDictionary *dictionary =
|
||||
self.bridge.launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey];
|
||||
NSDictionary *dictionary = self.bridge.launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey];
|
||||
if ([dictionary[UIApplicationLaunchOptionsUserActivityTypeKey] isEqual:NSUserActivityTypeBrowsingWeb]) {
|
||||
NSUserActivity* userActivity = (NSUserActivity*) dictionary[@"UIApplicationLaunchOptionsUserActivityKey"];
|
||||
url = userActivity.webpageURL;
|
||||
|
|
|
@ -7,21 +7,13 @@
|
|||
#import <React/RCTBridgeModule.h>
|
||||
#import <React/RCTEventEmitter.h>
|
||||
|
||||
@interface RNFirebaseLinks : RCTEventEmitter<RCTBridgeModule> {
|
||||
@interface RNFirebaseLinks : RCTEventEmitter<RCTBridgeModule>
|
||||
|
||||
}
|
||||
+ (BOOL)application:(UIApplication *)app
|
||||
openURL:(NSURL *)url
|
||||
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options;
|
||||
+ (_Nonnull instancetype)instance;
|
||||
|
||||
+ (BOOL)application:(UIApplication *)application
|
||||
openURL:(NSURL *)url
|
||||
sourceApplication:(NSString *)sourceApplication
|
||||
annotation:(id)annotation;
|
||||
|
||||
+ (BOOL)application:(UIApplication *)application
|
||||
continueUserActivity:(NSUserActivity *)userActivity
|
||||
restorationHandler:(void (^)(NSArray *))restorationHandler;
|
||||
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options;
|
||||
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;
|
||||
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler;
|
||||
|
||||
@end
|
||||
|
||||
|
|
|
@ -3,137 +3,80 @@
|
|||
#if __has_include(<FirebaseDynamicLinks/FirebaseDynamicLinks.h>)
|
||||
#import <Firebase.h>
|
||||
#import "RNFirebaseEvents.h"
|
||||
|
||||
|
||||
static void sendDynamicLink(NSURL *url, id sender) {
|
||||
if (url) {
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:LINKS_LINK_RECEIVED
|
||||
object:sender
|
||||
userInfo:@{@"url": url.absoluteString}];
|
||||
NSLog(@"sendDynamicLink Success: %@", url.absoluteString);
|
||||
}
|
||||
}
|
||||
#import "RNFirebaseUtil.h"
|
||||
|
||||
@implementation RNFirebaseLinks
|
||||
|
||||
static RNFirebaseLinks *theRNFirebaseLinks = nil;
|
||||
|
||||
+ (nonnull instancetype)instance {
|
||||
return theRNFirebaseLinks;
|
||||
}
|
||||
|
||||
RCT_EXPORT_MODULE();
|
||||
|
||||
- (id)init {
|
||||
self = [super init];
|
||||
if (self != nil) {
|
||||
NSLog(@"Setting up RNFirebaseLinks instance");
|
||||
[self initialiseLinks];
|
||||
// Set static instance for use from AppDelegate
|
||||
theRNFirebaseLinks = self;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)initialiseLinks {
|
||||
// Set up internal listener to send notification over bridge
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(sendDynamicLinkEvent:)
|
||||
name:LINKS_LINK_RECEIVED
|
||||
object:nil];
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
}
|
||||
|
||||
+ (BOOL)application:(UIApplication *)app
|
||||
// *******************************************************
|
||||
// ** Start AppDelegate methods
|
||||
// *******************************************************
|
||||
|
||||
- (BOOL)application:(UIApplication *)app
|
||||
openURL:(NSURL *)url
|
||||
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
|
||||
return [self handleLinkFromCustomSchemeURL:url];
|
||||
return [self application:app
|
||||
openURL:url
|
||||
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
|
||||
annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
|
||||
}
|
||||
|
||||
+ (BOOL)application:(UIApplication *)application
|
||||
- (BOOL)application:(UIApplication *)application
|
||||
openURL:(NSURL *)url
|
||||
sourceApplication:(NSString *)sourceApplication
|
||||
annotation:(id)annotation {
|
||||
return [self handleLinkFromCustomSchemeURL:url];
|
||||
}
|
||||
|
||||
+ (BOOL)handleLinkFromCustomSchemeURL:(NSURL *)url {
|
||||
FIRDynamicLink *dynamicLink =
|
||||
[[FIRDynamicLinks dynamicLinks] dynamicLinkFromCustomSchemeURL:url];
|
||||
FIRDynamicLink *dynamicLink = [[FIRDynamicLinks dynamicLinks] dynamicLinkFromCustomSchemeURL:url];
|
||||
if (dynamicLink && dynamicLink.url) {
|
||||
NSURL* dynamicLinkUrl = dynamicLink.url;
|
||||
sendDynamicLink(dynamicLinkUrl, self);
|
||||
NSURL* url = dynamicLink.url;
|
||||
[RNFirebaseUtil sendJSEvent:self name:LINKS_LINK_RECEIVED body:url];
|
||||
return YES;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
+ (BOOL)application:(UIApplication *)application
|
||||
- (BOOL)application:(UIApplication *)application
|
||||
continueUserActivity:(NSUserActivity *)userActivity
|
||||
restorationHandler:(void (^)(NSArray *))restorationHandler {
|
||||
BOOL handled = [[FIRDynamicLinks dynamicLinks]
|
||||
handleUniversalLink:userActivity.webpageURL
|
||||
completion:^(FIRDynamicLink * _Nullable dynamicLink, NSError * _Nullable error) {
|
||||
if (error != nil){
|
||||
NSLog(@"Failed to handle universal link: %@", [error localizedDescription]);
|
||||
}
|
||||
else {
|
||||
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
|
||||
NSURL* url = dynamicLink ? dynamicLink.url : userActivity.webpageURL;
|
||||
sendDynamicLink(url, self);
|
||||
}
|
||||
}
|
||||
}];
|
||||
return handled;
|
||||
}
|
||||
|
||||
- (NSArray<NSString *> *)supportedEvents {
|
||||
return @[LINKS_LINK_RECEIVED];
|
||||
}
|
||||
|
||||
- (void)sendDynamicLinkEvent:(NSNotification *)notification {
|
||||
[self sendEventWithName:LINKS_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]
|
||||
return [[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 {
|
||||
NSURL* url = dynamicLink ? dynamicLink.url : userActivity.webpageURL;
|
||||
[RNFirebaseUtil sendJSEvent:self name:LINKS_LINK_RECEIVED body:url];
|
||||
}
|
||||
}];
|
||||
}
|
||||
else {
|
||||
NSLog(@"no initial link");
|
||||
resolve((id)kCFNull);
|
||||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(getInitialLink:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
||||
if (self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey]) {
|
||||
NSURL* url = (NSURL*)self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey];
|
||||
[self handleInitialLinkFromCustomSchemeURL:url resolver:resolve rejecter:reject];
|
||||
|
||||
} else {
|
||||
NSDictionary *userActivityDictionary =
|
||||
self.bridge.launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey];
|
||||
[self handleInitialLinkFromUniversalLinkURL:userActivityDictionary resolver:resolve rejecter:reject];
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
// *******************************************************
|
||||
// ** Finish AppDelegate methods
|
||||
// *******************************************************
|
||||
|
||||
// ** Start React Module methods **
|
||||
RCT_EXPORT_METHOD(createDynamicLink: (NSDictionary *) metadata resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
||||
@try {
|
||||
FIRDynamicLinkComponents *components = [self getDynamicLinkComponentsFromMetadata:metadata];
|
||||
|
@ -176,6 +119,32 @@ RCT_EXPORT_METHOD(createShortDynamicLink: (NSDictionary *) metadata resolver:(RC
|
|||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(getInitialLink:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
||||
if (self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey]) {
|
||||
NSURL* url = (NSURL*)self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey];
|
||||
FIRDynamicLink *dynamicLink = [[FIRDynamicLinks dynamicLinks] dynamicLinkFromCustomSchemeURL:url];
|
||||
resolve(dynamicLink ? dynamicLink.url.absoluteString : nil);
|
||||
} else if (self.bridge.launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey]
|
||||
&& [self.bridge.launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey][UIApplicationLaunchOptionsUserActivityTypeKey] isEqualToString:NSUserActivityTypeBrowsingWeb]) {
|
||||
NSDictionary *dictionary = self.bridge.launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey];
|
||||
NSUserActivity* userActivity = (NSUserActivity*) dictionary[@"UIApplicationLaunchOptionsUserActivityKey"];
|
||||
[[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 {
|
||||
resolve(nil);
|
||||
}
|
||||
}
|
||||
|
||||
// ** Start internals **
|
||||
- (FIRDynamicLinkComponents *)getDynamicLinkComponentsFromMetadata:(NSDictionary *)metadata {
|
||||
@try {
|
||||
NSURL *link = [NSURL URLWithString:metadata[@"link"]];
|
||||
|
@ -272,8 +241,11 @@ RCT_EXPORT_METHOD(createShortDynamicLink: (NSDictionary *) metadata resolver:(RC
|
|||
}
|
||||
}
|
||||
|
||||
+ (BOOL)requiresMainQueueSetup
|
||||
{
|
||||
- (NSArray<NSString *> *)supportedEvents {
|
||||
return @[LINKS_LINK_RECEIVED];
|
||||
}
|
||||
|
||||
+ (BOOL)requiresMainQueueSetup {
|
||||
return YES;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue