Files
jindengchen-ai-report/cool-unix/uni_modules/cool-open-web/utssdk/app-ios/index.uts
2025-11-13 10:36:23 +08:00

58 lines
1.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { URL } from "Foundation";
import { UIApplication } from "UIKit";
/**
* 在iOS设备上打开指定的网页URL
* @param url 要打开的网页地址支持http、https、tel、mailto等协议
* @returns 返回操作结果true表示成功false表示失败
*/
export function openWeb(url: string): boolean {
// 参数验证检查URL是否为空或无效
if (url == null || url.trim() == "") {
console.error("openWeb: URL参数不能为空");
return false;
}
try {
// 创建URL对象用于验证URL格式的有效性
let href = new URL((string = url.trim()));
// 检查URL对象是否创建成功
if (href == null) {
console.error("openWeb: 无效的URL格式 -", url);
return false;
}
// 检查系统版本iOS 16.0及以上版本使用新的API
if (UTSiOS.available("iOS 16.0, *")) {
// iOS 16.0+ 使用 open(_:options:completionHandler:) 方法
// 先检查系统是否支持打开该URL
if (UIApplication.shared.canOpenURL(href!)) {
// 使用新API打开URL传入空的options和completionHandler
UIApplication.shared.open(href!, (options = new Map()), (completionHandler = nil));
console.log("openWeb: 成功使用新API打开URL -", url);
return true;
} else {
console.warn("openWeb: 系统不支持打开该URL协议 -", url);
return false;
}
} else {
// iOS 16.0以下版本使用已弃用但仍可用的 openURL 方法
// 先检查系统是否支持打开该URL
if (UIApplication.shared.canOpenURL(href!)) {
// 使用传统API打开URL
UIApplication.shared.openURL(href!);
console.log("openWeb: 成功使用传统API打开URL -", url);
return true;
} else {
console.warn("openWeb: 系统不支持打开该URL协议 -", url);
return false;
}
}
} catch (e) {
// 捕获可能的异常如URL格式错误等
console.error("openWeb: 打开URL时发生错误 -", e);
return false;
}
}