Content-Length: 467875 | pFad | http://github.com/NativeScript/NativeScript/commit/87b50b26b77c795054b025041a78bca1d35302bd

BA feat: introduce openUrlAsync · NativeScript/NativeScript@87b50b2 · GitHub
Skip to content

Commit 87b50b2

Browse files
committed
feat: introduce openUrlAsync
1 parent 148a54b commit 87b50b2

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

packages/core/utils/index.android.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ export function openUrl(location: string): boolean {
4040
return true;
4141
}
4242

43+
export function openUrlAsync(location: string): Promise<boolean> {
44+
return new Promise<boolean>((resolve, reject) => {
45+
try {
46+
const context = AndroidUtils.getApplicationContext();
47+
const intent = new android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(location.trim()));
48+
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
49+
context.startActivity(intent);
50+
resolve(true);
51+
} catch (e) {
52+
// We don't do anything with an error. We just output it
53+
Trace.write(`Failed to start activity for handling URL: ${location}`, Trace.categories.Error, Trace.messageType.error);
54+
resolve(false);
55+
}
56+
});
57+
}
58+
4359
/**
4460
* Check whether external storage is read only
4561
*
@@ -98,7 +114,7 @@ External storage is unavailable (please check app permissions).
98114
Applications cannot access internal storage of other application on Android (see: https://developer.android.com/guide/topics/data/data-storage).
99115
`,
100116
Trace.categories.Error,
101-
Trace.messageType.error
117+
Trace.messageType.error,
102118
);
103119

104120
return false;
@@ -161,7 +177,7 @@ Applications cannot access internal storage of other application on Android (see
161177
Please ensure you have your manifest correctly configured with the FileProvider.
162178
(see: https://developer.android.com/reference/android/support/v4/content/FileProvider#ProviderDefinition)
163179
`,
164-
Trace.categories.Error
180+
Trace.categories.Error,
165181
);
166182
}
167183

packages/core/utils/index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ export function isDataURI(uri: string): boolean;
112112
*/
113113
export function openUrl(url: string): boolean;
114114

115+
/**
116+
* Opens url asynchronously.
117+
* @param url The url.
118+
*/
119+
export function openUrlAsync(url: string): Promise<boolean>;
120+
115121
/**
116122
* Opens file.
117123
* @param filePath The file.

packages/core/utils/index.ios.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Trace } from '../trace';
2-
import { dataSerialize, ios as iOSUtils } from './native-helper';
2+
import { ios as iOSUtils } from './native-helper';
33

44
export { clearInterval, clearTimeout, setInterval, setTimeout } from '../timer';
55
export * from './common';
@@ -39,7 +39,7 @@ export function openUrl(location: string): boolean {
3939
try {
4040
const url = NSURL.URLWithString(location.trim());
4141
if (UIApplication.sharedApplication.canOpenURL(url)) {
42-
UIApplication.sharedApplication.openURLOptionsCompletionHandler(url, dataSerialize({}), null);
42+
openUrlAsync(location);
4343
return true;
4444
}
4545
} catch (e) {
@@ -50,6 +50,25 @@ export function openUrl(location: string): boolean {
5050
return false;
5151
}
5252

53+
export function openUrlAsync(location: string): Promise<boolean> {
54+
return new Promise<boolean>((resolve, reject) => {
55+
try {
56+
const url = NSURL.URLWithString(location.trim());
57+
const app = UIApplication.sharedApplication;
58+
if (app.canOpenURL(url)) {
59+
app.openURLOptionsCompletionHandler(url, null, (success: boolean) => {
60+
resolve(success);
61+
});
62+
} else {
63+
resolve(false);
64+
}
65+
} catch (e) {
66+
Trace.write('Error in OpenURL', Trace.categories.Error, Trace.messageType.error);
67+
reject(e);
68+
}
69+
});
70+
}
71+
5372
export function isRealDevice(): boolean {
5473
return iOSUtils.isRealDevice();
5574
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/NativeScript/NativeScript/commit/87b50b26b77c795054b025041a78bca1d35302bd

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy