-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·77 lines (70 loc) · 1.97 KB
/
index.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
$.index.open();
if (OS_IOS) {
Ti.App.iOS.addEventListener('handleurl', (e) => {
/**
* When we launch on iOS we get an object like below, `launchOptions` is the URL used to launch the application.
*
* {
* "launchOptions":{
* "url":"tisample://"
* },
* "bubbles":true,
* "type":"handleurl",
* "source":{},
* "cancelBubble":false
* }
*/
console.log(`iOS handled event ${JSON.stringify(e)}`);
handleURL(e);
});
} else if (OS_ANDROID) {
const deeply = require('ti.deeply');
deeply.setCallback((e) => {
/**
* When we launch on Android we get an object like below, here we'll just normalise it to match iOS
* {
* "data":"tisample://alert?name=Titanium&whatIsIt=awesome",
* "action":"android.intent.action.VIEW",
* "extras":null
* }
*/
console.log(`Android handled event ${JSON.stringify(e)}`);
const eventData = { launchOptions: { url: e.data } };
handleURL(eventData);
});
}
function parseQueryParams(url) {
const queryParams = {};
const pairs = decodeURI(url).split('&');
for (const pair of pairs) {
const keyValuePair = pair.split('=');
queryParams[keyValuePair[0]] = keyValuePair[1];
}
return queryParams;
}
function handleURL (eventData) {
/**
* The URL in an event is just like a web URL,
* you can pass data along using query parameters and
* then the app can then extract the action and data from the URL like below
*
* For example:
* tisample://alert?somedata=foobar
* tisample://updateLabel?somedata=123456
*/
console.log(`Handled URL, data was ${JSON.stringify(eventData)}`);
const [ ignore, action, rawQueryParams ] = eventData.launchOptions.url.match(/tisample:\/\/(\w+)\??(.+)/);
const data = parseQueryParams(rawQueryParams);
switch (action) {
case 'alert':
alert(data);
break;
case 'updateLabel':
$.displayLabel.text = data;
break
default:
console.warn(`Unknown action ${action}`);
console.log(`data: ${JSON.stringify(data)}`);
break;
}
}