From ae73164dacd5a4251d56dbf9556e33d767d68184 Mon Sep 17 00:00:00 2001 From: lizhongyi Date: Fri, 26 Jul 2024 19:45:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0iOS=20=E5=B9=B3=E5=8F=B0=20ut?= =?UTF-8?q?s-get-native-view=E6=8F=92=E4=BB=B6=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/component/web-view/web-view.uvue | 4 +- .../utssdk/app-ios/DCGetElementByID.swift | 22 +++++ .../utssdk/app-ios/index.uts | 99 ++++--------------- 3 files changed, 42 insertions(+), 83 deletions(-) create mode 100644 uni_modules/uts-get-native-view/utssdk/app-ios/DCGetElementByID.swift diff --git a/pages/component/web-view/web-view.uvue b/pages/component/web-view/web-view.uvue index 731bd828..60c29abc 100644 --- a/pages/component/web-view/web-view.uvue +++ b/pages/component/web-view/web-view.uvue @@ -112,8 +112,6 @@ }, loading(event : UniWebViewLoadingEvent) { console.log(JSON.stringify(event.detail)); - this.canGoBack = canWebViewGoBack('web-view'); - this.canGoForward = canWebViewGoForward('web-view'); if (this.autoTest) { this.eventLoading = { "tagName": event.target?.tagName, @@ -124,6 +122,8 @@ }, load(event : UniWebViewLoadEvent) { console.log(JSON.stringify(event.detail)); + this.canGoBack = canWebViewGoBack('web-view'); + this.canGoForward = canWebViewGoForward('web-view'); if (this.autoTest) { this.eventLoad = { "tagName": event.target?.tagName, diff --git a/uni_modules/uts-get-native-view/utssdk/app-ios/DCGetElementByID.swift b/uni_modules/uts-get-native-view/utssdk/app-ios/DCGetElementByID.swift new file mode 100644 index 00000000..f60b4608 --- /dev/null +++ b/uni_modules/uts-get-native-view/utssdk/app-ios/DCGetElementByID.swift @@ -0,0 +1,22 @@ +// +// DCUni.swift +// DCGetElementByID +// +// Created by DCloud-iOS-XHY on 2024/7/8. +// + +import DCloudUniappRuntime + +public class DCGetElementByID { + /// 返回一个匹配特定 ID 的元素, 如果不存在,返回 null 规则同 https://doc.dcloud.net.cn/uni-app-x/api/get-element.html#getelementbyid + public static func dcGetElementById(_ id: String) -> UniElement? { + let appManager = UniSDKEngine.self.getAppManager() + if let app = appManager.getCurretApp(), + let pageManager = app.pageManager as? UniPageManagerImpl, + let page = pageManager.getTopPage(), + let domManager = page.document as? UniDomManager { + return domManager.getElementById(id) + } + return nil + } +} diff --git a/uni_modules/uts-get-native-view/utssdk/app-ios/index.uts b/uni_modules/uts-get-native-view/utssdk/app-ios/index.uts index c24f8c2c..39fe984e 100644 --- a/uni_modules/uts-get-native-view/utssdk/app-ios/index.uts +++ b/uni_modules/uts-get-native-view/utssdk/app-ios/index.uts @@ -1,85 +1,22 @@ -/** - * 引用 iOS 系统库,示例如下: - * import { UIDevice } from "UIKit"; - * [可选实现,按需引入] - */ - -/* 引入 interface.uts 文件中定义的变量 */ -import { MyApiOptions, MyApiResult, MyApi, MyApiSync } from '../interface.uts'; - -/* 引入 unierror.uts 文件中定义的变量 */ -import { MyApiFailImpl } from '../unierror'; - -/** - * 引入三方库 - * [可选实现,按需引入] - * - * 在 iOS 平台引入三方库有以下两种方式: - * 1、通过引入三方库framework 或者.a 等方式,需要将 .framework 放到 ./Frameworks 目录下,将.a 放到 ./Libs 目录下。更多信息[详见](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#ios-平台原生配置) - * 2、通过 cocoaPods 方式引入,将要引入的 pod 信息配置到 config.json 文件下的 dependencies-pods 字段下。详细配置方式[详见](https://uniapp.dcloud.net.cn/plugin/uts-ios-cocoapods.html) - * - * 在通过上述任意方式依赖三方库后,使用时需要在文件中 import: - * 示例:import { LottieLoopMode } from 'Lottie' - */ - -/** - * UTSiOS 为平台内置对象,不需要 import 可直接调用其API,[详见](https://uniapp.dcloud.net.cn/uts/utsios.html) - */ - -/** - * 异步方法 - * - * uni-app项目中(vue/nvue)调用示例: - * 1、引入方法声明 import { myApi } from "@/uni_modules/uts-api" - * 2、方法调用 - * myApi({ - * paramA: false, - * complete: (res) => { - * console.log(res) - * } - * }); - * - */ -export const myApi : MyApi = function (options : MyApiOptions) { - - if (options.paramA == true) { - // 返回数据 - const res : MyApiResult = { - fieldA: 85, - fieldB: true, - fieldC: 'some message' - }; - options.success?.(res); - options.complete?.(res); - - } else { - // 返回错误 - let failResult = new MyApiFailImpl(9010001); - options.fail?.(failResult) - options.complete?.(failResult) +import { CanWebViewGoBack, CanWebViewGoForward } from "../interface"; +import { WKWebView } from 'WebKit'; + +export const canWebViewGoBack : CanWebViewGoBack = function (elementId : string) : boolean { + // const element = uni.getElementById(elementId) + const element = DCGetElementByID.dcGetElementById(elementId) + const view = element?.getIOSView(); + if (view != null && view instanceof WKWebView) { + return (view! as WKWebView).canGoBack; } - + return false; } -/** - * 同步方法 - * - * uni-app项目中(vue/nvue)调用示例: - * 1、引入方法声明 import { myApiSync } from "@/uni_modules/uts-api" - * 2、方法调用 - * myApiSync(true); - * - */ -export const myApiSync : MyApiSync = function (paramA : boolean) : MyApiResult { - // 返回数据,根据插件功能获取实际的返回值 - const res : MyApiResult = { - fieldA: 85, - fieldB: paramA, - fieldC: 'some message' - }; - return res; +export const canWebViewGoForward : CanWebViewGoForward = function (elementId : string) : boolean { + // const element = uni.getElementById(elementId) + const element = DCGetElementByID.dcGetElementById(elementId) + const view = element?.getIOSView(); + if (view != null && view instanceof WKWebView) { + return (view! as WKWebView).canGoForward; + } + return false; } - -/** - * 更多插件开发的信息详见:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html - */ -- GitLab