2017-09-04 13:01:00 +00:00
|
|
|
#import "RNFirebaseLinks.h"
|
|
|
|
|
2017-10-14 22:33:30 +00:00
|
|
|
#if __has_include(<FirebaseDynamicLinks/FirebaseDynamicLinks.h>)
|
|
|
|
#import <Firebase.h>
|
2017-09-25 14:34:26 +00:00
|
|
|
#import "RNFirebaseEvents.h"
|
2018-03-22 16:57:33 +00:00
|
|
|
#import "RNFirebaseUtil.h"
|
2017-09-26 13:54:21 +00:00
|
|
|
|
2018-03-22 16:57:33 +00:00
|
|
|
@implementation RNFirebaseLinks
|
2017-09-26 13:54:21 +00:00
|
|
|
|
2018-03-22 16:57:33 +00:00
|
|
|
static RNFirebaseLinks *theRNFirebaseLinks = nil;
|
2018-03-30 08:25:41 +00:00
|
|
|
static NSString *initialLink = nil;
|
|
|
|
static bool jsReady = FALSE;
|
2017-09-26 13:54:21 +00:00
|
|
|
|
2018-03-22 16:57:33 +00:00
|
|
|
+ (nonnull instancetype)instance {
|
2018-03-30 08:25:41 +00:00
|
|
|
// If an event comes in before the bridge has initialised the native module
|
|
|
|
// then we create a temporary instance which handles events until the bridge
|
|
|
|
// and JS side are ready
|
|
|
|
if (theRNFirebaseLinks == nil) {
|
|
|
|
theRNFirebaseLinks = [[RNFirebaseLinks alloc] init];
|
|
|
|
}
|
2018-03-22 16:57:33 +00:00
|
|
|
return theRNFirebaseLinks;
|
|
|
|
}
|
2017-09-04 13:01:00 +00:00
|
|
|
|
|
|
|
RCT_EXPORT_MODULE();
|
|
|
|
|
2017-09-25 14:34:26 +00:00
|
|
|
- (id)init {
|
|
|
|
self = [super init];
|
|
|
|
if (self != nil) {
|
|
|
|
NSLog(@"Setting up RNFirebaseLinks instance");
|
2018-03-22 16:57:33 +00:00
|
|
|
// Set static instance for use from AppDelegate
|
|
|
|
theRNFirebaseLinks = self;
|
2017-09-25 14:34:26 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:57:33 +00:00
|
|
|
// *******************************************************
|
|
|
|
// ** Start AppDelegate methods
|
|
|
|
// *******************************************************
|
|
|
|
|
|
|
|
- (BOOL)application:(UIApplication *)app
|
2017-09-26 13:54:21 +00:00
|
|
|
openURL:(NSURL *)url
|
|
|
|
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
|
2018-03-22 16:57:33 +00:00
|
|
|
FIRDynamicLink *dynamicLink = [[FIRDynamicLinks dynamicLinks] dynamicLinkFromCustomSchemeURL:url];
|
2017-10-14 22:33:30 +00:00
|
|
|
if (dynamicLink && dynamicLink.url) {
|
2018-03-22 16:57:33 +00:00
|
|
|
NSURL* url = dynamicLink.url;
|
2018-03-30 08:25:41 +00:00
|
|
|
[self sendJSEvent:self name:LINKS_LINK_RECEIVED body:url.absoluteString];
|
2017-09-30 12:54:11 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
return NO;
|
2017-09-26 13:54:21 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:57:33 +00:00
|
|
|
- (BOOL)application:(UIApplication *)application
|
2017-09-26 13:54:21 +00:00
|
|
|
continueUserActivity:(NSUserActivity *)userActivity
|
2017-09-30 16:56:58 +00:00
|
|
|
restorationHandler:(void (^)(NSArray *))restorationHandler {
|
2018-03-22 16:57:33 +00:00
|
|
|
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
|
|
|
|
return [[FIRDynamicLinks dynamicLinks]
|
|
|
|
handleUniversalLink:userActivity.webpageURL
|
|
|
|
completion:^(FIRDynamicLink * _Nullable dynamicLink, NSError * _Nullable error) {
|
2018-07-09 21:16:57 +00:00
|
|
|
if (dynamicLink && dynamicLink.url && error == nil) {
|
2018-07-09 19:48:55 +00:00
|
|
|
NSURL* url = dynamicLink.url;
|
2018-03-30 08:25:41 +00:00
|
|
|
[self sendJSEvent:self name:LINKS_LINK_RECEIVED body:url.absoluteString];
|
2018-07-09 19:48:55 +00:00
|
|
|
} else {
|
|
|
|
NSLog(@"Failed to handle universal link: %@", userActivity.webpageURL);
|
2018-03-22 16:57:33 +00:00
|
|
|
}
|
|
|
|
}];
|
2017-09-26 13:54:21 +00:00
|
|
|
}
|
2018-03-22 16:57:33 +00:00
|
|
|
return NO;
|
2017-09-26 13:54:21 +00:00
|
|
|
}
|
2018-03-22 16:57:33 +00:00
|
|
|
// *******************************************************
|
|
|
|
// ** Finish AppDelegate methods
|
|
|
|
// *******************************************************
|
2017-09-25 14:34:26 +00:00
|
|
|
|
2018-03-23 10:33:17 +00:00
|
|
|
- (void)sendLink:(NSString *)link {
|
2018-03-30 08:25:41 +00:00
|
|
|
[self sendJSEvent:self name:LINKS_LINK_RECEIVED body:link];
|
2018-03-23 10:33:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:57:33 +00:00
|
|
|
// ** Start React Module methods **
|
2018-03-23 13:24:31 +00:00
|
|
|
RCT_EXPORT_METHOD(createDynamicLink:(NSDictionary *)linkData
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
2017-10-07 22:00:36 +00:00
|
|
|
@try {
|
2018-03-23 13:24:31 +00:00
|
|
|
FIRDynamicLinkComponents *dynamicLink = [self buildDynamicLink:linkData];
|
2018-03-23 10:33:17 +00:00
|
|
|
|
2018-03-23 13:24:31 +00:00
|
|
|
if (dynamicLink == nil) {
|
2017-10-07 22:00:36 +00:00
|
|
|
reject(@"links/failure", @"Failed to create Dynamic Link", nil);
|
|
|
|
} else {
|
2018-03-23 13:24:31 +00:00
|
|
|
NSString *longLink = dynamicLink.url.absoluteString;
|
|
|
|
NSLog(@"created long dynamic link: %@", longLink);
|
|
|
|
resolve(longLink);
|
2017-10-07 22:00:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
@catch(NSException * e) {
|
|
|
|
NSLog(@"create dynamic link failure %@", e);
|
|
|
|
reject(@"links/failure",[e reason], nil);
|
2017-09-04 13:01:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:24:31 +00:00
|
|
|
RCT_EXPORT_METHOD(createShortDynamicLink:(NSDictionary *)linkData
|
|
|
|
type:(NSString *)type
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
2017-10-07 22:00:36 +00:00
|
|
|
@try {
|
2018-03-23 13:24:31 +00:00
|
|
|
FIRDynamicLinkComponents *components = [self buildDynamicLink:linkData];
|
|
|
|
if (type) {
|
|
|
|
FIRDynamicLinkComponentsOptions *options = [FIRDynamicLinkComponentsOptions options];
|
|
|
|
if ([type isEqual: @"SHORT"]) {
|
|
|
|
options.pathLength = FIRShortDynamicLinkPathLengthShort;
|
|
|
|
} else if ([type isEqual: @"UNGUESSABLE"]) {
|
|
|
|
options.pathLength = FIRShortDynamicLinkPathLengthUnguessable;
|
|
|
|
}
|
|
|
|
components.options = options;
|
|
|
|
}
|
|
|
|
[components shortenWithCompletion:^(NSURL *_Nullable shortURL, NSArray *_Nullable warnings, NSError *_Nullable error) {
|
2017-10-07 22:00:36 +00:00
|
|
|
if (error) {
|
|
|
|
NSLog(@"create short dynamic link failure %@", [error localizedDescription]);
|
|
|
|
reject(@"links/failure", @"Failed to create Short Dynamic Link", error);
|
2018-03-23 13:24:31 +00:00
|
|
|
} else {
|
|
|
|
NSString *shortLink = shortURL.absoluteString;
|
|
|
|
NSLog(@"created short dynamic link: %@", shortLink);
|
|
|
|
resolve(shortLink);
|
2018-01-12 14:39:18 +00:00
|
|
|
}
|
2017-10-07 22:00:36 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
@catch(NSException * e) {
|
|
|
|
NSLog(@"create short dynamic link failure %@", e);
|
|
|
|
reject(@"links/failure",[e reason], nil);
|
|
|
|
}
|
2017-09-04 13:01:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:57:33 +00:00
|
|
|
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];
|
2018-03-30 08:25:41 +00:00
|
|
|
resolve(dynamicLink ? dynamicLink.url.absoluteString : initialLink);
|
2018-03-22 16:57:33 +00:00
|
|
|
} 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 {
|
2018-03-30 08:25:41 +00:00
|
|
|
resolve(initialLink);
|
2018-03-22 16:57:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 08:25:41 +00:00
|
|
|
RCT_EXPORT_METHOD(jsInitialised:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
|
|
|
jsReady = TRUE;
|
|
|
|
resolve(nil);
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:57:33 +00:00
|
|
|
// ** Start internals **
|
2018-03-30 08:25:41 +00:00
|
|
|
|
|
|
|
// Because of the time delay between the app starting and the bridge being initialised
|
|
|
|
// we catch any events that are received before the JS is ready to receive them
|
|
|
|
- (void)sendJSEvent:(RCTEventEmitter *)emitter name:(NSString *)name body:(id)body {
|
|
|
|
if (emitter.bridge && jsReady) {
|
|
|
|
[RNFirebaseUtil sendJSEvent:emitter name:name body:body];
|
|
|
|
} else if (!initialLink) {
|
|
|
|
initialLink = body;
|
|
|
|
} else {
|
2018-03-30 09:07:23 +00:00
|
|
|
NSLog(@"Multiple link events received before the JS links module has been initialised");
|
2018-03-30 08:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:24:31 +00:00
|
|
|
- (FIRDynamicLinkComponents *)buildDynamicLink:(NSDictionary *)linkData {
|
2017-10-07 22:00:36 +00:00
|
|
|
@try {
|
2018-03-23 13:24:31 +00:00
|
|
|
NSURL *link = [NSURL URLWithString:linkData[@"link"]];
|
|
|
|
FIRDynamicLinkComponents *components = [FIRDynamicLinkComponents componentsWithLink:link domain:linkData[@"dynamicLinkDomain"]];
|
2018-03-23 10:33:17 +00:00
|
|
|
|
2018-03-23 13:24:31 +00:00
|
|
|
[self setAnalyticsParameters:linkData[@"analytics"] components:components];
|
|
|
|
[self setAndroidParameters:linkData[@"android"] components:components];
|
|
|
|
[self setIosParameters:linkData[@"ios"] components:components];
|
|
|
|
[self setITunesParameters:linkData[@"itunes"] components:components];
|
|
|
|
[self setNavigationParameters:linkData[@"navigation"] components:components];
|
|
|
|
[self setSocialParameters:linkData[@"social"] components:components];
|
2018-03-23 10:33:17 +00:00
|
|
|
|
2017-10-07 22:00:36 +00:00
|
|
|
return components;
|
|
|
|
}
|
|
|
|
@catch(NSException * e) {
|
|
|
|
NSLog(@"error while building componets from meta data %@", e);
|
|
|
|
@throw;
|
|
|
|
}
|
2017-09-04 13:01:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:24:31 +00:00
|
|
|
- (void)setAnalyticsParameters:(NSDictionary *)analyticsData
|
|
|
|
components:(FIRDynamicLinkComponents *)components {
|
|
|
|
FIRDynamicLinkGoogleAnalyticsParameters *analyticsParams = [FIRDynamicLinkGoogleAnalyticsParameters parameters];
|
|
|
|
|
|
|
|
if (analyticsData[@"campaign"]) {
|
|
|
|
analyticsParams.campaign = analyticsData[@"campaign"];
|
|
|
|
}
|
|
|
|
if (analyticsData[@"content"]) {
|
|
|
|
analyticsParams.content = analyticsData[@"content"];
|
|
|
|
}
|
|
|
|
if (analyticsData[@"medium"]) {
|
|
|
|
analyticsParams.medium = analyticsData[@"medium"];
|
|
|
|
}
|
|
|
|
if (analyticsData[@"source"]) {
|
|
|
|
analyticsParams.source = analyticsData[@"source"];
|
|
|
|
}
|
|
|
|
if (analyticsData[@"term"]) {
|
|
|
|
analyticsParams.term = analyticsData[@"term"];
|
|
|
|
}
|
|
|
|
components.analyticsParameters = analyticsParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setAndroidParameters:(NSDictionary *)androidData
|
2017-09-04 13:01:00 +00:00
|
|
|
components:(FIRDynamicLinkComponents *)components {
|
2018-03-23 13:24:31 +00:00
|
|
|
if (androidData[@"packageName"]) {
|
|
|
|
FIRDynamicLinkAndroidParameters *androidParams = [FIRDynamicLinkAndroidParameters parametersWithPackageName: androidData[@"packageName"]];
|
2018-03-23 10:33:17 +00:00
|
|
|
|
2018-03-23 13:24:31 +00:00
|
|
|
if (androidData[@"fallbackUrl"]) {
|
|
|
|
androidParams.fallbackURL = [NSURL URLWithString:androidData[@"fallbackUrl"]];
|
2017-09-04 13:01:00 +00:00
|
|
|
}
|
2018-03-23 13:24:31 +00:00
|
|
|
if (androidData[@"minimumVersion"]) {
|
|
|
|
androidParams.minimumVersion = [androidData[@"minimumVersion"] integerValue];
|
2017-10-07 22:00:36 +00:00
|
|
|
}
|
2017-09-04 13:01:00 +00:00
|
|
|
components.androidParameters = androidParams;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:24:31 +00:00
|
|
|
- (void)setIosParameters:(NSDictionary *)iosData
|
2017-09-04 13:01:00 +00:00
|
|
|
components:(FIRDynamicLinkComponents *)components {
|
2018-03-23 13:24:31 +00:00
|
|
|
if (iosData[@"bundleId"]) {
|
|
|
|
FIRDynamicLinkIOSParameters *iOSParams = [FIRDynamicLinkIOSParameters parametersWithBundleID:iosData[@"bundleId"]];
|
|
|
|
if (iosData[@"appStoreId"]) {
|
|
|
|
iOSParams.appStoreID = iosData[@"appStoreId"];
|
2017-09-04 13:01:00 +00:00
|
|
|
}
|
2018-03-23 13:24:31 +00:00
|
|
|
if (iosData[@"customScheme"]) {
|
|
|
|
iOSParams.customScheme = iosData[@"customScheme"];
|
2017-09-04 13:01:00 +00:00
|
|
|
}
|
2018-03-23 13:24:31 +00:00
|
|
|
if (iosData[@"fallbackUrl"]) {
|
|
|
|
iOSParams.fallbackURL = [NSURL URLWithString:iosData[@"fallbackUrl"]];
|
2017-09-04 13:01:00 +00:00
|
|
|
}
|
2018-03-23 13:24:31 +00:00
|
|
|
if (iosData[@"iPadBundleId"]) {
|
|
|
|
iOSParams.iPadBundleID = iosData[@"iPadBundleId"];
|
2017-09-04 13:01:00 +00:00
|
|
|
}
|
2018-03-23 13:24:31 +00:00
|
|
|
if (iosData[@"iPadFallbackUrl"]) {
|
|
|
|
iOSParams.iPadFallbackURL = [NSURL URLWithString:iosData[@"iPadFallbackUrl"]];
|
2017-09-04 13:01:00 +00:00
|
|
|
}
|
2018-03-23 13:24:31 +00:00
|
|
|
if (iosData[@"minimumVersion"]) {
|
|
|
|
iOSParams.minimumAppVersion = iosData[@"minimumVersion"];
|
2017-09-04 13:01:00 +00:00
|
|
|
}
|
|
|
|
components.iOSParameters = iOSParams;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:24:31 +00:00
|
|
|
- (void)setITunesParameters:(NSDictionary *)itunesData
|
|
|
|
components:(FIRDynamicLinkComponents *)components {
|
|
|
|
FIRDynamicLinkItunesConnectAnalyticsParameters *itunesParams = [FIRDynamicLinkItunesConnectAnalyticsParameters parameters];
|
|
|
|
if (itunesData[@"affiliateToken"]) {
|
|
|
|
itunesParams.affiliateToken = itunesData[@"affiliateToken"];
|
|
|
|
}
|
|
|
|
if (itunesData[@"campaignToken"]) {
|
|
|
|
itunesParams.campaignToken = itunesData[@"campaignToken"];
|
|
|
|
}
|
|
|
|
if (itunesData[@"providerToken"]) {
|
|
|
|
itunesParams.providerToken = itunesData[@"providerToken"];
|
|
|
|
}
|
|
|
|
components.iTunesConnectParameters = itunesParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setNavigationParameters:(NSDictionary *)navigationData
|
|
|
|
components:(FIRDynamicLinkComponents *)components {
|
|
|
|
FIRDynamicLinkNavigationInfoParameters *navigationParams = [FIRDynamicLinkNavigationInfoParameters parameters];
|
|
|
|
if (navigationData[@"forcedRedirectEnabled"]) {
|
|
|
|
navigationParams.forcedRedirectEnabled = navigationData[@"forcedRedirectEnabled"];
|
2017-09-04 13:01:00 +00:00
|
|
|
}
|
2018-03-23 13:24:31 +00:00
|
|
|
components.navigationInfoParameters = navigationParams;
|
2017-09-04 13:01:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:24:31 +00:00
|
|
|
- (void)setSocialParameters:(NSDictionary *)socialData
|
2017-09-30 12:54:11 +00:00
|
|
|
components:(FIRDynamicLinkComponents *)components {
|
2018-03-23 13:24:31 +00:00
|
|
|
FIRDynamicLinkSocialMetaTagParameters *socialParams = [FIRDynamicLinkSocialMetaTagParameters parameters];
|
|
|
|
if (socialData[@"descriptionText"]) {
|
|
|
|
socialParams.descriptionText = socialData[@"descriptionText"];
|
2017-09-04 13:01:00 +00:00
|
|
|
}
|
2018-03-23 13:24:31 +00:00
|
|
|
if (socialData[@"imageUrl"]) {
|
|
|
|
socialParams.imageURL = [NSURL URLWithString:socialData[@"imageUrl"]];
|
|
|
|
}
|
|
|
|
if (socialData[@"title"]) {
|
|
|
|
socialParams.title = socialData[@"title"];
|
|
|
|
}
|
|
|
|
components.socialMetaTagParameters = socialParams;
|
2017-09-04 13:01:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:57:33 +00:00
|
|
|
- (NSArray<NSString *> *)supportedEvents {
|
|
|
|
return @[LINKS_LINK_RECEIVED];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (BOOL)requiresMainQueueSetup {
|
2017-12-15 16:09:02 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2017-09-04 13:01:00 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
#else
|
|
|
|
@implementation RNFirebaseLinks
|
|
|
|
@end
|
|
|
|
#endif
|