This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
AppLinkNavigation.js
108 lines (95 loc) · 2.78 KB
/
AppLinkNavigation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule AppLinkNavigation
* @flow
*/
'use strict';
var React = require('react-native');
var { LinkingIOS } = React;
var AppLinkURL = require('./AppLinkURL');
var USER_AGENT = 'react-native-applinks 0.0.1';
var VERSION = '1.0';
class AppLinkNavigation {
_appLinkResolver: AppLinkResolver;
_refererAppLink: AppLink;
_platform: string;
constructor(
app_link_resolver: AppLinkResolver,
referer_app_link: AppLink,
platform: string
) {
this._appLinkResolver = app_link_resolver;
this._refererAppLink = referer_app_link;
this._platform = platform || 'ios';
switch (this._platform) {
case 'iphone':
case 'ipad':
case 'ios':
break;
default:
throw 'Unexpected platform: ' + platform;
}
}
fetchUrlFromWebUrl(web_url: string, success: Function, error: Function) {
this._appLinkResolver.resolve(
web_url,
(al) => { this.fetchUrlFromAppLink(al, success, error) },
error
);
}
fetchUrlFromAppLink(al: AppLink, success: Function, error: Function) {
if (!al) {
error('Empty applink object.');
}
var targets = al.getTargets('ios');
if (this._platform === 'iphone') {
targets = al.getTargets('iphone').concat(targets);
} else if (this._platform === 'ipad') {
targets = al.getTargets('ipad').concat(targets);
}
this._findBestTarget(targets, 0,
(target) => {
success(this._addApplinkDataToUrl(target.url, al.getURL()))
},
() => {
success(this._addApplinkDataToUrl(al.getWebUrl(), al.getURL()))
}
);
}
/**
* Iterating through the collection of app link targets.
* Selecting first one that can be opened.
*/
_findBestTarget(targets: Array, index: number, onFound: Function, onNotFound: Function) {
if (index >= targets.length) {
onNotFound();
} else {
LinkingIOS.canOpenURL(targets[index].url, (supported) => {
if (supported) {
onFound(targets[index]);
} else {
this._findBestTarget(targets, ++index, onFound, onNotFound);
}
});
}
}
_addApplinkDataToUrl(url: string, originalTargetURL: string) {
var referer = this._refererAppLink || {};
var alData = {
target_url: originalTargetURL,
referer_app_link: referer,
version: VERSION,
user_agent: USER_AGENT
};
var alURL = new AppLinkURL(url);
alURL.appLinkData = alData;
return alURL.getURLString();
}
}
module.exports = AppLinkNavigation;