feat(core): openUrlAsync utility (#10637)

This commit is contained in:
Eduardo Speroni
2025-01-14 01:43:52 -03:00
committed by GitHub
parent 603b2dc547
commit f6eab0d62f
3 changed files with 45 additions and 4 deletions

View File

@ -40,6 +40,22 @@ export function openUrl(location: string): boolean {
return true; return true;
} }
export function openUrlAsync(location: string): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
try {
const context = AndroidUtils.getApplicationContext();
const intent = new android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(location.trim()));
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
resolve(true);
} catch (e) {
// We don't do anything with an error. We just output it
Trace.write(`Failed to start activity for handling URL: ${location}`, Trace.categories.Error, Trace.messageType.error);
resolve(false);
}
});
}
/** /**
* Check whether external storage is read only * Check whether external storage is read only
* *
@ -98,7 +114,7 @@ External storage is unavailable (please check app permissions).
Applications cannot access internal storage of other application on Android (see: https://developer.android.com/guide/topics/data/data-storage). Applications cannot access internal storage of other application on Android (see: https://developer.android.com/guide/topics/data/data-storage).
`, `,
Trace.categories.Error, Trace.categories.Error,
Trace.messageType.error Trace.messageType.error,
); );
return false; return false;
@ -161,7 +177,7 @@ Applications cannot access internal storage of other application on Android (see
Please ensure you have your manifest correctly configured with the FileProvider. Please ensure you have your manifest correctly configured with the FileProvider.
(see: https://developer.android.com/reference/android/support/v4/content/FileProvider#ProviderDefinition) (see: https://developer.android.com/reference/android/support/v4/content/FileProvider#ProviderDefinition)
`, `,
Trace.categories.Error Trace.categories.Error,
); );
} }

View File

@ -112,6 +112,12 @@ export function isDataURI(uri: string): boolean;
*/ */
export function openUrl(url: string): boolean; export function openUrl(url: string): boolean;
/**
* Opens url asynchronously.
* @param url The url.
*/
export function openUrlAsync(url: string): Promise<boolean>;
/** /**
* Opens file. * Opens file.
* @param filePath The file. * @param filePath The file.

View File

@ -1,5 +1,5 @@
import { Trace } from '../trace'; import { Trace } from '../trace';
import { dataSerialize, ios as iOSUtils } from './native-helper'; import { ios as iOSUtils } from './native-helper';
export { clearInterval, clearTimeout, setInterval, setTimeout } from '../timer'; export { clearInterval, clearTimeout, setInterval, setTimeout } from '../timer';
export * from './common'; export * from './common';
@ -39,7 +39,7 @@ export function openUrl(location: string): boolean {
try { try {
const url = NSURL.URLWithString(location.trim()); const url = NSURL.URLWithString(location.trim());
if (UIApplication.sharedApplication.canOpenURL(url)) { if (UIApplication.sharedApplication.canOpenURL(url)) {
UIApplication.sharedApplication.openURLOptionsCompletionHandler(url, dataSerialize({}), null); openUrlAsync(location);
return true; return true;
} }
} catch (e) { } catch (e) {
@ -50,6 +50,25 @@ export function openUrl(location: string): boolean {
return false; return false;
} }
export function openUrlAsync(location: string): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
try {
const url = NSURL.URLWithString(location.trim());
const app = UIApplication.sharedApplication;
if (app.canOpenURL(url)) {
app.openURLOptionsCompletionHandler(url, null, (success: boolean) => {
resolve(success);
});
} else {
resolve(false);
}
} catch (e) {
Trace.write('Error in OpenURL', Trace.categories.Error, Trace.messageType.error);
reject(e);
}
});
}
export function isRealDevice(): boolean { export function isRealDevice(): boolean {
return iOSUtils.isRealDevice(); return iOSUtils.isRealDevice();
} }