Skip to content

feat: introduce openUrlAsync #10637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions packages/core/utils/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ export function openUrl(location: string): boolean {
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
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
);
}

Expand Down
6 changes: 6 additions & 0 deletions packages/core/utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>;

/**
* Opens file.
* @param filePath The file.
Expand Down
23 changes: 21 additions & 2 deletions packages/core/utils/index.ios.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) {
Expand All @@ -50,6 +50,25 @@ export function openUrl(location: string): boolean {
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 {
return iOSUtils.isRealDevice();
}
Expand Down
Loading