新版本
This commit is contained in:
6
utssdk/webview/package.json
Normal file
6
utssdk/webview/package.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "webview",
|
||||
"version": "1.0.0",
|
||||
"description": "Web视图插件,支持在App内打开网页",
|
||||
"main": "src/index.uts"
|
||||
}
|
||||
44
utssdk/webview/src/android-declarations.uts
Normal file
44
utssdk/webview/src/android-declarations.uts
Normal file
@@ -0,0 +1,44 @@
|
||||
// #ifdef APP-ANDROID
|
||||
/**
|
||||
* Android平台声明文件
|
||||
* 用于声明 androidx.browser.customtabs 模块和相关类
|
||||
*/
|
||||
|
||||
declare namespace UTSAndroid {
|
||||
// 获取应用上下文
|
||||
function getAppContext(): android.content.Context;
|
||||
|
||||
namespace android.content {
|
||||
class Context {
|
||||
startActivity(intent: Intent): void;
|
||||
}
|
||||
|
||||
class Intent {
|
||||
constructor(action: string, uri: android.net.Uri);
|
||||
static readonly ACTION_VIEW: string;
|
||||
addFlags(flags: number): Intent;
|
||||
static readonly FLAG_ACTIVITY_NEW_TASK: number;
|
||||
}
|
||||
}
|
||||
|
||||
namespace android.net {
|
||||
class Uri {
|
||||
static parse(uriString: string): Uri;
|
||||
}
|
||||
}
|
||||
|
||||
namespace androidx.browser.customtabs {
|
||||
class CustomTabsIntent {
|
||||
launchUrl(context: android.content.Context, uri: android.net.Uri): void;
|
||||
|
||||
static class Builder {
|
||||
constructor();
|
||||
build(): CustomTabsIntent;
|
||||
setToolbarColor(color: number): Builder;
|
||||
setShowTitle(showTitle: boolean): Builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// #endif
|
||||
98
utssdk/webview/src/index.uts
Normal file
98
utssdk/webview/src/index.uts
Normal file
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* WebView插件 - 用于在App内打开网页
|
||||
* 支持iOS和Android平台
|
||||
*/
|
||||
|
||||
// 定义插件接口
|
||||
export default class WebViewPlugin {
|
||||
/**
|
||||
* 在App内打开网页
|
||||
* @param url 要打开的URL
|
||||
*/
|
||||
openUrl(url: string): void {
|
||||
if (!url || url.trim() === '') {
|
||||
console.error('URL不能为空');
|
||||
return;
|
||||
}
|
||||
|
||||
// 根据平台调用不同的实现
|
||||
// #ifdef APP-IOS
|
||||
this.openUrlInIOS(url);
|
||||
// #endif
|
||||
|
||||
// #ifdef APP-ANDROID
|
||||
this.openUrlInAndroid(url);
|
||||
// #endif
|
||||
|
||||
// #ifndef APP-PLUS
|
||||
// 非App环境下,直接使用uni.navigateTo打开web-view页面
|
||||
uni.navigateTo({
|
||||
url: `/pages/webview/webview?url=${encodeURIComponent(url)}`
|
||||
});
|
||||
// #endif
|
||||
}
|
||||
|
||||
// iOS平台实现
|
||||
// #ifdef APP-IOS
|
||||
private openUrlInIOS(url: string): void {
|
||||
try {
|
||||
const { DispatchQueue } = UTSiOS.UIKit;
|
||||
const { SFSafariViewController } = UTSiOS.SafariServices;
|
||||
|
||||
const urlObject = UTSiOS.URL.URLWithString(url);
|
||||
if (!urlObject) {
|
||||
console.error('无效的URL');
|
||||
return;
|
||||
}
|
||||
|
||||
const vc = new SFSafariViewController(url: urlObject);
|
||||
|
||||
// 在主线程上执行UI操作
|
||||
DispatchQueue.main.async((): void => {
|
||||
UTSiOS.getCurrentViewController().presentViewController(vc, animated: true, completion: null);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('iOS打开URL失败:', error);
|
||||
// 失败后使用系统浏览器打开
|
||||
plus.runtime.openURL(url);
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
|
||||
// Android平台实现
|
||||
// #ifdef APP-ANDROID
|
||||
private openUrlInAndroid(url: string): void {
|
||||
try {
|
||||
const Intent = UTSAndroid.android.content.Intent;
|
||||
const Uri = UTSAndroid.android.net.Uri;
|
||||
|
||||
// 创建CustomTabsIntent
|
||||
const CustomTabsIntent = UTSAndroid.androidx.browser.customtabs.CustomTabsIntent;
|
||||
const builder = new CustomTabsIntent.Builder();
|
||||
const customTabsIntent = builder.build();
|
||||
|
||||
// 打开URL
|
||||
const uri = Uri.parse(url);
|
||||
const context = UTSAndroid.getAppContext();
|
||||
|
||||
customTabsIntent.launchUrl(context, uri);
|
||||
} catch (error) {
|
||||
console.error('Android打开URL失败:', error);
|
||||
try {
|
||||
// 如果CustomTabs不可用,尝试使用WebView Activity
|
||||
const Intent = UTSAndroid.android.content.Intent;
|
||||
const Uri = UTSAndroid.android.net.Uri;
|
||||
|
||||
const intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
|
||||
const context = UTSAndroid.getAppContext();
|
||||
context.startActivity(intent);
|
||||
} catch (fallbackError) {
|
||||
// 所有方法都失败,使用plus API
|
||||
plus.runtime.openURL(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
}
|
||||
48
utssdk/webview/src/ios-declarations.uts
Normal file
48
utssdk/webview/src/ios-declarations.uts
Normal file
@@ -0,0 +1,48 @@
|
||||
// #ifdef APP-IOS
|
||||
/**
|
||||
* iOS平台声明文件
|
||||
* 用于声明 SafariServices 模块和相关类
|
||||
*/
|
||||
|
||||
declare namespace UTSiOS {
|
||||
namespace SafariServices {
|
||||
/**
|
||||
* SFSafariViewController 类
|
||||
* 用于在App内显示网页内容
|
||||
*/
|
||||
class SFSafariViewController {
|
||||
constructor(url: UTSiOS.URL);
|
||||
|
||||
// 设置配置
|
||||
static configuration: any;
|
||||
|
||||
// 设置代理
|
||||
setDelegate(delegate: any): void;
|
||||
|
||||
// 设置首选的条形颜色
|
||||
setPreferredBarTintColor(color: any): void;
|
||||
|
||||
// 设置首选的控制颜色
|
||||
setPreferredControlTintColor(color: any): void;
|
||||
|
||||
// 设置模态展示样式
|
||||
setModalPresentationStyle(style: number): void;
|
||||
}
|
||||
}
|
||||
|
||||
// URL类
|
||||
class URL {
|
||||
static URLWithString(string: string): URL;
|
||||
}
|
||||
|
||||
namespace UIKit {
|
||||
class DispatchQueue {
|
||||
static readonly main: DispatchQueue;
|
||||
async(closure: () => void): void;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取当前视图控制器
|
||||
function getCurrentViewController(): any;
|
||||
}
|
||||
// #endif
|
||||
10
utssdk/webview/tsconfig.json
Normal file
10
utssdk/webview/tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "esnext",
|
||||
"module": "esnext",
|
||||
"strict": true,
|
||||
"jsx": "preserve",
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user