diff --git a/packages/core/utils/index.android.ts b/packages/core/utils/index.android.ts index b66d6bbdad..bb0826e443 100644 --- a/packages/core/utils/index.android.ts +++ b/packages/core/utils/index.android.ts @@ -40,6 +40,22 @@ export function openUrl(location: string): boolean { return true; } +export function openUrlAsync(location: string): Promise { + return new Promise((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 * @@ -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). `, Trace.categories.Error, - Trace.messageType.error + Trace.messageType.error, ); 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. (see: https://developer.android.com/reference/android/support/v4/content/FileProvider#ProviderDefinition) `, - Trace.categories.Error + Trace.categories.Error, ); } diff --git a/packages/core/utils/index.d.ts b/packages/core/utils/index.d.ts index c551cbdcfc..79389d3977 100644 --- a/packages/core/utils/index.d.ts +++ b/packages/core/utils/index.d.ts @@ -112,6 +112,12 @@ export function isDataURI(uri: string): boolean; */ export function openUrl(url: string): boolean; +/** + * Opens url asynchronously. + * @param url The url. + */ +export function openUrlAsync(url: string): Promise; + /** * Opens file. * @param filePath The file. diff --git a/packages/core/utils/index.ios.ts b/packages/core/utils/index.ios.ts index 2d4d25da2e..891247f67e 100644 --- a/packages/core/utils/index.ios.ts +++ b/packages/core/utils/index.ios.ts @@ -1,5 +1,5 @@ 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 * from './common'; @@ -39,7 +39,7 @@ export function openUrl(location: string): boolean { try { const url = NSURL.URLWithString(location.trim()); if (UIApplication.sharedApplication.canOpenURL(url)) { - UIApplication.sharedApplication.openURLOptionsCompletionHandler(url, dataSerialize({}), null); + openUrlAsync(location); return true; } } catch (e) { @@ -50,6 +50,25 @@ export function openUrl(location: string): boolean { return false; } +export function openUrlAsync(location: string): Promise { + return new Promise((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 { return iOSUtils.isRealDevice(); }